| 1 |
// Package json implements a JSON handler. |
| 2 |
package json |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/json" |
| 6 |
"io" |
| 7 |
|
| 8 |
"github.com/bep/logg" |
| 9 |
) |
| 10 |
|
| 11 |
type Handler struct { |
| 12 |
w io.Writer |
| 13 |
} |
| 14 |
|
| 15 |
// New Handler implementation for JSON logging. |
| 16 |
// Eeach log Entry is written as a single JSON object, no more than one write to w. |
| 17 |
// The writer w should be safe for concurrent use by multiple |
| 18 |
// goroutines if the returned Handler will be used concurrently. |
| 19 |
func New(w io.Writer) *Handler { |
| 20 |
return &Handler{ |
| 21 |
w, |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
// HandleLog implements logg.Handler. |
| 26 |
func (h *Handler) HandleLog(e *logg.Entry) error { |
| 27 |
enc := json.NewEncoder(h.w) |
| 28 |
enc.SetEscapeHTML(false) |
| 29 |
return enc.Encode(e) |
| 30 |
} |