1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-01-17 17:44:30 +02:00

logger: caller's skip correction (#2280)

This commit is contained in:
Arsen 2021-09-22 11:01:03 +03:00 committed by GitHub
parent b8fbe87e1f
commit 916ed6b8ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 51 deletions

View File

@ -6,11 +6,11 @@ import (
) )
type Helper struct { type Helper struct {
Logger logger Logger
} }
func NewHelper(log Logger) *Helper { func NewHelper(logger Logger) *Helper {
return &Helper{Logger: log} return &Helper{logger: logger}
} }
// Extract always returns valid Helper with logger from context or with DefaultLogger as fallback. // Extract always returns valid Helper with logger from context or with DefaultLogger as fallback.
@ -24,96 +24,104 @@ func Extract(ctx context.Context) *Helper {
return NewHelper(DefaultLogger) return NewHelper(DefaultLogger)
} }
func (h *Helper) Log(level Level, args ...interface{}) {
h.logger.Log(level, args...)
}
func (h *Helper) Logf(level Level, template string, args ...interface{}) {
h.logger.Logf(level, template, args...)
}
func (h *Helper) Info(args ...interface{}) { func (h *Helper) Info(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(InfoLevel) { if !h.logger.Options().Level.Enabled(InfoLevel) {
return return
} }
h.Log(InfoLevel, args...) h.logger.Log(InfoLevel, args...)
} }
func (h *Helper) Infof(template string, args ...interface{}) { func (h *Helper) Infof(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(InfoLevel) { if !h.logger.Options().Level.Enabled(InfoLevel) {
return return
} }
h.Logf(InfoLevel, template, args...) h.logger.Logf(InfoLevel, template, args...)
} }
func (h *Helper) Trace(args ...interface{}) { func (h *Helper) Trace(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(TraceLevel) { if !h.logger.Options().Level.Enabled(TraceLevel) {
return return
} }
h.Log(TraceLevel, args...) h.logger.Log(TraceLevel, args...)
} }
func (h *Helper) Tracef(template string, args ...interface{}) { func (h *Helper) Tracef(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(TraceLevel) { if !h.logger.Options().Level.Enabled(TraceLevel) {
return return
} }
h.Logf(TraceLevel, template, args...) h.logger.Logf(TraceLevel, template, args...)
} }
func (h *Helper) Debug(args ...interface{}) { func (h *Helper) Debug(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(DebugLevel) { if !h.logger.Options().Level.Enabled(DebugLevel) {
return return
} }
h.Log(DebugLevel, args...) h.logger.Log(DebugLevel, args...)
} }
func (h *Helper) Debugf(template string, args ...interface{}) { func (h *Helper) Debugf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(DebugLevel) { if !h.logger.Options().Level.Enabled(DebugLevel) {
return return
} }
h.Logf(DebugLevel, template, args...) h.logger.Logf(DebugLevel, template, args...)
} }
func (h *Helper) Warn(args ...interface{}) { func (h *Helper) Warn(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(WarnLevel) { if !h.logger.Options().Level.Enabled(WarnLevel) {
return return
} }
h.Log(WarnLevel, args...) h.logger.Log(WarnLevel, args...)
} }
func (h *Helper) Warnf(template string, args ...interface{}) { func (h *Helper) Warnf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(WarnLevel) { if !h.logger.Options().Level.Enabled(WarnLevel) {
return return
} }
h.Logf(WarnLevel, template, args...) h.logger.Logf(WarnLevel, template, args...)
} }
func (h *Helper) Error(args ...interface{}) { func (h *Helper) Error(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(ErrorLevel) { if !h.logger.Options().Level.Enabled(ErrorLevel) {
return return
} }
h.Log(ErrorLevel, args...) h.logger.Log(ErrorLevel, args...)
} }
func (h *Helper) Errorf(template string, args ...interface{}) { func (h *Helper) Errorf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(ErrorLevel) { if !h.logger.Options().Level.Enabled(ErrorLevel) {
return return
} }
h.Logf(ErrorLevel, template, args...) h.logger.Logf(ErrorLevel, template, args...)
} }
func (h *Helper) Fatal(args ...interface{}) { func (h *Helper) Fatal(args ...interface{}) {
if !h.Logger.Options().Level.Enabled(FatalLevel) { if !h.logger.Options().Level.Enabled(FatalLevel) {
return return
} }
h.Log(FatalLevel, args...) h.logger.Log(FatalLevel, args...)
os.Exit(1) os.Exit(1)
} }
func (h *Helper) Fatalf(template string, args ...interface{}) { func (h *Helper) Fatalf(template string, args ...interface{}) {
if !h.Logger.Options().Level.Enabled(FatalLevel) { if !h.logger.Options().Level.Enabled(FatalLevel) {
return return
} }
h.Logf(FatalLevel, template, args...) h.logger.Logf(FatalLevel, template, args...)
os.Exit(1) os.Exit(1)
} }
func (h *Helper) WithError(err error) *Helper { func (h *Helper) WithError(err error) *Helper {
return &Helper{Logger: h.Logger.Fields(map[string]interface{}{"error": err})} return &Helper{logger: h.logger.Fields(map[string]interface{}{"error": err})}
} }
func (h *Helper) WithFields(fields map[string]interface{}) *Helper { func (h *Helper) WithFields(fields map[string]interface{}) *Helper {
return &Helper{Logger: h.Logger.Fields(fields)} return &Helper{logger: h.logger.Fields(fields)}
} }

View File

@ -6,7 +6,8 @@ import (
) )
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
l := NewLogger(WithLevel(TraceLevel)) l := NewLogger(WithLevel(TraceLevel), WithCallerSkipCount(2))
h1 := NewHelper(l).WithFields(map[string]interface{}{"key1": "val1"}) h1 := NewHelper(l).WithFields(map[string]interface{}{"key1": "val1"})
h1.Log(TraceLevel, "simple log before trace_msg1") h1.Log(TraceLevel, "simple log before trace_msg1")
h1.Trace("trace_msg1") h1.Trace("trace_msg1")
@ -19,11 +20,12 @@ func TestLogger(t *testing.T) {
h2.Logf(TraceLevel, "formatted log after trace_msg%s", "2") h2.Logf(TraceLevel, "formatted log after trace_msg%s", "2")
h2.Warn("warn_msg2") h2.Warn("warn_msg2")
l = NewLogger(WithLevel(TraceLevel), WithCallerSkipCount(1))
l.Fields(map[string]interface{}{"key3": "val4"}).Log(InfoLevel, "test_msg") l.Fields(map[string]interface{}{"key3": "val4"}).Log(InfoLevel, "test_msg")
} }
func TestExtract(t *testing.T) { func TestExtract(t *testing.T) {
l := NewLogger(WithLevel(TraceLevel)).Fields(map[string]interface{}{"requestID": "req-1"}) l := NewLogger(WithLevel(TraceLevel), WithCallerSkipCount(2)).Fields(map[string]interface{}{"requestID": "req-1"})
ctx := NewContext(context.Background(), l) ctx := NewContext(context.Background(), l)

View File

@ -11,12 +11,6 @@ type Options struct {
logger.Options logger.Options
} }
type callerSkipKey struct{}
func WithCallerSkip(i int) logger.Option {
return logger.SetOption(callerSkipKey{}, i)
}
type configKey struct{} type configKey struct{}
// WithConfig pass zap.Config to logger // WithConfig pass zap.Config to logger

View File

@ -37,18 +37,13 @@ func (l *zaplog) Init(opts ...logger.Option) error {
} }
skip, ok := l.opts.Context.Value(callerSkipKey{}).(int)
if !ok || skip < 1 {
skip = 1
}
// Set log Level if not default // Set log Level if not default
zapConfig.Level = zap.NewAtomicLevel() zapConfig.Level = zap.NewAtomicLevel()
if l.opts.Level != logger.InfoLevel { if l.opts.Level != logger.InfoLevel {
zapConfig.Level.SetLevel(loggerToZapLevel(l.opts.Level)) zapConfig.Level.SetLevel(loggerToZapLevel(l.opts.Level))
} }
log, err := zapConfig.Build(zap.AddCallerSkip(skip)) log, err := zapConfig.Build(zap.AddCallerSkip(l.opts.CallerSkipCount))
if err != nil { if err != nil {
return err return err
} }
@ -166,10 +161,11 @@ func (l *zaplog) Options() logger.Options {
func NewLogger(opts ...logger.Option) (logger.Logger, error) { func NewLogger(opts ...logger.Option) (logger.Logger, error) {
// Default options // Default options
options := logger.Options{ options := logger.Options{
Level: logger.InfoLevel, Level: logger.InfoLevel,
Fields: make(map[string]interface{}), Fields: make(map[string]interface{}),
Out: os.Stderr, Out: os.Stderr,
Context: context.Background(), Context: context.Background(),
CallerSkipCount: 2,
} }
l := &zaplog{opts: options} l := &zaplog{opts: options}

View File

@ -20,7 +20,8 @@ func TestName(t *testing.T) {
} }
func TestLogf(t *testing.T) { func TestLogf(t *testing.T) {
l, err := NewLogger() // skip is 2, because we call logger through logger package
l, err := NewLogger(logger.WithCallerSkipCount(2))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -30,7 +31,8 @@ func TestLogf(t *testing.T) {
} }
func TestSetLevel(t *testing.T) { func TestSetLevel(t *testing.T) {
l, err := NewLogger() // skip is 1, because we call logger directly
l, err := NewLogger(logger.WithCallerSkipCount(1))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -53,10 +53,14 @@ func (l *zeroLogger) Init(opts ...logger.Option) error {
l.opts.Mode = Production l.opts.Mode = Production
} }
skip := 4
if l.opts.CallerSkipCount > 0 {
skip = l.opts.CallerSkipCount
}
// RESET // RESET
zerolog.TimeFieldFormat = time.RFC3339 zerolog.TimeFieldFormat = time.RFC3339
zerolog.ErrorStackMarshaler = nil zerolog.ErrorStackMarshaler = nil
zerolog.CallerSkipFrameCount = 4 zerolog.CallerSkipFrameCount = skip
switch l.opts.Mode { switch l.opts.Mode {
case Development: case Development:
@ -94,7 +98,7 @@ func (l *zeroLogger) Init(opts ...logger.Option) error {
if l.opts.ReportCaller { if l.opts.ReportCaller {
l.zLog = l.zLog.With().Caller().Logger() l.zLog = l.zLog.With().Caller().Logger()
} }
// Adding hooks if exist // Adding hooks if exist
for _, hook := range l.opts.Hooks { for _, hook := range l.opts.Hooks {
l.zLog = l.zLog.Hook(hook) l.zLog = l.zLog.Hook(hook)