-
Notifications
You must be signed in to change notification settings - Fork 557
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
Comments
Yes, we could add that as optional features. Those are costly operations which IMHO don't add much value. |
@ccll You can add this feature to your logger with a custom hook by using If you want advice on how to implement it, I can help. |
@Ullaakut Thanks for the advice, I'll definitely try that. |
@Ullaakut please do post the snip with |
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. |
it doesn't work, it prints the like where |
My bad, indeed it shouldn't have been |
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:
zerolog should support this too, may be in an optional context field like
Timestamp()
?The text was updated successfully, but these errors were encountered: