-
Notifications
You must be signed in to change notification settings - Fork 4
/
logger.go
172 lines (141 loc) · 3.84 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
package limacharlie
import (
"encoding/json"
"os"
"sync"
"time"
"github.com/rs/zerolog"
"github.com/google/uuid"
)
// LCLogger is the interface for limacharlie logging
type LCLogger interface {
Fatal(msg string)
Error(msg string)
Warn(msg string)
Info(msg string)
Debug(msg string)
Trace(msg string)
}
// LCLoggerEmpty does not actually log anything
type LCLoggerEmpty struct{}
// Fatal empty stub for logging interface
func (l *LCLoggerEmpty) Fatal(msg string) {}
// Error empty stub for logging interface
func (l *LCLoggerEmpty) Error(msg string) {}
// Warn empty stub for logging interface
func (l *LCLoggerEmpty) Warn(msg string) {}
// Info empty stub for logging interface
func (l *LCLoggerEmpty) Info(msg string) {}
// Debug empty stub for logging interface
func (l *LCLoggerEmpty) Debug(msg string) {}
// Trace empty stub for logging interface
func (l *LCLoggerEmpty) Trace(msg string) {}
// LCLoggerZerolog implements the logging interface with zerolog
type LCLoggerZerolog struct {
l zerolog.Logger
}
// Fatal see zerolog logger fatal function
func (l *LCLoggerZerolog) Fatal(msg string) {
l.l.Fatal().Msg(msg)
}
// Error see zerolog logger error function
func (l *LCLoggerZerolog) Error(msg string) {
l.l.Error().Msg(msg)
}
// Warn see zerolog logger warn function
func (l *LCLoggerZerolog) Warn(msg string) {
l.l.Warn().Msg(msg)
}
// Info see zerolog logger info function
func (l *LCLoggerZerolog) Info(msg string) {
l.l.Info().Msg(msg)
}
// Debug see zerolog logger debug function
func (l *LCLoggerZerolog) Debug(msg string) {
l.l.Debug().Msg(msg)
}
// Trace see zerolog logger trace function
func (l *LCLoggerZerolog) Trace(msg string) {
l.l.Trace().Msg(msg)
}
// LCLoggerGCP implements the logging interface with GCP structured logging
type gcpLogTime struct {
Seconds int64 `json:"seconds"`
Nanos int `json:"nanos"`
}
type gcpLogLine struct {
Message string `json:"message"`
Timestamp gcpLogTime `json:"timestamp"`
CustomData map[string]interface{} `json:"ext_data,omitempty"`
InstanceID string `json:"instance_id,omitempty"`
Severity string `json:"severity,omitempty"`
}
var newLine = []byte{'\n'}
type LCLoggerGCP struct {
m sync.Mutex
instanceID string
once sync.Once
}
func (l *LCLoggerGCP) logToStdout(msg string, severity string) {
line := gcpLogLine{
Message: string(msg),
InstanceID: l.instanceID,
Severity: severity,
}
now := time.Now()
line.Timestamp.Seconds = now.Unix()
line.Timestamp.Nanos = now.Nanosecond()
b, _ := json.Marshal(&line)
l.m.Lock()
defer l.m.Unlock()
os.Stdout.Write(b)
os.Stdout.Write(newLine)
}
func (l *LCLoggerGCP) logToStderr(msg string, severity string) {
line := gcpLogLine{
Message: string(msg),
InstanceID: l.instanceID,
Severity: severity,
}
now := time.Now()
line.Timestamp.Seconds = now.Unix()
line.Timestamp.Nanos = now.Nanosecond()
b, _ := json.Marshal(&line)
l.m.Lock()
defer l.m.Unlock()
os.Stderr.Write(b)
os.Stderr.Write(newLine)
}
func (l *LCLoggerGCP) init() {
l.instanceID = uuid.NewString()
}
// Fatal see GCP logger fatal function
func (l *LCLoggerGCP) Fatal(msg string) {
l.once.Do(l.init)
l.logToStderr(msg, "CRITICAL")
}
// Error see GCP logger error function
func (l *LCLoggerGCP) Error(msg string) {
l.once.Do(l.init)
l.logToStderr(msg, "ERROR")
}
// Warn see GCP logger warn function
func (l *LCLoggerGCP) Warn(msg string) {
l.once.Do(l.init)
l.logToStdout(msg, "WARNING")
}
// Info see GCP logger info function
func (l *LCLoggerGCP) Info(msg string) {
l.once.Do(l.init)
l.logToStdout(msg, "INFO")
}
// Debug see GCP logger debug function
func (l *LCLoggerGCP) Debug(msg string) {
l.once.Do(l.init)
l.logToStdout(msg, "DEBUG")
}
// Trace see GCP logger trace function
func (l *LCLoggerGCP) Trace(msg string) {
l.once.Do(l.init)
l.logToStdout(msg, "DEFAULT")
}