Skip to content

Commit

Permalink
Setup sarama logger (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
vadimalekseev authored Oct 18, 2023
1 parent edb8f5c commit 82f9ea3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/file.d/file.d.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
kingpin.Version(buildinfo.Version)
kingpin.Parse()

logger.Infof("Hi! I'm file.d version=%s %s", buildinfo.Version)
logger.Infof("Hi! I'm file.d version=%s", buildinfo.Version)

setRuntimeSettings()
insaneJSON.DisableBeautifulErrors = true
Expand Down
50 changes: 29 additions & 21 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/Shopify/sarama"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
Expand Down Expand Up @@ -33,27 +34,34 @@ func init() {

Level = zap.NewAtomicLevelAt(level)

Instance = zap.New(
zapcore.NewCore(
zapcore.NewJSONEncoder(zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: func(time time.Time, encoder zapcore.PrimitiveArrayEncoder) {
zapcore.RFC3339NanoTimeEncoder(time.UTC(), encoder)
},
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}),
zapcore.AddSync(os.Stderr),
Level,
),
).Sugar().Named("fd")
config := zapcore.EncoderConfig{
TimeKey: "ts",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: func(time time.Time, encoder zapcore.PrimitiveArrayEncoder) {
zapcore.RFC3339NanoTimeEncoder(time.UTC(), encoder)
},
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
}

core := zapcore.NewCore(zapcore.NewJSONEncoder(config), zapcore.AddSync(os.Stderr), Level)
Instance = zap.New(core).
Sugar().
Named("fd")

saramaConfig := config
// omit level key for sarama logger
saramaConfig.LevelKey = zapcore.OmitKey
saramaCore := zapcore.NewCore(zapcore.NewJSONEncoder(saramaConfig), zapcore.AddSync(os.Stderr), Level)
saramaLogger := zap.New(saramaCore).Named("sarama")
sarama.Logger, _ = zap.NewStdLogAt(saramaLogger, zapcore.InfoLevel)
sarama.DebugLogger, _ = zap.NewStdLogAt(saramaLogger, zapcore.DebugLevel)

Instance.Infof("Logger initialized with level: %s", level)
}
Expand Down

0 comments on commit 82f9ea3

Please sign in to comment.