From 6bcd15ecf028bbb3e5cc7225f5fd110b06cf58b0 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Thu, 1 Jun 2017 23:17:28 -0700 Subject: [PATCH] Add support for zerolog as an output for stdlib logger --- README.md | 15 +++++++++++++++ event.go | 14 +++++++------- log.go | 12 ++++++++++++ log_example_test.go | 14 ++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a59fb5e..4e18417 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,21 @@ log.Ctx(ctx).Info().Msg("hello world") // Output: {"component":"module","level":"info","message":"hello world"} ``` +### Set as standard logger output + +```go +log := zerolog.New(os.Stdout).With(). + Str("foo", "bar"). + Logger() + +stdlog.SetFlags(0) +stdlog.SetOutput(log) + +stdlog.Print("hello world") + +// Output: {"foo":"bar","message":"hello world"} +``` + ### Integration with `net/http` The `github.com/rs/zerolog/hlog` package provides some helpers to integrate zerolog with `http.Handler`. diff --git a/event.go b/event.go index d15dda7..9bb3f56 100644 --- a/event.go +++ b/event.go @@ -38,12 +38,12 @@ func newEvent(w LevelWriter, level Level, enabled bool) *Event { return e } -func (e *Event) write() (n int, err error) { +func (e *Event) write() (err error) { if !e.enabled { - return 0, nil + return nil } e.buf = append(e.buf, '}', '\n') - n, err = e.w.WriteLevel(e.level, e.buf) + _, err = e.w.WriteLevel(e.level, e.buf) eventPool.Put(e) return } @@ -58,9 +58,9 @@ func (e *Event) Enabled() bool { // // NOTICE: once this methid is called, the *Event should be disposed. // Calling Msg twice can have unexpected result. -func (e *Event) Msg(msg string) (n int, err error) { +func (e *Event) Msg(msg string) error { if !e.enabled { - return 0, nil + return nil } if msg != "" { e.buf = appendString(e.buf, MessageFieldName, msg) @@ -75,9 +75,9 @@ func (e *Event) Msg(msg string) (n int, err error) { // // NOTICE: once this methid is called, the *Event should be disposed. // Calling Msg twice can have unexpected result. -func (e *Event) Msgf(format string, v ...interface{}) (n int, err error) { +func (e *Event) Msgf(format string, v ...interface{}) error { if !e.enabled { - return 0, nil + return nil } msg := fmt.Sprintf(format, v...) if msg != "" { diff --git a/log.go b/log.go index ab50cec..a74b310 100644 --- a/log.go +++ b/log.go @@ -249,6 +249,18 @@ func (l Logger) Log() *Event { return l.newEvent(ErrorLevel, false, nil) } +// Write implements the io.Writer interface. This is useful to set as a writer +// for the standard library log. +func (l Logger) Write(p []byte) (n int, err error) { + n = len(p) + if n > 0 && p[n-1] == '\n' { + // Trim CR added by stdlog. + p = p[0 : n-1] + } + err = l.Log().Msg(string(p)) + return +} + func (l Logger) newEvent(level Level, addLevelField bool, done func(string)) *Event { enabled := l.should(level) if !enabled { diff --git a/log_example_test.go b/log_example_test.go index 863f3c1..a53d448 100644 --- a/log_example_test.go +++ b/log_example_test.go @@ -2,6 +2,7 @@ package zerolog_test import ( "errors" + stdlog "log" "os" "time" @@ -90,6 +91,19 @@ func ExampleLogger_Error() { // Output: {"level":"error","error":"some error","message":"error doing something"} } +func ExampleLogger_Write() { + log := zerolog.New(os.Stdout).With(). + Str("foo", "bar"). + Logger() + + stdlog.SetFlags(0) + stdlog.SetOutput(log) + + stdlog.Print("hello world") + + // Output: {"foo":"bar","message":"hello world"} +} + func ExampleLogger_Log() { log := zerolog.New(os.Stdout)