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:
parent
263b0bde36
commit
e9344a8c50
21
README.md
21
README.md
|
@ -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
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue