File: json.go

package info (click to toggle)
golang-github-bep-logg 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: makefile: 9
file content (30 lines) | stat: -rw-r--r-- 643 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Package json implements a JSON handler.
package json

import (
	"encoding/json"
	"io"

	"github.com/bep/logg"
)

type Handler struct {
	w io.Writer
}

// New Handler implementation for JSON logging.
// Eeach log Entry is written as a single JSON object, no more than one write to w.
// The writer w should be safe for concurrent use by multiple
// goroutines if the returned Handler will be used concurrently.
func New(w io.Writer) *Handler {
	return &Handler{
		w,
	}
}

// HandleLog implements logg.Handler.
func (h *Handler) HandleLog(e *logg.Entry) error {
	enc := json.NewEncoder(h.w)
	enc.SetEscapeHTML(false)
	return enc.Encode(e)
}