-
Notifications
You must be signed in to change notification settings - Fork 21
/
entry.go
173 lines (148 loc) · 3.86 KB
/
entry.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
package log
import (
"fmt"
"time"
)
// Fields is the type to send to WithFields
type Fields []Field
// Entry defines a single log entry
type Entry struct {
Message string `json:"message"`
Timestamp time.Time `json:"timestamp"`
Fields []Field `json:"fields"`
Level Level `json:"level"`
start time.Time
}
func (e Entry) clone(fields ...Field) Entry {
f := make([]Field, len(e.Fields)+len(fields))
copy(f[copy(f, e.Fields):], fields)
e.Fields = f
return e
}
func newEntry(fields ...Field) Entry {
e := Entry{
Fields: make([]Field, len(fields)+len(logFields)),
}
copy(e.Fields[copy(e.Fields, logFields):], fields)
return e
}
// WithField returns a new log entry with the supplied field.
func (e Entry) WithField(key string, value interface{}) Entry {
ne := e.clone(Field{Key: key, Value: value})
return ne
}
// WithFields returns a new log entry with the supplied fields appended
func (e Entry) WithFields(fields ...Field) Entry {
ne := e.clone(fields...)
return ne
}
// WithTrace with add duration of how long the between this function call and
// the subsequent log
func (e Entry) WithTrace() Entry {
e.start = time.Now()
return e
}
// WithError add a minimal stack trace to the log Entry
func (e Entry) WithError(err error) Entry {
return withErrFn(e.clone(), err)
}
// Debug logs a debug entry
func (e Entry) Debug(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = DebugLevel
HandleEntry(e)
}
// Debugf logs a debug entry with formatting
func (e Entry) Debugf(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = DebugLevel
HandleEntry(e)
}
// Info logs a normal. information, entry
func (e Entry) Info(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = InfoLevel
HandleEntry(e)
}
// Infof logs a normal. information, entry with formatting
func (e Entry) Infof(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = InfoLevel
HandleEntry(e)
}
// Notice logs a notice log entry
func (e Entry) Notice(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = NoticeLevel
HandleEntry(e)
}
// Noticef logs a notice log entry with formatting
func (e Entry) Noticef(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = NoticeLevel
HandleEntry(e)
}
// Warn logs a warning log entry
func (e Entry) Warn(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = WarnLevel
HandleEntry(e)
}
// Warnf logs a warning log entry with formatting
func (e Entry) Warnf(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = WarnLevel
HandleEntry(e)
}
// Panic logs a panic log entry
func (e Entry) Panic(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = PanicLevel
HandleEntry(e)
exitFunc(1)
}
// Panicf logs a panic log entry with formatting
func (e Entry) Panicf(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = PanicLevel
HandleEntry(e)
exitFunc(1)
}
// Alert logs an alert log entry
func (e Entry) Alert(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = AlertLevel
HandleEntry(e)
}
// Alertf logs an alert log entry with formatting
func (e Entry) Alertf(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = AlertLevel
HandleEntry(e)
}
// Fatal logs a fatal log entry
func (e Entry) Fatal(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = FatalLevel
HandleEntry(e)
exitFunc(1)
}
// Fatalf logs a fatal log entry with formatting
func (e Entry) Fatalf(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = FatalLevel
HandleEntry(e)
exitFunc(1)
}
// Error logs an error log entry
func (e Entry) Error(v ...interface{}) {
e.Message = fmt.Sprint(v...)
e.Level = ErrorLevel
HandleEntry(e)
}
// Errorf logs an error log entry with formatting
func (e Entry) Errorf(s string, v ...interface{}) {
e.Message = fmt.Sprintf(s, v...)
e.Level = ErrorLevel
HandleEntry(e)
}