-
Notifications
You must be signed in to change notification settings - Fork 4
/
appinsightlogger.go
231 lines (194 loc) · 6.69 KB
/
appinsightlogger.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright 2018 The Nuclio Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package appinsightslogger
import (
"context"
"errors"
"fmt"
"strconv"
"time"
"github.com/microsoft/ApplicationInsights-Go/appinsights"
"github.com/microsoft/ApplicationInsights-Go/appinsights/contracts"
"github.com/nuclio/logger"
)
type Logger struct {
client appinsights.TelemetryClient
name string
level logger.Level
}
func NewLogger(client appinsights.TelemetryClient, name string, level logger.Level) (*Logger, error) {
return &Logger{
client: client,
name: name,
level: level,
}, nil
}
func (l *Logger) Close() error {
l.Flush()
select {
case <-l.client.Channel().Close(10 * time.Second):
return nil
case <-time.After(30 * time.Second):
return errors.New("timed out closing channel")
}
}
// Error emits an unstructured error log
func (l *Logger) Error(format interface{}, vars ...interface{}) {
if l.level <= logger.LevelError {
l.emitUnstructured(appinsights.Error, format, vars...)
}
}
// Warn emits an unstructured warning log
func (l *Logger) Warn(format interface{}, vars ...interface{}) {
if l.level <= logger.LevelWarn {
l.emitUnstructured(appinsights.Warning, format, vars...)
}
}
// Info emits an unstructured informational log
func (l *Logger) Info(format interface{}, vars ...interface{}) {
if l.level <= logger.LevelInfo {
l.emitUnstructured(appinsights.Information, format, vars...)
}
}
// Debug emits an unstructured debug log
func (l *Logger) Debug(format interface{}, vars ...interface{}) {
// debug will use the *Verbose* severity level
if l.level <= logger.LevelDebug {
l.emitUnstructured(appinsights.Verbose, format, vars...)
}
}
// ErrorWith emits a structured error log
func (l *Logger) ErrorWith(format interface{}, vars ...interface{}) {
if l.level <= logger.LevelError {
l.emitStructured(appinsights.Error, format, vars...)
}
}
// WarnWith emits a structured warning log
func (l *Logger) WarnWith(format interface{}, vars ...interface{}) {
if l.level <= logger.LevelWarn {
l.emitStructured(appinsights.Warning, format, vars...)
}
}
// InfoWith emits a structured info log
func (l *Logger) InfoWith(format interface{}, vars ...interface{}) {
if l.level <= logger.LevelInfo {
l.emitStructured(appinsights.Information, format, vars...)
}
}
// DebugWith emits a structured debug log
func (l *Logger) DebugWith(format interface{}, vars ...interface{}) {
if l.level <= logger.LevelDebug {
l.emitStructured(appinsights.Verbose, format, vars...)
}
}
// ErrorCtx emits an unstructred error log
func (l *Logger) ErrorCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelError {
l.emitUnstructured(appinsights.Error, format, l.addContextToVars(ctx, vars)...)
}
}
// WarnCtx emits an unstructred warn log
func (l *Logger) WarnCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelWarn {
l.emitUnstructured(appinsights.Warning, format, l.addContextToVars(ctx, vars)...)
}
}
// InfoCtx emits an unstructred info log
func (l *Logger) InfoCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelInfo {
l.emitUnstructured(appinsights.Information, format, l.addContextToVars(ctx, vars)...)
}
}
// DebugCtx emits an unstructred debug log
func (l *Logger) DebugCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelDebug {
l.emitUnstructured(appinsights.Verbose, format, l.addContextToVars(ctx, vars)...)
}
}
// ErrorWithCtx emits a structured error log
func (l *Logger) ErrorWithCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelError {
l.emitStructured(appinsights.Error, format, l.addContextToVars(ctx, vars)...)
}
}
// WarnWithCtx emits a structured warn log
func (l *Logger) WarnWithCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelWarn {
l.emitStructured(appinsights.Warning, format, l.addContextToVars(ctx, vars)...)
}
}
// InfoWithCtx emits a structured info log
func (l *Logger) InfoWithCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelInfo {
l.emitStructured(appinsights.Information, format, l.addContextToVars(ctx, vars)...)
}
}
// DebugWithCtx emits a structured debug log
func (l *Logger) DebugWithCtx(ctx context.Context, format interface{}, vars ...interface{}) {
if l.level <= logger.LevelDebug {
l.emitStructured(appinsights.Verbose, format, l.addContextToVars(ctx, vars)...)
}
}
// Flush flushes buffered logs
func (l *Logger) Flush() {
l.client.Channel().Flush()
}
// GetChild returns a child logger
func (l *Logger) GetChild(name string) logger.Logger {
loggerInstance, _ := NewLogger(l.client, fmt.Sprintf("%s.%s", l.name, name), l.level)
return loggerInstance
}
func toString(value interface{}) string {
switch typedValue := value.(type) {
case string:
return typedValue
case int:
return strconv.Itoa(typedValue)
case float64:
return strconv.FormatFloat(typedValue, 'f', 6, 64)
default:
return fmt.Sprintf("%v", value)
}
}
func (l *Logger) emitUnstructured(severity contracts.SeverityLevel, format interface{}, vars ...interface{}) {
message := fmt.Sprintf(toString(format), vars...)
trace := appinsights.NewTraceTelemetry(message, severity)
l.client.Track(trace)
}
func (l *Logger) emitStructured(severity contracts.SeverityLevel, message interface{}, vars ...interface{}) {
trace := appinsights.NewTraceTelemetry(toString(message), severity)
// set properties
for varIdx := 0; varIdx < len(vars); varIdx += 2 {
key := toString(vars[varIdx])
value := toString(vars[varIdx+1])
trace.Properties[key] = value
}
l.client.Track(trace)
}
func (l *Logger) addContextToVars(ctx context.Context, vars []interface{}) []interface{} {
if ctx == nil {
return vars
}
// get request ID from context
requestID := ctx.Value("RequestID")
// if not set, don't add it to vars
if requestID == nil || requestID == "" {
return vars
}
// create a slice 2 slots larger
varsWithContext := make([]interface{}, 0, len(vars)+2)
varsWithContext = append(varsWithContext, "requestID")
varsWithContext = append(varsWithContext, requestID)
varsWithContext = append(varsWithContext, vars...)
return varsWithContext
}