generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.go
153 lines (127 loc) · 3.52 KB
/
logger.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package log
import (
"bufio"
"fmt"
"io"
"os"
"runtime"
"time"
"golang.org/x/exp/maps"
)
var _ Interface = (*Logger)(nil)
const scopeKey = "scope"
type Entry struct {
Time time.Time `json:"-"`
Level Level `json:"level"`
Attributes map[string]string `json:"attributes,omitempty"`
Message string `json:"message"`
Error error `json:"-"`
}
// Logger is the concrete logger.
type Logger struct {
level Level
attributes map[string]string
sink Sink
}
// New returns a new logger.
func New(level Level, sink Sink) *Logger {
return &Logger{
level: level,
attributes: map[string]string{},
sink: sink,
}
}
func (l Logger) Scope(scope string) *Logger {
return l.Attrs(map[string]string{scopeKey: scope})
}
// Attrs creates a new logger with the given attributes.
func (l Logger) Attrs(attributes map[string]string) *Logger {
attr := make(map[string]string, len(l.attributes)+len(attributes))
maps.Copy(attr, l.attributes)
maps.Copy(attr, attributes)
l.attributes = attr
return &l
}
func (l Logger) AddSink(sink Sink) *Logger {
l.sink = Tee(l.sink, sink)
return &l
}
func (l Logger) Level(level Level) *Logger {
l.level = level
return &l
}
func (l Logger) GetLevel() Level {
return l.level
}
func (l *Logger) Log(entry Entry) {
if entry.Level < l.level {
return
}
if entry.Time.IsZero() {
entry.Time = time.Now()
}
entry.Attributes = l.attributes
if err := l.sink.Log(entry); err != nil {
fmt.Fprintf(os.Stderr, "ftl:log: failed to log entry: %v", err)
}
}
func (l *Logger) Logf(level Level, format string, args ...interface{}) {
l.Log(Entry{Level: level, Message: fmt.Sprintf(format, args...)})
}
func (l *Logger) Tracef(format string, args ...interface{}) {
l.Log(Entry{Level: Trace, Message: fmt.Sprintf(format, args...)})
}
func (l *Logger) Debugf(format string, args ...interface{}) {
l.Log(Entry{Level: Debug, Message: fmt.Sprintf(format, args...)})
}
func (l *Logger) Infof(format string, args ...interface{}) {
l.Log(Entry{Level: Info, Message: fmt.Sprintf(format, args...)})
}
func (l *Logger) Warnf(format string, args ...interface{}) {
l.Log(Entry{Level: Warn, Message: fmt.Sprintf(format, args...)})
}
func (l *Logger) Errorf(err error, format string, args ...interface{}) {
if err == nil {
return
}
l.Log(Entry{Level: Error, Message: fmt.Sprintf(format, args...) + ": " + err.Error(), Error: err})
}
// WriterAt returns a writer that logs each line at the given level.
func (l *Logger) WriterAt(level Level) *io.PipeWriter {
// Based on MIT licensed Logrus https://github.com/sirupsen/logrus/blob/bdc0db8ead3853c56b7cd1ac2ba4e11b47d7da6b/writer.go#L27
reader, writer := io.Pipe()
var printFunc func(format string, args ...interface{})
switch level {
case Trace:
printFunc = l.Tracef
case Debug:
printFunc = l.Debugf
case Info:
printFunc = l.Infof
case Warn:
printFunc = l.Warnf
case Error:
printFunc = func(format string, args ...interface{}) {
l.Errorf(nil, format, args...)
}
default:
panic(level)
}
go l.writerScanner(reader, printFunc)
runtime.SetFinalizer(writer, writerFinalizer)
return writer
}
func (l *Logger) writerScanner(reader *io.PipeReader, printFunc func(format string, args ...interface{})) {
scanner := bufio.NewScanner(reader)
scanner.Buffer(nil, 1024*1024) // 1MB buffer
for scanner.Scan() {
printFunc("%s", scanner.Text())
}
if err := scanner.Err(); err != nil {
l.Errorf(err, "Error while reading from Writer")
}
reader.Close()
}
func writerFinalizer(writer *io.PipeWriter) {
writer.Close()
}