-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
228 lines (183 loc) · 5.79 KB
/
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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2016 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
// Package logger provides a simple logging interface that supports multiple
// levels of logging. It goes up to 11.
package logger
import (
"fmt"
"log"
"github.com/mgutz/ansi"
)
const (
// Silent logs no output at all.
Silent = iota
// Error logs only errors.
Error = iota
// Blocked logs blocking calls and lower.
Blocked = iota
// Unblocked logs all unblocked notifications and lower.
Unblocked = iota
// Warn logs warnings and lower.
Warn = iota
// Reject logs rejections (e.g., in a firewall) and lower.
Reject = iota
// Listen logs listeners and lower.
Listen = iota
// Install logs install notifications and lower.
Install = iota
// Init logs initialization notifications and lower.
Init = iota
// Request logs incoming requests and lower.
Request = iota
// Info logs info output and lower.
Info = iota
// Debug logs all log output.
Debug = iota
max = iota
)
// Logger only outputs log messages that are equal or lower than its log level.
type Logger struct{ level int }
// LogLevel translates human-readable log level names to their int values.
var LogLevel = map[string]int{
"silent": Silent,
"error": Error,
"blocked": Blocked,
"unblocked": Unblocked,
"warn": Warn,
"reject": Reject,
"listen": Listen,
"install": Install,
"init": Init,
"request": Request,
"info": Info,
"debug": Debug}
var prefixes = [...]string{
"",
ansi.Color("[***error***]", "black:red"),
ansi.Color("[**blocked**]", "255+b:165"),
ansi.Color("[*unblocked*]", "255+b:165"),
ansi.Color("[**warning**]", "red:yellow+h"),
ansi.Color("[ rejection ]", "125+b:208"),
ansi.Color("[ listening ]", "black:cyan+h"),
/* */ "[ installed ]",
/* */ "[initialized]",
/* */ "[ request ]",
/* */ "[information]",
/* */ "[ debug ]"}
func out(loggerLevel, messageLevel int, format string, v ...interface{}) {
if loggerLevel == Silent || messageLevel == Silent {
return
}
message := fmt.Sprintf(format, v...)
if messageLevel > Silent && messageLevel <= loggerLevel {
log.Printf("%s %s", prefixes[messageLevel], message)
}
}
// Level returns a logger instance's log level if logger level allows.
func (l *Logger) Level() int { return l.level }
// SetLevel sets a logger instance's log level if logger level allows.
func (l *Logger) SetLevel(level int) error {
if level > Debug || level < Silent {
return fmt.Errorf("logger level must be >= %d and < %d", Silent, max)
}
l.level = level
return nil
}
// Error logs an error if logger level allows.
func (l *Logger) Error(format string, v ...interface{}) {
out(l.level, Error, format, v...)
}
// Blocked logs a blocked message if logger level allows.
func (l *Logger) Blocked(format string, v ...interface{}) {
out(l.level, Blocked, format, v...)
}
// Unblocked logs an unblocked notification if logger level allows.
func (l *Logger) Unblocked(format string, v ...interface{}) {
out(l.level, Unblocked, format, v...)
}
// Warn logs a warning if logger level allows.
func (l *Logger) Warn(format string, v ...interface{}) {
out(l.level, Warn, format, v...)
}
// Reject logs a rejection if logger level allows.
func (l *Logger) Reject(format string, v ...interface{}) {
out(l.level, Reject, format, v...)
}
// Listen logs a listener message if logger level allows.
func (l *Logger) Listen(format string, v ...interface{}) {
out(l.level, Listen, format, v...)
}
// Install logs an install message if logger level allows.
func (l *Logger) Install(format string, v ...interface{}) {
out(l.level, Install, format, v...)
}
// Init logs an initialization message if logger level allows.
func (l *Logger) Init(format string, v ...interface{}) {
out(l.level, Init, format, v...)
}
// Request logs a request if logger level allows.
func (l *Logger) Request(format string, v ...interface{}) {
out(l.level, Request, format, v...)
}
// Info logs an info message if logger level allows.
func (l *Logger) Info(format string, v ...interface{}) {
out(l.level, Info, format, v...)
}
// Debug logs a debug message if logger level allows.
func (l *Logger) Debug(format string, v ...interface{}) {
out(l.level, Debug, format, v...)
}
// MustError logs an error.
func MustError(format string, v ...interface{}) {
out(max, Error, format, v...)
}
// MustBlocked logs a blocked message.
func MustBlocked(format string, v ...interface{}) {
out(max, Blocked, format, v...)
}
// MustUnblocked logs an unblocked message.
func MustUnblocked(format string, v ...interface{}) {
out(max, Unblocked, format, v...)
}
// MustWarn logs a warning.
func MustWarn(format string, v ...interface{}) {
out(max, Warn, format, v...)
}
// MustReject logs a rejection.
func MustReject(format string, v ...interface{}) {
out(max, Reject, format, v...)
}
// MustListen logs a listener message.
func MustListen(format string, v ...interface{}) {
out(max, Listen, format, v...)
}
// MustInstall logs an install message.
func MustInstall(format string, v ...interface{}) {
out(max, Install, format, v...)
}
// MustInit logs an initialization message.
func MustInit(format string, v ...interface{}) {
out(max, Init, format, v...)
}
// MustRequest logs a request.
func MustRequest(format string, v ...interface{}) {
out(max, Request, format, v...)
}
// MustInfo logs an info message.
func MustInfo(format string, v ...interface{}) {
out(max, Info, format, v...)
}
// MustDebug logs a debug message.
func MustDebug(format string, v ...interface{}) {
out(max, Debug, format, v...)
}
// New instantiates a Logger and sets its level. If the level is invalid, it
// returns an error.
func New(level int) (*Logger, error) {
l := new(Logger)
if err := l.SetLevel(level); err != nil {
return nil, err
}
return l, nil
}