-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
217 lines (194 loc) · 6.17 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
package gcflogger
import (
"context"
"encoding/json"
"fmt"
"log"
)
// Logger defines the Cloud Functions logger object.
type Logger struct{}
// Entry defines a log entry.
type Entry struct {
Message string `json:"message"`
Severity string `json:"severity,omitempty"`
}
// String renders an entry structure to the JSON format expected by Cloud Logging.
func (e Entry) String() string {
out, err := json.Marshal(e)
if err != nil {
log.Printf("json.Marshal error: %v", err)
}
return string(out)
}
// New creates a new logger designed to work with Google Cloud Functions.
// ctx is a context.
func New(ctx context.Context) *Logger {
log.SetFlags(0)
return &Logger{}
}
// Default prints a log entry with no assigned severity level.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Default(v ...interface{}) {
log.Println(Entry{
Severity: "DEFAULT",
Message: fmt.Sprint(v...),
})
}
// Defaultf prints a log entry with no assigned severity level.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Defaultf(format string, v ...interface{}) {
log.Println(Entry{
Severity: "DEFAULT",
Message: fmt.Sprintf(format, v...),
})
}
// Debug prints a log entry with severity level 100.
// It should be used for debug or trace information.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Debug(v ...interface{}) {
log.Println(Entry{
Severity: "DEBUG",
Message: fmt.Sprint(v...),
})
}
// Debugf prints a log entry with severity level 100.
// It should be used for debug or trace information.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Debugf(format string, v ...interface{}) {
log.Println(Entry{
Severity: "DEBUG",
Message: fmt.Sprintf(format, v...),
})
}
// Info prints a log entry with severity level 200.
// It should be used for routine information, such as ongoing status or
// performance.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Info(v ...interface{}) {
log.Println(Entry{
Severity: "INFO",
Message: fmt.Sprint(v...),
})
}
// Infof prints a log entry with severity level 200.
// It should be used for routine information, such as ongoing status or
// performance.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Infof(format string, v ...interface{}) {
log.Println(Entry{
Severity: "INFO",
Message: fmt.Sprintf(format, v...),
})
}
// Notice prints a log entry with severity level 300.
// It should be used for normal but significant events, such as start up, shut
// down, or a configuration change.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Notice(v ...interface{}) {
log.Println(Entry{
Severity: "NOTICE",
Message: fmt.Sprint(v...),
})
}
// Noticef prints a log entry with severity level 300.
// It should be used for normal but significant events, such as start up, shut
// down, or a configuration change.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Noticef(format string, v ...interface{}) {
log.Println(Entry{
Severity: "NOTICE",
Message: fmt.Sprintf(format, v...),
})
}
// Warning prints a log entry with severity level 400.
// It should be used for warning events that might cause problems.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Warning(v ...interface{}) {
log.Println(Entry{
Severity: "WARNING",
Message: fmt.Sprint(v...),
})
}
// Warningf prints a log entry with severity level 400.
// It should be used for warning events that might cause problems.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Warningf(format string, v ...interface{}) {
log.Println(Entry{
Severity: "WARNING",
Message: fmt.Sprintf(format, v...),
})
}
// Error prints a log entry with severity level 500.
// It should be used for error events that are likely to cause problems.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Error(v ...interface{}) {
log.Println(Entry{
Severity: "ERROR",
Message: fmt.Sprint(v...),
})
}
// Errorf prints a log entry with severity level 500.
// It should be used for error events that are likely to cause problems.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Errorf(format string, v ...interface{}) {
log.Println(Entry{
Severity: "ERROR",
Message: fmt.Sprintf(format, v...),
})
}
// Critical prints a log entry with severity level 600.
// It should be used for critical events that cause more severe problems or
// outages.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Critical(v ...interface{}) {
log.Println(Entry{
Severity: "CRITICAL",
Message: fmt.Sprint(v...),
})
}
// Criticalf prints a log entry with severity level 600.
// It should be used for critical events that cause more severe problems or
// outages.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Criticalf(format string, v ...interface{}) {
log.Println(Entry{
Severity: "CRITICAL",
Message: fmt.Sprintf(format, v...),
})
}
// Alert prints a log entry with severity level 700.
// It should be used when a person must take an action immediately.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Alert(v ...interface{}) {
log.Println(Entry{
Severity: "ALERT",
Message: fmt.Sprint(v...),
})
}
// Alertf prints a log entry with severity level 700.
// It should be used when a person must take an action immediately.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Alertf(format string, v ...interface{}) {
log.Println(Entry{
Severity: "ALERT",
Message: fmt.Sprintf(format, v...),
})
}
// Emergency prints a log entry with severity level 800.
// It should be used when one or more systems are unusable.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Emergency(v ...interface{}) {
log.Println(Entry{
Severity: "EMERGENCY",
Message: fmt.Sprint(v...),
})
}
// Emergencyf prints a log entry with severity level 800.
// It should be used when one or more systems are unusable.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Emergencyf(format string, v ...interface{}) {
log.Println(Entry{
Severity: "EMERGENCY",
Message: fmt.Sprintf(format, v...),
})
}