英文:
How to automatically log functions / line numbers in App Engine Go logs?
问题
我已经编写了一些 GAE Golang 应用程序,对于我目前的日志记录方法感到不满意。我似乎找不到一种简单的方法来记录行号,甚至无法确定在 App Engine 中创建日志的函数。是否有一种自动创建这种有意义的日志的方式?目前,我只能手动给我的错误编号,例如:
c.Debugf("Backend - Upkeep error 5 - %v", err)
英文:
I have written a number of GAE Golang applications and I'm rather unsatisfied with my current logging approach. I can't seem to find an easy way to log line numbers or even which functions a log was created in App Engine. Is there some automatic way of creating such meaningful logs? At the moment I am resulting to just manually numbering my errors, like:
c.Debugf("Backend - Upkeep error 5 - %v", err)
答案1
得分: 4
你需要创建自己的函数并使用runtime.Caller。
工作示例:http://play.golang.org/p/CQghRzJ3x_
func debug(format string, args ...interface{}) {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
log.Printf(file+":"+strconv.Itoa(line)+" - "+format, args...)
}
英文:
You need to create your own function and use runtime.Caller
Working Example: http://play.golang.org/p/CQghRzJ3x_
func debug(format string, args ...interface{}) {
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
}
log.Printf(file+":"+strconv.Itoa(line)+" - "+format, args...)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论