docs: add an example for Lshortfile-like implementation of CallerMarshalFunc (#423)

Signed-off-by: Igor Beliakov <demtis.register@gmail.com>
This commit is contained in:
Igor Beliakov 2022-03-12 17:33:09 +01:00 committed by GitHub
parent 263b0bde36
commit e9344a8c50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 1 deletions

View File

@ -399,6 +399,8 @@ log.Logger = log.With().Str("foo", "bar").Logger()
### Add file and line number to log ### Add file and line number to log
Equivalent of `Llongfile`:
```go ```go
log.Logger = log.With().Caller().Logger() log.Logger = log.With().Caller().Logger()
log.Info().Msg("hello world") log.Info().Msg("hello world")
@ -406,6 +408,25 @@ log.Info().Msg("hello world")
// Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"} // Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"}
``` ```
Equivalent of `Lshortfile`:
```go
zerolog.CallerMarshalFunc = func(file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
return file + ":" + strconv.Itoa(line)
}
log.Logger = log.With().Caller().Logger()
log.Info().Msg("hello world")
// Output: {"level": "info", "message": "hello world", "caller": "some_file:21"}
```
### Thread-safe, lock-free, non-blocking writer ### Thread-safe, lock-free, non-blocking writer