forked from jcelliott/lumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consolelog.go
159 lines (133 loc) · 3.48 KB
/
consolelog.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
154
155
156
157
158
159
package lumber
import (
"fmt"
"io"
"os"
"time"
)
type ConsoleLogger struct {
out io.WriteCloser
outLevel int
timeFormat string
prefix string
levels []string
closed bool
}
// Create a new console logger with output level o, and an empty prefix
func NewConsoleLogger(o int) *ConsoleLogger {
return &ConsoleLogger{
out: os.Stdout,
outLevel: o,
timeFormat: TIMEFORMAT,
prefix: "",
levels: levels,
}
}
func NewBasicLogger(f io.WriteCloser, level int) *ConsoleLogger {
return &ConsoleLogger{
out: f,
outLevel: level,
timeFormat: TIMEFORMAT,
prefix: "",
levels: levels,
}
}
// Generic output function. If msg does not end with a newline, one will be appended.
func (l *ConsoleLogger) output(msg *Message) {
buf := []byte{}
buf = append(buf, msg.time.Format(l.timeFormat)...)
if l.prefix != "" {
buf = append(buf, ' ')
buf = append(buf, l.prefix...)
}
buf = append(buf, ' ')
buf = append(buf, l.levels[msg.level]...)
buf = append(buf, ' ')
buf = append(buf, msg.m...)
if len(msg.m) > 0 && msg.m[len(msg.m)-1] != '\n' {
buf = append(buf, '\n')
}
l.out.Write(buf)
}
// Sets the available levels for this logger
func (l *ConsoleLogger) SetLevels(lvls []string) {
if lvls[len(lvls)-1] != "*LOG*" {
lvls = append(lvls, "*LOG*")
}
l.levels = lvls
}
// Sets the output level for this logger
func (l *ConsoleLogger) Level(o int) {
if o >= 0 && o <= len(l.levels)-1 {
l.outLevel = o
}
}
// Sets the prefix for this logger
func (l *ConsoleLogger) Prefix(p string) {
l.prefix = p
}
// Sets the time format for this logger
func (l *ConsoleLogger) TimeFormat(f string) {
l.timeFormat = f
}
// Close the logger
func (l *ConsoleLogger) Close() {
l.closed = true
l.output(&Message{len(l.levels) - 1, "Closing log now", time.Now()})
l.out.Close()
}
func (l *ConsoleLogger) log(lvl int, format string, v ...interface{}) {
if lvl < l.outLevel || l.closed {
return
}
// recover in case the channel has already been closed (unlikely race condition)
// this could also be solved with a lock, but would cause a performance hit
defer recover()
l.output(&Message{lvl, fmt.Sprintf(format, v...), time.Now()})
}
// Logging functions
func (l *ConsoleLogger) Fatal(format string, v ...interface{}) {
l.log(FATAL, format, v...)
}
func (l *ConsoleLogger) Error(format string, v ...interface{}) {
l.log(ERROR, format, v...)
}
func (l *ConsoleLogger) Warn(format string, v ...interface{}) {
l.log(WARN, format, v...)
}
func (l *ConsoleLogger) Info(format string, v ...interface{}) {
l.log(INFO, format, v...)
}
func (l *ConsoleLogger) Debug(format string, v ...interface{}) {
l.log(DEBUG, format, v...)
}
func (l *ConsoleLogger) Trace(format string, v ...interface{}) {
l.log(TRACE, format, v...)
}
func (l *ConsoleLogger) Print(lvl int, v ...interface{}) {
l.output(&Message{lvl, fmt.Sprint(v...), time.Now()})
}
func (l *ConsoleLogger) Printf(lvl int, format string, v ...interface{}) {
l.output(&Message{lvl, fmt.Sprintf(format, v...), time.Now()})
}
func (l *ConsoleLogger) GetLevel() int {
return l.outLevel
}
func (l *ConsoleLogger) IsFatal() bool {
return l.outLevel <= FATAL
}
func (l *ConsoleLogger) IsError() bool {
return l.outLevel <= ERROR
}
func (l *ConsoleLogger) IsWarn() bool {
return l.outLevel <= WARN
}
func (l *ConsoleLogger) IsInfo() bool {
return l.outLevel <= INFO
}
func (l *ConsoleLogger) IsDebug() bool {
return l.outLevel <= DEBUG
}
func (l *ConsoleLogger) IsTrace() bool {
return l.outLevel <= TRACE
}