Search code examples
google-app-enginelogginggoline-numbers

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


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)

Solution

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