Skip to content

Commit

Permalink
fix: produer logger bug and little refine (#224)
Browse files Browse the repository at this point in the history
* fix: init logger typo

* fix: produer logger bug and little refine
  • Loading branch information
wxybear authored Aug 29, 2023
1 parent 6241b3a commit c91071d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 53 deletions.
48 changes: 21 additions & 27 deletions consumer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package consumerLibrary

import (
"os"
"io"
"sync"
"time"

Expand Down Expand Up @@ -169,23 +170,31 @@ func (consumerWorker *ConsumerWorker) cleanShardConsumer(owned_shards []int) {
)
}

// This function is used to initialize the global log configuration
// This function is used to initialize the global logger
func logConfig(option LogHubConfig) log.Logger {
var logger log.Logger

var writer io.Writer
if option.LogFileName == "" {
if option.IsJsonType {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
} else {
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
}
writer = log.NewSyncWriter(os.Stdout)
} else {
if option.IsJsonType {
logger = log.NewJSONLogger(initLogFlusher(option))
} else {
logger = log.NewLogfmtLogger(initLogFlusher(option))
if option.LogMaxSize == 0 {
option.LogMaxSize = 10
}
if option.LogMaxBackups == 0 {
option.LogMaxBackups = 10
}
writer = &lumberjack.Logger{
Filename: option.LogFileName,
MaxSize: option.LogMaxSize,
MaxBackups: option.LogMaxBackups,
Compress: option.LogCompass,
}
}
var logger log.Logger
if option.IsJsonType {
logger = log.NewJSONLogger(writer)
} else {
logger = log.NewLogfmtLogger(writer)
}
switch option.AllowLogLevel {
case "debug":
logger = level.NewFilter(logger, level.AllowDebug())
Expand All @@ -201,18 +210,3 @@ func logConfig(option LogHubConfig) log.Logger {
logger = log.With(logger, "time", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return logger
}

func initLogFlusher(option LogHubConfig) *lumberjack.Logger {
if option.LogMaxSize == 0 {
option.LogMaxSize = 10
}
if option.LogMaxBackups == 0 {
option.LogMaxBackups = 10
}
return &lumberjack.Logger{
Filename: option.LogFileName,
MaxSize: option.LogMaxSize,
MaxBackups: option.LogMaxBackups,
Compress: option.LogCompass,
}
}
47 changes: 21 additions & 26 deletions producer/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ import (
"github.com/go-kit/kit/log/level"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"io"
)

func logConfig(producerConfig *ProducerConfig) log.Logger {
var logger log.Logger

var writer io.Writer
if producerConfig.LogFileName == "" {
if producerConfig.IsJsonType {
logger = log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
} else {
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
}
writer = log.NewSyncWriter(os.Stdout)
} else {
if producerConfig.IsJsonType {
logger = log.NewLogfmtLogger(initLogFlusher(producerConfig))
} else {
logger = log.NewJSONLogger(initLogFlusher(producerConfig))
if producerConfig.LogMaxSize == 0 {
producerConfig.LogMaxSize = 10
}
if producerConfig.LogMaxBackups == 0 {
producerConfig.LogMaxBackups = 10
}
writer = &lumberjack.Logger{
Filename: producerConfig.LogFileName,
MaxSize: producerConfig.LogMaxSize,
MaxBackups: producerConfig.LogMaxBackups,
Compress: producerConfig.LogCompress,
}
}
var logger log.Logger
if producerConfig.IsJsonType {
logger = log.NewJSONLogger(writer)
} else {
logger = log.NewLogfmtLogger(writer)
}

switch producerConfig.AllowLogLevel {
case "debug":
logger = level.NewFilter(logger, level.AllowDebug())
Expand All @@ -38,18 +48,3 @@ func logConfig(producerConfig *ProducerConfig) log.Logger {
logger = log.With(logger, "time", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
return logger
}

func initLogFlusher(producerConfig *ProducerConfig) *lumberjack.Logger {
if producerConfig.LogMaxSize == 0 {
producerConfig.LogMaxSize = 10
}
if producerConfig.LogMaxBackups == 0 {
producerConfig.LogMaxBackups = 10
}
return &lumberjack.Logger{
Filename: producerConfig.LogFileName,
MaxSize: producerConfig.LogMaxSize,
MaxBackups: producerConfig.LogMaxBackups,
Compress: producerConfig.LogCompress,
}
}

0 comments on commit c91071d

Please sign in to comment.