File: stream-handler.go

package info (click to toggle)
golang-github-anacrolix-log 0.14.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 180 kB
  • sloc: makefile: 2
file content (32 lines) | stat: -rw-r--r-- 473 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
31
32
package log

import (
	"fmt"
	"io"
)

type StreamHandler struct {
	W   io.Writer
	Fmt ByteFormatter
}

func (me StreamHandler) Handle(r Record) {
	r.Msg = r.Skip(1)
	me.W.Write(me.Fmt(r))
}

type ByteFormatter func(Record) []byte

func LineFormatter(msg Record) []byte {
	ret := []byte(fmt.Sprintf(
		"[%s %s] %s %s",
		DefaultTimeFormatter(),
		msg.Level.LogString(),
		msg.Text(),
		msg.Names,
	))
	if ret[len(ret)-1] != '\n' {
		ret = append(ret, '\n')
	}
	return ret
}