How to automatically log functions / line numbers in App Engine Go logs?

huangapple go评论97阅读模式
英文:

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...)
}

huangapple
  • 本文由 发表于 2014年7月3日 02:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/24538207.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定