forked from aphistic/golf
/
log_default.go
171 lines (141 loc) · 6.45 KB
/
log_default.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
165
166
167
168
169
170
171
package golf
import (
"errors"
"fmt"
)
var defaultLogger *Logger
// Set the default Logger. Any calls to log messages not associated with a
// specific Logger (such as calling l.Dbg()) will use the default logger.
func DefaultLogger(l *Logger) {
defaultLogger = l
}
func genDefaultMsg(attrs map[string]interface{}, level int, msg string, va ...interface{}) *Message {
newMsg := defaultLogger.NewMessage()
newMsg.Level = level
if len(va) > 0 {
newMsg.ShortMessage = fmt.Sprintf(msg, va...)
} else {
newMsg.ShortMessage = msg
}
newMsg.Attrs = attrs
return newMsg
}
func logDefaultMsg(attrs map[string]interface{}, level int, msg string, va ...interface{}) error {
if defaultLogger == nil {
return errors.New("A default logger is not set.")
}
newMsg := genDefaultMsg(attrs, level, msg, va...)
return defaultLogger.client.QueueMsg(newMsg)
}
// Log a message 'msg' at LEVEL_DBG level on the default logger
func Dbg(msg string) error {
return logDefaultMsg(nil, LEVEL_DBG, msg)
}
// Log a message at LEVEL_DBG with 'format' populated with values from 'va' on the default logger
func Dbgf(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_DBG, format, va...)
}
// Log a message at LEVEL_DBG with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Dbgm(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_DBG, format, va...)
}
// Log a message 'msg' at LEVEL_INFO level on the default logger
func Info(msg string) error {
return logDefaultMsg(nil, LEVEL_INFO, msg)
}
// Log a message at LEVEL_INFO with 'format' populated with values from 'va' on the default logger
func Infof(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_INFO, format, va...)
}
// Log a message at LEVEL_INFO with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Infom(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_INFO, format, va...)
}
// Log a message 'msg' at LEVEL_NOTICE level on the default logger
func Notice(msg string) error {
return logDefaultMsg(nil, LEVEL_NOTICE, msg)
}
// Log a message at LEVEL_NOTICE with 'format' populated with values from 'va' on the default logger
func Noticef(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_NOTICE, format, va...)
}
// Log a message at LEVEL_NOTICE with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Noticem(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_NOTICE, format, va...)
}
// Log a message 'msg' at LEVEL_WARN level on the default logger
func Warn(msg string) error {
return logDefaultMsg(nil, LEVEL_WARN, msg)
}
// Log a message at LEVEL_WARN with 'format' populated with values from 'va' on the default logger
func Warnf(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_WARN, format, va...)
}
// Log a message at LEVEL_WARN with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Warnm(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_WARN, format, va...)
}
// Log a message 'msg' at LEVEL_ERR level on the default logger on the default logger
func Err(msg string) error {
return logDefaultMsg(nil, LEVEL_ERR, msg)
}
// Log a message at LEVEL_ERR with 'format' populated with values from 'va' on the default logger
func Errf(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_ERR, format, va...)
}
// Log a message at LEVEL_ERR with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Errm(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_ERR, format, va...)
}
// Log a message 'msg' at LEVEL_CRIT level on the default logger
func Crit(msg string) error {
return logDefaultMsg(nil, LEVEL_CRIT, msg)
}
// Log a message at LEVEL_CRIT with 'format' populated with values from 'va' on the default logger
func Critf(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_CRIT, format, va...)
}
// Log a message at LEVEL_CRIT with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Critm(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_CRIT, format, va...)
}
// Log a message 'msg' at LEVEL_ALERT level on the default logger
func Alert(msg string) error {
return logDefaultMsg(nil, LEVEL_ALERT, msg)
}
// Log a message at LEVEL_ALERT with 'format' populated with values from 'va' on the default logger
func Alertf(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_ALERT, format, va...)
}
// Log a message at LEVEL_ALERT with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Alertm(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_ALERT, format, va...)
}
// Log a message 'msg' at LEVEL_EMERG level on the default logger
func Emerg(msg string) error {
return logDefaultMsg(nil, LEVEL_EMERG, msg)
}
// Log a message at LEVEL_EMERG with 'format' populated with values from 'va' on the default logger
func Emergf(format string, va ...interface{}) error {
return logDefaultMsg(nil, LEVEL_EMERG, format, va...)
}
// Log a message at LEVEL_EMERG with 'format' populated with values from 'va' on the default logger.
// The attributes from 'attrs' will be included with the message, overriding any that may
// be set at the Logger level
func Emergm(attrs map[string]interface{}, format string, va ...interface{}) error {
return logDefaultMsg(attrs, LEVEL_EMERG, format, va...)
}