Skip to content

[log][generator][go] Generate file line number for logging to avoid calling runtime #43

Open
@at15

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

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

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

Ref

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions