8000 [log][generator][go] Generate file line number for logging to avoid calling runtime · Issue #43 · dyweb/gommon · GitHub
[go: up one dir, main page]

Skip to content

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

Open

Description

@at15

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

  1. added this to the 0.0.3 milestone on Feb 4, 2018
  2. self-assigned this
    on Feb 4, 2018
  3. changed the title [-][log][generator][go] Compile time file line number generation[/-] [+][log][generator][go] Compile time file line number for logging[/+] on Feb 4, 2018
  4. changed the title [-][log][generator][go] Compile time file line number for logging[/-] [+][log][generator][go] Generate file line number for logging to avoid calling runtime[/+] on Feb 4, 2018
  5. added a commit that references this issue on Feb 5, 2018
  6. at15 commented on Feb 5, 2018

    @at15
    MemberAuthor

    I should implement logger using runtime first, also clean up things like fields before dig into this ...

  7. modified the milestones: 0.0.3, future on Feb 5, 2018
  8. added a commit that references this issue on Feb 5, 2018
  9. halink0803 commented on Feb 26, 2018

    @halink0803

    @at15 I saw this featured was merged, can you provide a short instruction on how to use this? Thank you.

  10. at15 commented on Feb 26, 2018

    @at15
    MemberAuthor

    @halink0803 the merged one is using runtime library, not generating line number at compile time like C (so it still has the overhead like most other logging libraries), an example can be found here https://github.com/dyweb/gommon/blob/master/log/_examples/simple/main.go

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    0