summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/logr/target/writer.go
blob: 2250da5138862c400c3614802650e039fcb79252 (plain) (blame)
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
31
32
33
34
35
36
37
38
39
40
package target

import (
	"io"
	"io/ioutil"

	"github.com/mattermost/logr"
)

// Writer outputs log records to any `io.Writer`.
type Writer struct {
	logr.Basic
	out io.Writer
}

// NewWriterTarget creates a target capable of outputting log records to an io.Writer.
func NewWriterTarget(filter logr.Filter, formatter logr.Formatter, out io.Writer, maxQueue int) *Writer {
	if out == nil {
		out = ioutil.Discard
	}
	w := &Writer{out: out}
	w.Basic.Start(w, w, filter, formatter, maxQueue)
	return w
}

// Write converts the log record to bytes, via the Formatter,
// and outputs to the io.Writer.
func (w *Writer) Write(rec *logr.LogRec) error {
	_, stacktrace := w.IsLevelEnabled(rec.Level())

	buf := rec.Logger().Logr().BorrowBuffer()
	defer rec.Logger().Logr().ReleaseBuffer(buf)

	buf, err := w.Formatter().Format(rec, stacktrace, buf)
	if err != nil {
		return err
	}
	_, err = w.out.Write(buf.Bytes())
	return err
}