-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
121 lines (113 loc) · 4.17 KB
/
interface.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
// Package liblog provides a common interface for logging backends
package liblog
// A Field is a marshaling operation used to add a key-value pair to a logger's
// context
type Field interface {
// Key returns the fields key part
Key() string
// Value returns the fields value part
Value() interface{}
}
type fieldImpl struct {
key string
value interface{}
}
func (f *fieldImpl) Key() string { return f.key }
func (f *fieldImpl) Value() interface{} { return f.value }
// NewField provides a simple shortcut function to create a struct that
// satisfies the Field interface
func NewField(key string, value interface{}) Field {
return &fieldImpl{
key: key,
value: value,
}
}
// Level is a logging priority. Higher levels are more important.
type Level int8
const (
// DebugLevel logs are typically voluminous, and are usually disabled in
// production.
DebugLevel Level = iota - 1
// InfoLevel is the default logging priority.
InfoLevel
// WarnLevel logs are more important than Info, but don't need individual
// human review.
WarnLevel
// ErrorLevel logs are high-priority. If an application is running smoothly,
// it shouldn't generate any error-level logs.
ErrorLevel
// DPanicLevel logs are particularly important errors. In development the
// logger panics after writing the message.
DPanicLevel
// PanicLevel logs a message, then panics.
PanicLevel
// FatalLevel logs a message, then calls os.Exit(1).
FatalLevel
)
// String returns a readable representation of Level
func (l Level) String() string {
switch l {
case DebugLevel:
return "DEBUG"
case InfoLevel:
return "INFO"
case WarnLevel:
return "WARN"
case ErrorLevel:
return "ERROR"
case DPanicLevel:
return "DPANIC"
case PanicLevel:
return "PANIC"
case FatalLevel:
return "FATAL"
default:
return "UNKNOWN"
}
}
// Logger provides leveled, structured logging. It is an abstract interface for
// different logging backends.
type Logger interface {
// Named adds a new path segment to the logger's name. Segments are joined by
// periods. By default, Loggers are unnamed.
Named(name string) Logger
// With creates a child logger and adds structured context to it. Fields added
// to the child don't affect the parent, and vice versa.
With(fields ...Field) Logger
// Sync flushes any buffered log entries and should be called by applications
// before exiting
Sync() error
// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
Debug(msg string, fields ...Field)
// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
Info(msg string, fields ...Field)
// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
Warn(msg string, fields ...Field)
// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
Error(msg string, fields ...Field)
// ErrorReturn logs a message at ErrorLevel exactly like the Error function, but
// additionally returns an error object, containing the provided information.
ErrorReturn(msg string, fields ...Field) error
// DPanic logs a message at DPanicLevel. The message includes any fields
// passed at the log site, as well as any fields accumulated on the logger.
//
// If the logger is in development mode, it then panics (DPanic means
// "development panic"). This is useful for catching errors that are
// recoverable, but shouldn't ever happen.
DPanic(msg string, fields ...Field)
// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
Panic(msg string, fields ...Field)
// Fatal logs a message at FatalLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
Fatal(msg string, fields ...Field)
}