Remove error returned by Msg and Msgf

When error happen, there is nothing the caller can really do and it is
not practicle to handle logging errors. Print an error on the standard
error instead.
This commit is contained in:
Olivier Poitrey 2017-06-06 10:08:58 -07:00
parent 95ecd5ad27
commit 46cd35d1f2
2 changed files with 12 additions and 7 deletions

View File

@ -3,6 +3,7 @@ package zerolog
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"sync" "sync"
"time" "time"
) )
@ -58,9 +59,9 @@ func (e *Event) Enabled() bool {
// //
// NOTICE: once this method is called, the *Event should be disposed. // NOTICE: once this method is called, the *Event should be disposed.
// Calling Msg twice can have unexpected result. // Calling Msg twice can have unexpected result.
func (e *Event) Msg(msg string) error { func (e *Event) Msg(msg string) {
if !e.enabled { if !e.enabled {
return nil return
} }
if msg != "" { if msg != "" {
e.buf = appendString(e.buf, MessageFieldName, msg) e.buf = appendString(e.buf, MessageFieldName, msg)
@ -68,16 +69,18 @@ func (e *Event) Msg(msg string) error {
if e.done != nil { if e.done != nil {
defer e.done(msg) defer e.done(msg)
} }
return e.write() if err := e.write(); err != nil {
fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v", err)
}
} }
// Msgf sends the event with formated msg added as the message field if not empty. // Msgf sends the event with formated msg added as the message field if not empty.
// //
// NOTICE: once this methid is called, the *Event should be disposed. // NOTICE: once this methid is called, the *Event should be disposed.
// Calling Msg twice can have unexpected result. // Calling Msg twice can have unexpected result.
func (e *Event) Msgf(format string, v ...interface{}) error { func (e *Event) Msgf(format string, v ...interface{}) {
if !e.enabled { if !e.enabled {
return nil return
} }
msg := fmt.Sprintf(format, v...) msg := fmt.Sprintf(format, v...)
if msg != "" { if msg != "" {
@ -86,7 +89,9 @@ func (e *Event) Msgf(format string, v ...interface{}) error {
if e.done != nil { if e.done != nil {
defer e.done(msg) defer e.done(msg)
} }
return e.write() if err := e.write(); err != nil {
fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v", err)
}
} }
// Dict adds the field key with a dict to the event context. // Dict adds the field key with a dict to the event context.

2
log.go
View File

@ -264,7 +264,7 @@ func (l Logger) Write(p []byte) (n int, err error) {
// Trim CR added by stdlog. // Trim CR added by stdlog.
p = p[0 : n-1] p = p[0 : n-1]
} }
err = l.Log().Msg(string(p)) l.Log().Msg(string(p))
return return
} }