forked from TrueCloudLab/frostfs-node
[#1437] logger: Use context to log trace id
Signed-off-by: Dmitrii Stepanov
This commit is contained in:
parent
fd004add00
commit
c16dae8b4d
3 changed files with 59 additions and 8 deletions
40
pkg/util/logger/log.go
Normal file
40
pkg/util/logger/log.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/tracing"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func (l *Logger) Debug(ctx context.Context, msg string, fields ...zap.Field) {
|
||||
if traceID := tracing.GetTraceID(ctx); traceID != "" {
|
||||
l.z.Debug(msg, append(fields, zap.String("trace_id", traceID))...)
|
||||
return
|
||||
}
|
||||
l.z.Debug(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *Logger) Info(ctx context.Context, msg string, fields ...zap.Field) {
|
||||
if traceID := tracing.GetTraceID(ctx); traceID != "" {
|
||||
l.z.Info(msg, append(fields, zap.String("trace_id", traceID))...)
|
||||
return
|
||||
}
|
||||
l.z.Info(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *Logger) Warn(ctx context.Context, msg string, fields ...zap.Field) {
|
||||
if traceID := tracing.GetTraceID(ctx); traceID != "" {
|
||||
l.z.Warn(msg, append(fields, zap.String("trace_id", traceID))...)
|
||||
return
|
||||
}
|
||||
l.z.Warn(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *Logger) Error(ctx context.Context, msg string, fields ...zap.Field) {
|
||||
if traceID := tracing.GetTraceID(ctx); traceID != "" {
|
||||
l.z.Error(msg, append(fields, zap.String("trace_id", traceID))...)
|
||||
return
|
||||
}
|
||||
l.z.Error(msg, fields...)
|
||||
}
|
|
@ -12,7 +12,7 @@ import (
|
|||
// Logger represents a component
|
||||
// for writing messages to log.
|
||||
type Logger struct {
|
||||
*zap.Logger
|
||||
z *zap.Logger
|
||||
lvl zap.AtomicLevel
|
||||
}
|
||||
|
||||
|
@ -136,7 +136,7 @@ func newConsoleLogger(prm *Prm) (*Logger, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
l := &Logger{Logger: lZap, lvl: lvl}
|
||||
l := &Logger{z: lZap, lvl: lvl}
|
||||
prm._log = l
|
||||
|
||||
return l, nil
|
||||
|
@ -169,7 +169,7 @@ func newJournaldLogger(prm *Prm) (*Logger, error) {
|
|||
|
||||
lZap := zap.New(coreWithContext, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)))
|
||||
|
||||
l := &Logger{Logger: lZap, lvl: lvl}
|
||||
l := &Logger{z: lZap, lvl: lvl}
|
||||
prm._log = l
|
||||
|
||||
return l, nil
|
||||
|
@ -179,3 +179,13 @@ func (l *Logger) reload(prm Prm) error {
|
|||
l.lvl.SetLevel(prm.level)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *Logger) WithOptions(options ...zap.Option) {
|
||||
l.z = l.z.WithOptions(options...)
|
||||
}
|
||||
|
||||
func NewLoggerWrapper(z *zap.Logger) *Logger {
|
||||
return &Logger{
|
||||
z: z,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,9 +11,10 @@ import (
|
|||
|
||||
// NewLogger creates a new logger.
|
||||
func NewLogger(t testing.TB) *logger.Logger {
|
||||
var l logger.Logger
|
||||
l.Logger = zaptest.NewLogger(t,
|
||||
zaptest.Level(zapcore.DebugLevel),
|
||||
zaptest.WrapOptions(zap.Development(), zap.AddCaller()))
|
||||
return &l
|
||||
return logger.NewLoggerWrapper(
|
||||
zaptest.NewLogger(t,
|
||||
zaptest.Level(zapcore.DebugLevel),
|
||||
zaptest.WrapOptions(zap.Development(), zap.AddCaller()),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue