Add support for zerolog as an output for stdlib logger
This commit is contained in:
parent
bf4b44614c
commit
6bcd15ecf0
15
README.md
15
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`.
|
||||
|
|
14
event.go
14
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 != "" {
|
||||
|
|
12
log.go
12
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 {
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue