forked from jcelliott/lumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lumber.go
164 lines (134 loc) · 2.66 KB
/
lumber.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
160
161
162
163
164
/*
Package lumber implements a simple logger that supports log levels and rotation.
*/
package lumber
import (
"strings"
"time"
)
const (
TRACE = iota
DEBUG
INFO
WARN
ERROR
FATAL
TIMEFORMAT = "2006-Jan-02 15:04:05.000000"
)
var (
stdLog Logger = NewConsoleLogger(INFO)
levels = []string{"TRACE", "DEBUG", "INFO ", "WARN ", "ERROR", "FATAL", "*LOG*"}
timeFormat = TIMEFORMAT
)
type Logger interface {
Fatal(string, ...interface{})
Error(string, ...interface{})
Warn(string, ...interface{})
Info(string, ...interface{})
Debug(string, ...interface{})
Trace(string, ...interface{})
IsFatal() bool
IsError() bool
IsWarn() bool
IsInfo() bool
IsDebug() bool
IsTrace() bool
GetLevel() int
Print(int, ...interface{})
Printf(int, string, ...interface{})
Level(int)
Prefix(string)
TimeFormat(string)
Close()
output(msg *Message)
}
type Message struct {
level int
m string
time time.Time
}
// SetLogger sets a new default logger
func SetLogger(l Logger) {
if stdLog != nil {
stdLog.Close()
}
stdLog = l
}
// Returns the string representation of the level
func LvlStr(l int) string {
if l >= 0 && l <= len(levels)-1 {
return levels[l]
}
return ""
}
// Returns the int value of the level
func LvlInt(s string) int {
for i, str := range levels {
if strings.TrimSpace(str) == strings.ToUpper(s) {
return i
}
}
return 0
}
// Sets the output level for the default logger
func Level(o int) {
stdLog.Level(o)
}
// Sets the time format for the default logger
func TimeFormat(f string) {
stdLog.TimeFormat(f)
}
// Close the default logger
func Close() {
stdLog.Close()
}
// Prefix sets a prefix for the default logger
func Prefix(p string) {
stdLog.Prefix(p)
}
// Logging functions
func Fatal(format string, v ...interface{}) {
stdLog.Fatal(format, v...)
}
func Error(format string, v ...interface{}) {
stdLog.Error(format, v...)
}
func Warn(format string, v ...interface{}) {
stdLog.Warn(format, v...)
}
func Info(format string, v ...interface{}) {
stdLog.Info(format, v...)
}
func Debug(format string, v ...interface{}) {
stdLog.Debug(format, v...)
}
func Trace(format string, v ...interface{}) {
stdLog.Trace(format, v...)
}
func Print(lvl int, v ...interface{}) {
stdLog.Print(lvl, v...)
}
func Printf(lvl int, format string, v ...interface{}) {
stdLog.Printf(lvl, format, v...)
}
func GetLevel() int {
return stdLog.GetLevel()
}
func IsFatal() bool {
return stdLog.IsFatal()
}
func IsError() bool {
return stdLog.IsError()
}
func IsWarn() bool {
return stdLog.IsWarn()
}
func IsInfo() bool {
return stdLog.IsInfo()
}
func IsDebug() bool {
return stdLog.IsDebug()
}
func IsTrace() bool {
return stdLog.IsTrace()
}