Skip to content

Commit

Permalink
Expose the Err helper from the global logger
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Nov 19, 2019
1 parent 54e95fe commit 5d9d766
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ func Hook(h zerolog.Hook) zerolog.Logger {
return Logger.Hook(h)
}

// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func Err(err error) *zerolog.Event {
return Logger.Err(err)
}

// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
Expand Down
11 changes: 11 additions & 0 deletions log/log_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ func ExampleLog() {
// Output: {"time":1199811905,"message":"hello world"}
}

// Example of a conditional level based on the presence of an error.
func ExampleErr() {
setup()
err := errors.New("some error")
log.Err(err).Msg("hello world")
log.Err(nil).Msg("hello world")

// Output: {"level":"error","error":"some error","time":1199811905,"message":"hello world"}
// {"level":"info","time":1199811905,"message":"hello world"}
}

// Example of a log at a particular "level" (in this case, "trace")
func ExampleTrace() {
setup()
Expand Down

0 comments on commit 5d9d766

Please sign in to comment.