blog-service icon indicating copy to clipboard operation
blog-service copied to clipboard

logger 库中 withCaller 错误

Open xuxusheng opened this issue 4 years ago • 2 comments

withCaller() 的调用现在放在 mian.setupLogger 函数里,导致不管怎么打印,日志里的 callers 字段的值都是指向 main.init 函数。

我自己挪到了 Logger.Output 函数里

image

xuxusheng avatar Dec 09 '20 08:12 xuxusheng

首先感谢楼主让我也注意到了这个问题.
除了楼主的做法, 我觉得也可以参考代码作者调用其他Logger方法的位置, 将 (*Logger).WithCaller() 放到 (*Logger).Infof() 等函数中, 不过需要在所有输出函数上都加上, 没有楼主简洁, 但效果是一样的.

func (l *Logger) Infof(ctx context.Context,format string, v ...interface{}) {
	l.WithContext(ctx).WithTrace().WithCaller(2).Output(LevelInfo, fmt.Sprintf(format, v...))
}

peakcrosser7 avatar Apr 20 '21 07:04 peakcrosser7

.WithCaller(2).

我用楼主的方式 caller 是 null 用你的方式可以得到正确输出。

shaojunda avatar Dec 03 '21 20:12 shaojunda