-
Notifications
You must be signed in to change notification settings - Fork 0
/
console_logger.go
60 lines (49 loc) · 1.1 KB
/
console_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
package log
import (
"bufio"
"io"
"os"
"github.com/no-src/log/formatter"
"github.com/no-src/log/level"
)
type consoleLogger struct {
baseLogger
w io.Writer
bw *bufio.Writer
withBuffer bool
}
// NewConsoleLogger get a console logger
func NewConsoleLogger(lvl level.Level) Logger {
return newConsoleLoggerWithBuffer(lvl, false)
}
// newConsoleLoggerWithBuffer get a console logger with buffer
func newConsoleLoggerWithBuffer(lvl level.Level, withBuffer bool) Logger {
logger := &consoleLogger{
w: os.Stdout,
bw: bufio.NewWriter(os.Stdout),
withBuffer: withBuffer,
}
// init baseLogger
logger.baseLogger.init(logger, lvl, true)
return logger
}
func (l *consoleLogger) Write(p []byte) (n int, err error) {
if l.withBuffer {
return l.bw.Write(p)
}
return l.w.Write(p)
}
func (l *consoleLogger) Close() error {
if l.withBuffer {
return l.bw.Flush()
}
return nil
}
func (l *consoleLogger) WithFormatter(f formatter.Formatter) Logger {
l.setFormatter(f)
return l
}
func (l *consoleLogger) WithTimeFormat(f string) Logger {
l.setTimeFormat(f)
return l
}