在GAE上获取日志点是否可能?

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

Is it possible to get the log point on GAE?

问题

我想知道在记录错误时是否可以记录行号和列号。GAE日志库似乎只存储错误消息,但我认为获取错误/日志点的确切位置(像大多数日志库一样)会非常有价值。

英文:

I'm wondering if it's possible to log the line/column number when i log an error. The GAE logging lib seems to store only the error message but I think it would be quite valuable to get the exact location of the error/log point like most logging libraries do.

答案1

得分: 1

你要求的绝对是可能的,你只需要非常明确地表达出来!我建议阅读Andrew Gerrand在http://blog.golang.org/error-handling-and-go上的优秀文章,了解有关这个问题的一般信息,包括与App Engine相关的注意事项。

该文章并没有特别涉及堆栈跟踪,但是你当然可以通过http://golang.org/pkg/runtime/#Stack自己实现堆栈跟踪。

让我们坦诚地承认,Go更多地是一个系统编程语言,而不是一个面向应用程序的语言,因此它并没有像其他面向应用程序的语言(如Java、Python、PHP等)那样为你提供隐式的、自动的支持。但是,Go确实为你提供了所有你需要的工具,以便你根据自己的需求来做尽可能少或尽可能多的“支持基础设施”来支持你自己的应用程序!-)

[*] 嘿,你甚至不能像其他语言那样自动默认地传播异常,你需要自己负责捕获和处理错误,哦,多么可怕...!-)

英文:

What you request is definitely possible, you just need to be very explicit about it! I recommend Andrew Gerrand's excellent article at http://blog.golang.org/error-handling-and-go for general information on the issue, including notes specific to App Engine.

That article does not specifically address stack traces, but of course you could do those yourself, via http://golang.org/pkg/runtime/#Stack .

Let's be honest and admit that Go -- being by design more of a system-programming language than of an application-focused one -- doesn't do quite as much implicit, automatic hand-holding for you, as more app-oriented languages such as the other App Engine ones -- Java, Python, PHP... [*] but, Go does give you all the tools you need to do just as little, or as much, "supporting infrastructure", as you desire to have in support of your own apps!-)

[*] hey, you don't even get automatically by-default propagating exceptions, as you do for other languages -- nay, you're responsible for catching and dealing with errors yourself, oh the horror...!-)

答案2

得分: 0

你可以使用runtime包来追踪调用者(我假设这基本上是你想要的)。这与错误处理无关,也没有与GAE特定问题有关。

// depth参数指定了源代码行在日志消息中的上方有多少个堆栈帧。
func traceCaller(depth int) (string, int) {
	_, file, line, ok := runtime.Caller(2 + depth)
	if !ok {
		file = "???"
		line = 1
	} else {
		slash := strings.LastIndex(file, "/")
		if slash >= 0 {
			file = file[slash+1:]
		}
	}
	return file, line
}
英文:

You can use the runtime package to trace the caller(I assume that's basically what you want). There is no GAE specific issue and it has nothing to do with the error handling.

   // The depth specifies how many stack frames above 
   // lives the source line to be identified in the log message.
    func traceCaller(depth int)(string, int){
    _, file, line, ok := runtime.Caller(2 + depth)
    	if !ok {
    		file = "???"
    		line = 1
    	} else {
    		slash := strings.LastIndex(file, "/")
    		if slash >= 0 {
    			file = file[slash+1:]
    		}
    	}
        return file, line
}

huangapple
  • 本文由 发表于 2015年3月22日 04:42:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/29187829.html
匿名

发表评论

匿名网友

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

确定