-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstream.go
140 lines (117 loc) · 3.51 KB
/
stream.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
package distillog
import (
"fmt"
"io"
"io/ioutil"
"os"
"sync"
"time"
)
// streamLogger implements the Logger interface and writes to the specified io.Writer ("stream").
// It mimics the stdlib logger including memory optimizations such as minimizing calls to fmt.Sprintf
// and using a shared buffer to format the message before writing it out.
type streamLogger struct {
stream io.WriteCloser
tag string
linebuf []byte
lock sync.Mutex
}
// NewStderrLogger returns a Logger that outputs messages to Stderr.
func NewStderrLogger(tag string) Logger {
return &streamLogger{
tag: tag,
linebuf: []byte{},
stream: noopCloser{os.Stderr},
}
}
// NewStdoutLogger returns a Logger that outputs messages to Stdout.
func NewStdoutLogger(tag string) Logger {
return &streamLogger{
tag: tag,
linebuf: []byte{},
stream: noopCloser{os.Stdout},
}
}
// NewNullLogger returns a logger that drops messages (outputs to /dev/null).
func NewNullLogger(tag string) Logger {
return &streamLogger{
tag: tag,
linebuf: []byte{},
stream: noopCloser{ioutil.Discard},
}
}
// NewStreamLogger returns a Logger that outputs messages to the specified stream.
func NewStreamLogger(tag string, stream io.WriteCloser) Logger {
return &streamLogger{
tag: tag,
linebuf: []byte{},
stream: stream,
}
}
// writes a formatted message (w/ timestamp, level) to the output stream.
func (w *streamLogger) output(timeStr, level, msg string) {
// We need to serialize access to the linebuffer that is used to assemble the message \
// as well as the output stream we will print to.
w.lock.Lock()
// save memory, (re)use a buffer instead of relying on fmt.Sprintf to format the output string
w.linebuf = w.linebuf[:0]
w.linebuf = append(w.linebuf, timeStr...)
w.linebuf = append(w.linebuf, ' ')
w.linebuf = append(w.linebuf, w.tag...)
w.linebuf = append(w.linebuf, ' ')
w.linebuf = append(w.linebuf, '[')
w.linebuf = fixedWidthStr(5, level, w.linebuf)
w.linebuf = append(w.linebuf, ']')
w.linebuf = append(w.linebuf, ' ')
w.linebuf = append(w.linebuf, msg...)
if len(msg) == 0 || msg[len(msg)-1] != '\n' {
w.linebuf = append(w.linebuf, '\n')
}
w.stream.Write(w.linebuf)
w.lock.Unlock()
}
func (w *streamLogger) Debugf(f string, v ...interface{}) {
msg := fmt.Sprintf(f, v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "DEBUG", msg)
}
func (w *streamLogger) Debugln(v ...interface{}) {
msg := fmt.Sprintln(v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "DEBUG", msg)
}
func (w *streamLogger) Infof(f string, v ...interface{}) {
msg := fmt.Sprintf(f, v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "INFO", msg)
}
func (w *streamLogger) Infoln(v ...interface{}) {
msg := fmt.Sprintln(v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "INFO", msg)
}
func (w *streamLogger) Warningf(f string, v ...interface{}) {
msg := fmt.Sprintf(f, v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "WARN", msg)
}
func (w *streamLogger) Warningln(v ...interface{}) {
msg := fmt.Sprintln(v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "WARN", msg)
}
func (w *streamLogger) Errorf(f string, v ...interface{}) {
msg := fmt.Sprintf(f, v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "ERROR", msg)
}
func (w *streamLogger) Errorln(v ...interface{}) {
msg := fmt.Sprintln(v...)
now := time.Now().Format(timeFormatStr)
w.output(now, "ERROR", msg)
}
func (w *streamLogger) Close() error {
w.lock.Lock()
defer w.lock.Unlock()
return w.stream.Close()
}