add support for context attrs funcs
Some checks failed
build / test (push) Has been cancelled
build / lint (push) Has been cancelled
codeql / analyze (go) (push) Has been cancelled

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-03-26 08:35:48 +03:00
parent 19cd5176ff
commit 5a9dd890c9

View File

@ -214,17 +214,35 @@ func (l *zeroLogger) Log(ctx context.Context, level logger.Level, args ...interf
return return
} }
var attrs []interface{}
for _, fn := range l.opts.Options.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...)
}
msg := fmt.Sprint(args...) msg := fmt.Sprint(args...)
if len(attrs) > 0 {
l.zLog.WithLevel(loggerToZerologLevel(level)).Fields(attrs).Msg(msg)
} else {
l.zLog.WithLevel(loggerToZerologLevel(level)).Msg(msg) l.zLog.WithLevel(loggerToZerologLevel(level)).Msg(msg)
} }
}
func (l *zeroLogger) Logf(ctx context.Context, level logger.Level, format string, args ...interface{}) { func (l *zeroLogger) Logf(ctx context.Context, level logger.Level, format string, args ...interface{}) {
if !l.V(level) { if !l.V(level) {
return return
} }
var attrs []interface{}
for _, fn := range l.opts.Options.ContextAttrFuncs {
attrs = append(attrs, fn(ctx)...)
}
if len(attrs) > 0 {
l.zLog.WithLevel(loggerToZerologLevel(level)).Fields(attrs).Msgf(format, args...)
} else {
l.zLog.WithLevel(loggerToZerologLevel(level)).Msgf(format, args...) l.zLog.WithLevel(loggerToZerologLevel(level)).Msgf(format, args...)
} }
}
func (l *zeroLogger) String() string { func (l *zeroLogger) String() string {
return "zerolog" return "zerolog"