-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
217 lines (177 loc) · 4.32 KB
/
logger.go
File metadata and controls
217 lines (177 loc) · 4.32 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
package logger
import (
"time"
"fmt"
"encoding/json"
"runtime"
"bytes"
"os"
"io"
)
type severity int
const (
DEBUG severity = iota
INFO
WARN
ERROR
)
var LogLevelName = [...]string{
"DEBUG",
"INFO",
"WARN",
"ERROR",
}
var LogLevelValue = map[string]severity{
"DEBUG": DEBUG,
"INFO": INFO,
"WARN": WARN,
"ERROR": ERROR,
}
type Fields map[string]string
type ServiceContext struct {
Service string `json:"service"`
Version string `json:"version"`
}
type ReportLocation struct {
FilePath string `json:"filePath"`
FunctionName string `json:"functionName"`
LineNumber int `json:"lineNumber"`
}
type Context struct {
Data Fields `json:"data,omitempty"`
ReportLocation *ReportLocation `json:"reportLocation,omitempty"`
}
type Payload struct {
Severity string `json:"severity"`
EventTime string `json:"eventTime"`
Caller string `json:"caller,omitempty"`
Message string `json:"message"`
ServiceContext *ServiceContext `json:"serviceContext"`
Context *Context `json:"context,omitempty"`
Stacktrace string `json:"stacktrace,omitempty"`
}
type Log struct {
Payload *Payload
writer io.Writer
}
func New() *Log {
if os.Getenv("SERVICE") == "" || os.Getenv("VERSION") == "" {
fmt.Errorf("cannot instantiate the logger, make sure the SERVICE and VERSION environment vars are set correctly")
}
return &Log{
Payload: &Payload{
ServiceContext: &ServiceContext{
Service: os.Getenv("SERVICE"),
Version: os.Getenv("VERSION"),
},
},
writer: os.Stdout,
}
}
func (l *Log) SetWriter(w io.Writer) *Log {
l.writer = w
return l
}
func (l *Log) set(key, val string) {
if l.Payload.Context == nil {
l.Payload.Context = &Context{
Data: Fields{},
}
}
l.Payload.Context.Data[key] = val
}
func (l *Log) log(severity, message string) {
l.Payload = &Payload{
Severity: severity,
EventTime: time.Now().Format(time.RFC3339),
Message: message,
ServiceContext: l.Payload.ServiceContext,
Context: l.Payload.Context,
Stacktrace: l.Payload.Stacktrace,
}
payload, ok := json.Marshal(l.Payload)
if ok != nil {
fmt.Errorf("cannot marshal payload: %s", ok.Error())
}
fmt.Fprintln(l.writer, string(payload))
}
// Checks whether the specified log level is valid in the current environment
func isValidLogLevel(logLevel severity) bool {
curLogLev, ok := LogLevelValue[os.Getenv("LOG_LEVEL")]
if !ok {
fmt.Errorf("the LOG_LEVEL environment variable is not set or has an incorrect value")
}
return curLogLev <= logLevel
}
func (l *Log) With(fields Fields) *Log {
if os.Getenv("SERVICE") == "" || os.Getenv("VERSION") == "" {
fmt.Errorf("cannot instantiate the logger, make sure the SERVICE and VERSION environment vars are set correctly")
}
log := &Log{
Payload: &Payload{
ServiceContext: &ServiceContext{
Service: os.Getenv("SERVICE"),
Version: os.Getenv("VERSION"),
},
},
writer: os.Stdout,
}
for k, v := range fields {
log.set(k, v)
}
return log
}
func (l *Log) Debug(message string) {
if !isValidLogLevel(DEBUG) {
return
}
l.log(LogLevelName[DEBUG], message)
}
func (l *Log) Debugf(message string, args ...interface{}) {
l.Debug(fmt.Sprintf(message, args...))
}
func (l *Log) Metric(message string) {
if !isValidLogLevel(INFO) {
return
}
l.log(LogLevelName[INFO], message)
}
func (l *Log) Info(message string) {
if !isValidLogLevel(INFO) {
return
}
l.log(LogLevelName[INFO], message)
}
func (l *Log) Infof(message string, args ...interface{}) {
l.Info(fmt.Sprintf(message, args...))
}
func (l *Log) Warn(message string) {
if !isValidLogLevel(WARN) {
return
}
l.log(LogLevelName[WARN], message)
}
func (l *Log) Warnf(message string, args ...interface{}) {
l.Warn(fmt.Sprintf(message, args...))
}
func (l *Log) Error(message string) {
buffer := make([]byte, 1024)
runtime.Stack(buffer, false)
_, file, line, _ := runtime.Caller(1)
l.Payload = &Payload{
ServiceContext: l.Payload.ServiceContext,
Context: &Context{
Data: l.Payload.Context.Data,
ReportLocation: &ReportLocation{
FilePath: file,
FunctionName: "unknown",
LineNumber: line,
},
},
Stacktrace: string(bytes.Trim(buffer, "\x00")),
}
l.log(LogLevelName[ERROR], message)
}
func (l *Log) Errorf(message string, args ...interface{}) {
l.Error(fmt.Sprintf(message, args...))
}