forked from adamlaska/boulder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.go
More file actions
289 lines (248 loc) · 7.64 KB
/
log.go
File metadata and controls
289 lines (248 loc) · 7.64 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package log
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io"
"log/syslog"
"os"
"path"
"runtime"
"strings"
"sync"
"github.com/jmhodges/clock"
)
// A Logger logs messages with explicit priority levels. It is
// implemented by a logging back-end as provided by New() or
// NewMock().
type Logger interface {
Err(msg string)
Errf(format string, a ...interface{})
Warning(msg string)
Warningf(format string, a ...interface{})
Info(msg string)
Infof(format string, a ...interface{})
Debug(msg string)
Debugf(format string, a ...interface{})
AuditPanic()
AuditInfo(msg string)
AuditInfof(format string, a ...interface{})
AuditObject(string, interface{})
AuditErr(string)
AuditErrf(format string, a ...interface{})
}
// impl implements Logger.
type impl struct {
w writer
}
// singleton defines the object of a Singleton pattern
type singleton struct {
once sync.Once
log Logger
}
// _Singleton is the single impl entity in memory
var _Singleton singleton
// The constant used to identify audit-specific messages
const auditTag = "[AUDIT]"
// New returns a new Logger that uses the given syslog.Writer as a backend.
func New(log *syslog.Writer, stdoutLogLevel int, syslogLogLevel int) (Logger, error) {
if log == nil {
return nil, errors.New("Attempted to use a nil System Logger.")
}
return &impl{
&bothWriter{log, stdoutLogLevel, syslogLogLevel, clock.New(), os.Stdout},
}, nil
}
// initialize should only be used in unit tests.
func initialize() {
// defaultPriority is never used because we always use specific priority-based
// logging methods.
const defaultPriority = syslog.LOG_INFO | syslog.LOG_LOCAL0
syslogger, err := syslog.Dial("", "", defaultPriority, "test")
if err != nil {
panic(err)
}
logger, err := New(syslogger, int(syslog.LOG_DEBUG), int(syslog.LOG_DEBUG))
if err != nil {
panic(err)
}
_ = Set(logger)
}
// Set configures the singleton Logger. This method
// must only be called once, and before calling Get the
// first time.
func Set(logger Logger) (err error) {
if _Singleton.log != nil {
err = errors.New("You may not call Set after it has already been implicitly or explicitly set.")
_Singleton.log.Warning(err.Error())
} else {
_Singleton.log = logger
}
return
}
// Get obtains the singleton Logger. If Set has not been called first, this
// method initializes with basic defaults. The basic defaults cannot error, and
// subsequent access to an already-set Logger also cannot error, so this method is
// error-safe.
func Get() Logger {
_Singleton.once.Do(func() {
if _Singleton.log == nil {
initialize()
}
})
return _Singleton.log
}
type writer interface {
logAtLevel(syslog.Priority, string)
}
// bothWriter implements writer and writes to both syslog and stdout.
type bothWriter struct {
*syslog.Writer
stdoutLevel int
syslogLevel int
clk clock.Clock
stdout io.Writer
}
func LogLineChecksum(line string) string {
crc := crc32.ChecksumIEEE([]byte(line))
// Using the hash.Hash32 doesn't make this any easier
// as it also returns a uint32 rather than []byte
buf := make([]byte, binary.MaxVarintLen32)
binary.PutUvarint(buf, uint64(crc))
return base64.RawURLEncoding.EncodeToString(buf)
}
// Log the provided message at the appropriate level, writing to
// both stdout and the Logger
func (w *bothWriter) logAtLevel(level syslog.Priority, msg string) {
var prefix string
var err error
const red = "\033[31m\033[1m"
const yellow = "\033[33m"
// Since messages are delimited by newlines, we have to escape any internal or
// trailing newlines before generating the checksum or outputting the message.
msg = strings.Replace(msg, "\n", "\\n", -1)
msg = fmt.Sprintf("%s %s", LogLineChecksum(msg), msg)
switch syslogAllowed := int(level) <= w.syslogLevel; level {
case syslog.LOG_ERR:
if syslogAllowed {
err = w.Err(msg)
}
prefix = red + "E"
case syslog.LOG_WARNING:
if syslogAllowed {
err = w.Warning(msg)
}
prefix = yellow + "W"
case syslog.LOG_INFO:
if syslogAllowed {
err = w.Info(msg)
}
prefix = "I"
case syslog.LOG_DEBUG:
if syslogAllowed {
err = w.Debug(msg)
}
prefix = "D"
default:
err = w.Err(fmt.Sprintf("%s (unknown logging level: %d)", msg, int(level)))
}
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to write to syslog: %s (%s)\n", msg, err)
}
var reset string
if strings.HasPrefix(prefix, "\033") {
reset = "\033[0m"
}
if int(level) <= w.stdoutLevel {
if _, err := fmt.Fprintf(w.stdout, "%s%s %s %s%s\n",
prefix,
w.clk.Now().Format("150405"),
path.Base(os.Args[0]),
msg,
reset); err != nil {
panic(fmt.Sprintf("failed to write to stdout: %v\n", err))
}
}
}
func (log *impl) auditAtLevel(level syslog.Priority, msg string) {
text := fmt.Sprintf("%s %s", auditTag, msg)
log.w.logAtLevel(level, text)
}
// AuditPanic catches panicking executables. This method should be added
// in a defer statement as early as possible
func (log *impl) AuditPanic() {
err := recover()
if err != nil {
buf := make([]byte, 8192)
log.AuditErrf("Panic caused by err: %s", err)
runtime.Stack(buf, false)
log.AuditErrf("Stack Trace (Current frame) %s", buf)
runtime.Stack(buf, true)
log.Warningf("Stack Trace (All frames): %s", buf)
}
}
// Err level messages are always marked with the audit tag, for special handling
// at the upstream system logger.
func (log *impl) Err(msg string) {
log.auditAtLevel(syslog.LOG_ERR, msg)
}
// Errf level messages are always marked with the audit tag, for special handling
// at the upstream system logger.
func (log *impl) Errf(format string, a ...interface{}) {
log.Err(fmt.Sprintf(format, a...))
}
// Warning level messages pass through normally.
func (log *impl) Warning(msg string) {
log.w.logAtLevel(syslog.LOG_WARNING, msg)
}
// Warningf level messages pass through normally.
func (log *impl) Warningf(format string, a ...interface{}) {
log.Warning(fmt.Sprintf(format, a...))
}
// Info level messages pass through normally.
func (log *impl) Info(msg string) {
log.w.logAtLevel(syslog.LOG_INFO, msg)
}
// Infof level messages pass through normally.
func (log *impl) Infof(format string, a ...interface{}) {
log.Info(fmt.Sprintf(format, a...))
}
// Debug level messages pass through normally.
func (log *impl) Debug(msg string) {
log.w.logAtLevel(syslog.LOG_DEBUG, msg)
}
// Debugf level messages pass through normally.
func (log *impl) Debugf(format string, a ...interface{}) {
log.Debug(fmt.Sprintf(format, a...))
}
// AuditInfo sends an INFO-severity message that is prefixed with the
// audit tag, for special handling at the upstream system logger.
func (log *impl) AuditInfo(msg string) {
log.auditAtLevel(syslog.LOG_INFO, msg)
}
// AuditInfof sends an INFO-severity message that is prefixed with the
// audit tag, for special handling at the upstream system logger.
func (log *impl) AuditInfof(format string, a ...interface{}) {
log.AuditInfo(fmt.Sprintf(format, a...))
}
// AuditObject sends an INFO-severity JSON-serialized object message that is prefixed
// with the audit tag, for special handling at the upstream system logger.
func (log *impl) AuditObject(msg string, obj interface{}) {
jsonObj, err := json.Marshal(obj)
if err != nil {
log.auditAtLevel(syslog.LOG_ERR, fmt.Sprintf("Object could not be serialized to JSON. Raw: %+v", obj))
return
}
log.auditAtLevel(syslog.LOG_INFO, fmt.Sprintf("%s JSON=%s", msg, jsonObj))
}
// AuditErr can format an error for auditing; it does so at ERR level.
func (log *impl) AuditErr(msg string) {
log.auditAtLevel(syslog.LOG_ERR, msg)
}
// AuditErrf can format an error for auditing; it does so at ERR level.
func (log *impl) AuditErrf(format string, a ...interface{}) {
log.AuditErr(fmt.Sprintf(format, a...))
}