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
|
// Logger represents a component
|
||||||
// for writing messages to log.
|
// for writing messages to log.
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
*zap.Logger
|
z *zap.Logger
|
||||||
lvl zap.AtomicLevel
|
lvl zap.AtomicLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ func newConsoleLogger(prm *Prm) (*Logger, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
l := &Logger{Logger: lZap, lvl: lvl}
|
l := &Logger{z: lZap, lvl: lvl}
|
||||||
prm._log = l
|
prm._log = l
|
||||||
|
|
||||||
return l, nil
|
return l, nil
|
||||||
|
@ -169,7 +169,7 @@ func newJournaldLogger(prm *Prm) (*Logger, error) {
|
||||||
|
|
||||||
lZap := zap.New(coreWithContext, zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)))
|
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
|
prm._log = l
|
||||||
|
|
||||||
return l, nil
|
return l, nil
|
||||||
|
@ -179,3 +179,13 @@ func (l *Logger) reload(prm Prm) error {
|
||||||
l.lvl.SetLevel(prm.level)
|
l.lvl.SetLevel(prm.level)
|
||||||
return nil
|
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.
|
// NewLogger creates a new logger.
|
||||||
func NewLogger(t testing.TB) *logger.Logger {
|
func NewLogger(t testing.TB) *logger.Logger {
|
||||||
var l logger.Logger
|
return logger.NewLoggerWrapper(
|
||||||
l.Logger = zaptest.NewLogger(t,
|
zaptest.NewLogger(t,
|
||||||
zaptest.Level(zapcore.DebugLevel),
|
zaptest.Level(zapcore.DebugLevel),
|
||||||
zaptest.WrapOptions(zap.Development(), zap.AddCaller()))
|
zaptest.WrapOptions(zap.Development(), zap.AddCaller()),
|
||||||
return &l
|
),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue