Please answer these questions before submitting your issue. Thanks!
What version of Go and beego are you using (bee version)?
go version go1.11 darwin/amd64
beego version v1.11.0
What operating system and processor architecture are you using (go env)?
operating system: mac os
GOARCH="amd64"
What did you do?
我想要在Log中打印文件名和行号,于是调用了这行代码:logs.EnableFuncCallDepth(true)
I want to print the file name and line number in the Log, so I call this line of code:
logs.EnableFuncCallDepth(true)
What did you expect to see?
输出正确的调用者文件名和行号
Output the correct caller file name and line number
What did you see instead?
输出的是beego源码的文件名和行数,这里是输出结果:[D] [log.go:634] test
The output is the file name and number of lines of the beego source, here is the output: [D] [log.go:634] test
通过查看源码和文档,我发现beego的loggerFuncCallDepth默认为2,我认为这是不合理的。从用户调用到runtime.Caller()被调用,经过了以下几层方法:用户调用 beego.Debug() -> beego/log.go Debug() -> logs/log.go Debug() -> BeeLogger.Debug() -> BeeLogger.writeMsg() -> runtime.Caller()
由调用的层级关系推断,loggerFuncCallDepth应该默认设置为4.当我加上这行代码后,得到了符合预期的输出:logs.SetLogFuncCallDepth(4)
By looking at the source code and documentation, I found that beego's loggerFuncCallDepth defaults to 2, which I think is unreasonable. From the user call to runtime.Caller() is called, the following layers are passed: the user calls beego.Debug() -> beego/log.go Debug() -> logs/log.go Debug() -> BeeLogger. Debug() -> BeeLogger.writeMsg() -> runtime.Caller()
Inferred from the hierarchical relationship of the call, loggerFuncCallDepth should be set to 4. By default, when I add this line of code, I get the expected output: logs.SetLogFuncCallDepth(4)
补充:问题出现的项目并不是beego项目,我仅仅是使用了beego的Log模块。在查看源码时,发现在beego/config.go的init方法中,调用了parseConfig(appConfigPath)方法,它会使最终的BeeLogger的loggerFuncCallDepth被设置为4,但是如果我只使用beego的Log模块,该方法就不会被调用,导致BeeLogger的loggerFuncCallDepth没有被设置为正确的值 (4)
Added: This problem project is not a beego project, I just use the log module of beego. When viewing the source code, I found that in the init method of beego/config.go, the parseConfig(appConfigPath) method is called, which will set the final BeeLogger's loggerFuncCallDepth to 4, but if I only use beego's Log module, this method Will not be called, causing BeeLogger's loggerFuncCallDepth not to be set to the correct value (4)
你应该可以手动调用SetLogFuncCallDepth() 方法解决:
// SetLogFuncCallDepth set log funcCallDepth
func SetLogFuncCallDepth(d int) {
beeLogger.loggerFuncCallDepth = d
}
你应该可以手动调用SetLogFuncCallDepth() 方法解决:
// SetLogFuncCallDepth set log funcCallDepth func SetLogFuncCallDepth(d int) { beeLogger.loggerFuncCallDepth = d }
是的,正如我上文所说,我通过调用SetLogFuncCallDepth方法解决了问题。但我认为,既然beego本身设计成模块化的,那么当我仅想使用日志模块的时候,它应该也会工作的符合预期才对。
你应该可以手动调用SetLogFuncCallDepth() 方法解决:
// SetLogFuncCallDepth set log funcCallDepth func SetLogFuncCallDepth(d int) { beeLogger.loggerFuncCallDepth = d }是的,正如我上文所说,我通过调用SetLogFuncCallDepth方法解决了问题。但我认为,既然beego本身设计成模块化的,那么当我仅想使用日志模块的时候,它应该也会工作的符合预期才对。
确实是模块化的,使用任何一个第三方包,我们都需要预先配置好,难道不是吗?就像使用redis驱动包,我们也要配置redis服务地址端口,这些是无法避免的,beeLogger.loggerFuncCallDepth的值只有使用者自己才知道设置为多少。
Most helpful comment
补充:问题出现的项目并不是beego项目,我仅仅是使用了beego的Log模块。在查看源码时,发现在beego/config.go的init方法中,调用了parseConfig(appConfigPath)方法,它会使最终的BeeLogger的loggerFuncCallDepth被设置为4,但是如果我只使用beego的Log模块,该方法就不会被调用,导致BeeLogger的loggerFuncCallDepth没有被设置为正确的值 (4)
Added: This problem project is not a beego project, I just use the log module of beego. When viewing the source code, I found that in the init method of beego/config.go, the parseConfig(appConfigPath) method is called, which will set the final BeeLogger's loggerFuncCallDepth to 4, but if I only use beego's Log module, this method Will not be called, causing BeeLogger's loggerFuncCallDepth not to be set to the correct value (4)