File: log.go

package info (click to toggle)
golang-github-kisom-goutils 0.0~git20161101.0.858c9cb-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 384 kB
  • ctags: 331
  • sloc: makefile: 6
file content (279 lines) | stat: -rw-r--r-- 10,404 bytes parent folder | download | duplicates (3)
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
package logging

import (
	"fmt"
	"io"
	"os"
	"time"
)

// Logger provides a standardised logging interface.
type Logger interface {
	// SetLevel sets the minimum log level.
	SetLevel(Level)

	// Good returns true if the Logger is healthy.
	Good() bool

	// Status returns an error corresponding to the logger's state;
	// if it's healthy (e.g. Good() returns true), Error will
	// return nil.
	Status() error

	// Close gives the Logger the opportunity to perform any cleanup.
	Close()

	// Log messages consist of four components:
	//
	// 1. The **level** attaches a notion of priority to the log message.
	//    Several log levels are available:
	//
	//    + FATAL (32): the system is in an unsuable state, and cannot
	//      continue to run. Most of the logging for this will cause the
	//      program to exit with an error code.
	//    + CRITICAL (16): critical conditions. The error, if uncorrected, is
	//      likely to cause a fatal condition shortly.  An example is running
	//      out of disk space. This is something that the ops team should get
	//      paged for.
	//    + ERROR (8): error conditions. A single error doesn't require an
	//      ops team to be paged, but repeated errors should often trigger a
	//      page based on threshold triggers. An example is a network
	//      failure: it might be a transient failure (these do happen), but
	//      most of the time it's self-correcting.
	//    + WARNING (4): warning conditions. An example of this is a bad
	//      request sent to a server. This isn't an error on the part of the
	//      program, but it may be indicative of other things. Like errors,
	//      the ops team shouldn't be paged for errors, but a page might be
	//      triggered if a certain threshold of warnings is reached (which is
	//      typically much higher than errors). For example, repeated
	//      warnings might be a sign that the system is under attack.
	//    + INFO (2): informational message. This is a normal log message
	//      that is used to deliver information, such as recording
	//      requests. Ops teams are never paged for informational
	//      messages. This is the default log level.
	//    + DEBUG (1): debug-level message. These are only used during
	//      development or if a deployed system repeatedly sees abnormal
	//      errors.
	//
	//    The numeric values indicate the priority of a given level.
	//
	// 2. The **actor** is used to specify which component is generating
	//    the log message. This could be the program name, or it could be
	//    a specific component inside the system.
	//
	// 3. The **event** is a short message indicating what happened. This is
	//    most like the traditional log message.
	//
	// 4. The **attributes** are an optional set of key-value string pairs that
	//    provide additional information.
	//
	// Additionally, each log message has an associated timestamp. For the
	// text-based logs, this is "%FT%T%z"; for the binary logs, this is a
	// 64-bit Unix timestamp. An example text-based timestamp might look like ::
	//
	//   [2016-03-27T20:59:27-0700] [INFO] [actor:server event:request received] client=192.168.2.5 request-size=839
	//
	// Note that this is organised in a manner that facilitates parsing::
	//
	//   /\[(\d{4}-\d{3}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4})\] \[(\w+\)]\) \[actor:(.+?) event:(.+?)\]/
	//
	// will cover the header:
	//
	// + ``$1`` contains the timestamp
	// + ``$2`` contains the level
	// + ``$3`` contains the actor
	// + ``$4`` contains the event
	Debug(actor, event string, attrs map[string]string)
	Info(actor, event string, attrs map[string]string)
	Warn(actor, event string, attrs map[string]string)
	Error(actor, event string, attrs map[string]string)
	Critical(actor, event string, attrs map[string]string)
	Fatal(actor, event string, attrs map[string]string)
	FatalCode(exitcode int, actor, event string, attrs map[string]string)
	FatalNoDie(actor, event string, attrs map[string]string)
}

// A LogWriter is a Logger that operates on an io.Writer.
type LogWriter struct {
	wo, we io.Writer
	lvl    Level
	state  error
	snl    bool // suppress newline
}

// NewLogWriter takes an output writer (wo) and an error writer (we),
// and produces a new Logger. If the error writer is nil, error logs
// will be multiplexed onto the output writer.
func NewLogWriter(wo, we io.Writer) *LogWriter {
	if we == nil {
		we = wo
	}

	return &LogWriter{
		wo:    wo,
		we:    we,
		lvl:   DefaultLevel,
		state: nil,
	}
}

func (lw *LogWriter) output(w io.Writer, lvl Level, actor, event string, attrs map[string]string) {
	t := time.Now().Format(DateFormat)
	fmt.Fprintf(w, "[%s] [%s] [actor:%s event:%s]", t, levelPrefix[lvl], actor, event)
	for k, v := range attrs {
		fmt.Fprintf(w, " %s=%s", k, v)
	}

	if !lw.snl {
		fmt.Fprintf(w, "\n")
	}
}

// Debug emits a debug-level message. These are only used during
// development or if a deployed system repeatedly sees abnormal
// errors.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) Debug(actor, event string, attrs map[string]string) {
	if lw.lvl > LevelDebug {
		return
	}
	lw.output(lw.wo, LevelDebug, actor, event, attrs)
}

// Info emits an informational message. This is a normal log message
// that is used to deliver information, such as recording
// requests. Ops teams are never paged for informational
// messages. This is the default log level.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) Info(actor, event string, attrs map[string]string) {
	if lw.lvl > LevelInfo {
		return
	}
	lw.output(lw.wo, LevelInfo, actor, event, attrs)
}

// Warn emits a warning message. An example of this is a bad request
// sent to a server. This isn't an error on the part of the program,
// but it may be indicative of other things. Like errors, the ops team
// shouldn't be paged for errors, but a page might be triggered if a
// certain threshold of warnings is reached (which is typically much
// higher than errors). For example, repeated warnings might be a sign
// that the system is under attack.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) Warn(actor, event string, attrs map[string]string) {
	if lw.lvl > LevelWarning {
		return
	}
	lw.output(lw.we, LevelWarning, actor, event, attrs)
}

// Error emits an error message. A single error doesn't require an ops
// team to be paged, but repeated errors should often trigger a page
// based on threshold triggers. An example is a network failure: it
// might be a transient failure (these do happen), but most of the
// time it's self-correcting.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) Error(actor, event string, attrs map[string]string) {
	if lw.lvl > LevelError {
		return
	}
	lw.output(lw.we, LevelError, actor, event, attrs)
}

// Critical emits a message indicating a critical condition. The
// error, if uncorrected, is likely to cause a fatal condition
// shortly.  An example is running out of disk space. This is
// something that the ops team should get paged for.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) Critical(actor, event string, attrs map[string]string) {
	if lw.lvl > LevelCritical {
		return
	}
	lw.output(lw.we, LevelCritical, actor, event, attrs)
}

// Fatal emits a message indicating that the system is in an unsuable
// state, and cannot continue to run. The program will exit with exit
// code 1.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) Fatal(actor, event string, attrs map[string]string) {
	if lw.lvl > LevelFatal {
		return
	}
	lw.output(lw.we, LevelFatal, actor, event, attrs)
	os.Exit(1)
}

// Fatal emits a message indicating that the system is in an unsuable
// state, and cannot continue to run. The program will exit with the
// exit code speicfied in the exitcode argument.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) FatalCode(exitcode int, actor, event string, attrs map[string]string) {
	if lw.lvl > LevelFatal {
		return
	}
	lw.output(lw.we, LevelFatal, actor, event, attrs)
	os.Exit(exitcode)
}

// Fatal emits a message indicating that the system is in an unsuable
// state, and cannot continue to run. The program will not exit; it is
// assumed that the caller has some final clean up to perform.
//
// Actor specifies the component emitting the message; event indicates
// the event that caused the log message to be emitted. attrs is a map
// of key-value string pairs that can be used to provide additional
// information.
func (lw *LogWriter) FatalNoDie(actor, event string, attrs map[string]string) {
	if lw.lvl > LevelFatal {
		return
	}
	lw.output(lw.we, LevelFatal, actor, event, attrs)
}

// Good returns true if the logger is healthy.
func (lw *LogWriter) Good() bool {
	return lw.state == nil
}

// Status returns an error value from the logger if it isn't healthy,
// or nil if the logger is healthy.
func (lw *LogWriter) Status() error {
	return lw.state
}

// SetLevel changes the log level.
func (lw *LogWriter) SetLevel(l Level) {
	lw.lvl = l
}

// Close is a no-op that satisfies the Logger interface.
func (lw *LogWriter) Close() {}