Skip to content

Commit

Permalink
docs: add an example for Lshortfile-like implementation of CallerMars…
Browse files Browse the repository at this point in the history
…halFunc (#423)

Signed-off-by: Igor Beliakov <demtis.register@gmail.com>
  • Loading branch information
weisdd committed Mar 12, 2022
1 parent 263b0bd commit e9344a8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,34 @@ log.Logger = log.With().Str("foo", "bar").Logger()

### Add file and line number to log

Equivalent of `Llongfile`:

```go
log.Logger = log.With().Caller().Logger()
log.Info().Msg("hello world")

// 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

Expand Down Expand Up @@ -560,7 +581,7 @@ func main() {
// Output (Line 1: Console; Line 2: Stdout)
// 12:36PM INF Hello World!
// {"level":"info","time":"2019-11-07T12:36:38+03:00","message":"Hello World!"}
```
```

## Global Settings

Expand Down

0 comments on commit e9344a8

Please sign in to comment.