Implement handlers to normalise how logging is handled

This commit is contained in:
Arturo Filastò
2018-02-21 17:06:30 +02:00
parent e7ee54436e
commit 3cdb927eb0
14 changed files with 176 additions and 30 deletions
+33
View File
@@ -0,0 +1,33 @@
package batch
import (
j "encoding/json"
"io"
"os"
"sync"
"github.com/apex/log"
)
// Default handler outputting to stderr.
var Default = New(os.Stderr)
// Handler implementation.
type Handler struct {
*j.Encoder
mu sync.Mutex
}
// New handler.
func New(w io.Writer) *Handler {
return &Handler{
Encoder: j.NewEncoder(w),
}
}
// HandleLog implements log.Handler.
func (h *Handler) HandleLog(e *log.Entry) error {
h.mu.Lock()
defer h.mu.Unlock()
return h.Encoder.Encode(e)
}