Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an optional context field to capture the filename and line number #22

Closed
ccll opened this issue Nov 23, 2017 · 7 comments
Closed

Add an optional context field to capture the filename and line number #22

ccll opened this issue Nov 23, 2017 · 7 comments

Comments

@ccll
Copy link

ccll commented Nov 23, 2017

Often it's useful to capture the filename and line number in the log, to help finding the location where this log entry is generated.

zap have this feature:

... "caller":"hello/main.go:221" ...

zerolog should support this too, may be in an optional context field like Timestamp()?

@rs
Copy link
Owner

rs commented Nov 27, 2017

Yes, we could add that as optional features. Those are costly operations which IMHO don't add much value.

@Ullaakut
Copy link

Ullaakut commented Jan 19, 2018

@ccll You can add this feature to your logger with a custom hook by using runtime.Caller, it's pretty straightforward and I don't think it really makes sense to have it as a feature in ZeroLog itself as it's clearly making the logging slower.

If you want advice on how to implement it, I can help.

@ccll
Copy link
Author

ccll commented Jan 20, 2018

@Ullaakut Thanks for the advice, I'll definitely try that.
Closing now.

@OptimisticLock
Copy link

@Ullaakut please do post the snip with runtime.Caller if you would.

@Ullaakut
Copy link

Ullaakut commented May 16, 2022

Hi @OptimisticLock. Something like that should work.

type LineInfoHook struct{}

func (h LineInfoHook) Run(e *zerolog.Event, l zerolog.Level, msg string) {
	_, file, line, ok := runtime.Caller(0)
	if ok {
		e.Str("file", file)
		e.Int("line", line)
	}
}

or if you want it in a single field:

type LineInfoHook struct{}

func (h LineInfoHook) Run(e *zerolog.Event, l zerolog.Level, msg string) {
	_, file, line, ok := runtime.Caller(0)
	if ok {
		e.Str("line", fmt.Sprintf("%s:%d", file, line))
	}
}

See https://github.com/rs/zerolog/blob/master/log_example_test.go for examples with hooks.
See the runtime package docs for more info about the Caller func.

@adbenitez
Copy link

Hi @OptimisticLock. Something like that should work.

it doesn't work, it prints the like where runtime.Caller(0) is called inside the hook, not the line where the logging happened

@Ullaakut
Copy link

Ullaakut commented Mar 9, 2023

My bad, indeed it shouldn't have been 0. Anyway, since then, the Caller() method has been added to zerolog to achieve this. Once again though, keep in mind that IIRC, this slows down the logger a great deal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants