Open
Description
Existing go logging library all use runtime
API to get caller when log file line number, which is expensive (we used to that as well #7). It's possible we parse files and add file line number before compile
i.e. standard log package https://golang.org/src/log/log.go?s=5257:5311#L139
if l.flag&(Lshortfile|Llongfile) != 0 {
// Release lock while getting caller info - it's expensive.
l.mu.Unlock()
var ok bool
_, file, line, ok = runtime.Caller(calldepth)
if !ok {
file = "???"
line = 0
}
l.mu.Lock()
}
- logrus does not have it Log filename and line number sirupsen/logrus#63, promethus guys hack around to do it https://github.com/prometheus/common/blob/master/log/log.go#L234-L244
There are essentially two ways (I can think of ...)
In place update special symbol like __FILE__
use a __FILE__
syntax like in script language (python __file__
) or C Marco, and using things like go generate with build flag, user need to explicit specify the special for all the logging call
- go does not (plan to) have it spec: add __FILE__ and __LINE__ macro golang/go#12876
log.DebugFLine("__FILE__", "num %d", len(bugs))
Generate new file with different build flag
- replace logging method with new function and location information
code written by user
// +build !gommon_log_line
package foo
var log = logutil.NewPackageLogger()
func bar() {
log.DebugFLine("github.com/at15/test/foo.go:10:32", "number of bugs %d", len(bugs))
}
code compiled by go tool
// Code generated by gommon from foo.go DO NOT EDIT.
// +build gommon_log_line
package foo
var log = logutil.NewPackageLogger()
func bar() {
log.DebugFLine("github.com/at15/test/foo.go:10:32", "number of bugs %d", len(bugs))
}
Activity