-
Notifications
You must be signed in to change notification settings - Fork 1
/
sink.go
64 lines (51 loc) · 1.44 KB
/
sink.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package logs
import (
"context"
"fmt"
"os"
)
type Sink interface {
FlowInto(pac *Packet) error
Close(ctx context.Context) error
}
func NewStandardSink() Sink {
return &standardSink{}
}
type standardSink struct {}
func (s *standardSink) FlowInto(pac *Packet) error {
buf := getBuffer()
defer putBuffer(buf)
buf.WriteString(fmt.Sprintf("[ \x1b[%dm%s\x1b[0m ]", pac.Lv.Color(), pac.Lv.String()))
buf.WriteString(fmt.Sprintf("[ \x1b[%dm%s\x1b[0m ]", 37, pac.Occurred.Format("2006-01-02 15:04:05.999999999 -0700 MST"))) // 36
buf.WriteString(fmt.Sprintf("[ %d ]", pac.Gid))
_, fn, file, line := pac.Caller()
buf.WriteString(fmt.Sprintf("[ %s ][ %s:%d ][ ", fn, file, line))
buf.WriteString(fmt.Sprintf(pac.Formatter, pac.Elements...))
buf.WriteByte(' ')
buf.WriteByte(']')
buf.WriteByte('\n')
var wErr error = nil
_, wErr = buf.WriteTo(os.Stdout)
return wErr
}
func (s *standardSink) Close(ctx context.Context) error {
return nil
}
func NewJsonSink() Sink {
return &jsonSink{}
}
type jsonSink struct {}
func (s *jsonSink) FlowInto(pac *Packet) error {
buf := getBuffer()
defer putBuffer(buf)
_, fn, file, line := pac.Caller()
buf.WriteString(
fmt.Sprintf(`{"level":"%s","fn":"%s","file":"%s","line":%d,"msg":"%s"}`,
pac.Lv.String(), fn, file, line, fmt.Sprintf(pac.Formatter, pac.Elements...)))
buf.WriteByte('\n')
_, wErr := buf.WriteTo(os.Stdout)
return wErr
}
func (s *jsonSink) Close(ctx context.Context) error {
return nil
}