-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.go
More file actions
466 lines (401 loc) · 9.88 KB
/
handler.go
File metadata and controls
466 lines (401 loc) · 9.88 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// Package sloglambda is a [slog.Handler] for AWS Lambda functions.
//
// It auto-configures log format (JSON or text) and level from Lambda's
// advanced logging environment variables. Options override env values.
//
// See https://docs.aws.amazon.com/lambda/latest/dg/monitoring-logs.html
package sloglambda
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"os"
"runtime"
"slices"
"strconv"
"strings"
"sync"
"time"
"github.com/aws/aws-lambda-go/lambdacontext"
)
const (
lambdaEnvLogLevel = "AWS_LAMBDA_LOG_LEVEL"
lambdaEnvLogFormat = "AWS_LAMBDA_LOG_FORMAT"
lambdaEnvFunctionName = "AWS_LAMBDA_FUNCTION_NAME"
lambdaEnvFunctionVersion = "AWS_LAMBDA_FUNCTION_VERSION"
traceLevelDebugOffset = 4
fatalLevelErrorOffset = 4
)
var (
kLambdaRecord = "record"
kLambdaFunctionName = "functionName"
kLambdaFunctionVersion = "version"
kLambdaRequestId = "requestId"
kLambdaLogType = "type"
)
// Handler implements [slog.Handler] and writes structured log records to an
// [io.Writer]. Use [NewHandler] to create one.
type Handler struct {
out io.Writer
logType string
mu *sync.Mutex
level slog.Leveler
json bool
source bool
excludeTime bool
gattr []groupOrAttrs
}
// Option configures a [Handler]. Pass options to [NewHandler].
type Option func(*Handler)
// WithLevel sets the minimum log level. Messages below this level are dropped.
// Defaults to the value of AWS_LAMBDA_LOG_LEVEL, or INFO if unset.
func WithLevel(level slog.Leveler) Option {
return func(h *Handler) {
h.level = level
}
}
// WithJSON sets the output format to JSON.
func WithJSON() Option {
return func(h *Handler) {
h.json = true
}
}
// WithText sets the output format to key=value text.
func WithText() Option {
return func(h *Handler) {
h.json = false
}
}
// WithSource adds caller information (function, file, line) to each log record.
func WithSource() Option {
return func(h *Handler) {
h.source = true
}
}
// WithType sets the "type" field included in every log record.
// Defaults to "app.log".
func WithType(logType string) Option {
return func(h *Handler) {
h.logType = logType
}
}
// WithoutTime omits the timestamp from log output. Useful for testing or when
// CloudWatch already provides timestamps.
func WithoutTime() Option {
return func(h *Handler) {
h.excludeTime = true
}
}
// NewHandler returns a [Handler] that writes to w.
//
// By default it reads AWS_LAMBDA_LOG_LEVEL (TRACE, DEBUG, INFO, WARN, ERROR,
// FATAL) and AWS_LAMBDA_LOG_FORMAT (json, text) from the environment. Any
// provided options take precedence over environment values.
func NewHandler(w io.Writer, options ...Option) *Handler {
h := &Handler{
out: w,
mu: new(sync.Mutex),
level: loggerLevelFromLambdaEnv(),
json: loggerIsJSON(),
source: false,
logType: "app.log",
}
for _, opt := range options {
opt(h)
}
return h
}
func loggerLevelFromLambdaEnv() slog.Level {
return loggerLevelFromString(os.Getenv(lambdaEnvLogLevel))
}
func loggerLevelFromString(level string) slog.Level {
switch strings.ToLower(strings.TrimSpace(level)) {
case "trace":
return slog.LevelDebug - traceLevelDebugOffset
case "debug":
return slog.LevelDebug
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
case "fatal":
return slog.LevelError + fatalLevelErrorOffset
default:
return slog.LevelInfo
}
}
func lambdaLoggerLevelString(l slog.Level) string {
str := func(base string, val slog.Level) string {
if val == 0 {
return base
}
return fmt.Sprintf("%s%+d", base, val)
}
switch {
case l < slog.LevelDebug:
return str("TRACE", l-(slog.LevelDebug-traceLevelDebugOffset))
case l < slog.LevelInfo:
return str("DEBUG", l-slog.LevelDebug)
case l < slog.LevelWarn:
return str("INFO", l-slog.LevelInfo)
case l < slog.LevelError:
return str("WARN", l-slog.LevelWarn)
case l < slog.LevelError+fatalLevelErrorOffset:
return str("ERROR", l-slog.LevelError)
default:
return str("FATAL", l-(slog.LevelError+fatalLevelErrorOffset))
}
}
func loggerIsJSON() bool {
env := os.Getenv(lambdaEnvLogFormat)
return strings.ToLower(strings.TrimSpace(env)) == "json"
}
func (h *Handler) Enabled(_ context.Context, level slog.Level) bool {
return level >= h.level.Level()
}
func (h *Handler) WithAttrs(attr []slog.Attr) slog.Handler {
return h.copy(groupOrAttrs{attrs: attr})
}
func (h *Handler) WithGroup(name string) slog.Handler {
return h.copy(groupOrAttrs{group: name})
}
func (h *Handler) copy(g groupOrAttrs) *Handler {
c := *h
c.gattr = make([]groupOrAttrs, len(h.gattr)+1)
copy(c.gattr, h.gattr)
c.gattr[len(c.gattr)-1] = g
return &c
}
func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
value := make(logRecord, 10)
topLevel := value
value.append(slog.String(slog.LevelKey, lambdaLoggerLevelString(record.Level)))
value.append(slog.String(slog.MessageKey, record.Message))
if !record.Time.IsZero() && !h.excludeTime {
value.append(slog.Time(slog.TimeKey, record.Time))
}
lambdaGroup := make(logRecord, 3)
if value, ok := os.LookupEnv(lambdaEnvFunctionName); ok {
lambdaGroup.append(slog.String(kLambdaFunctionName, value))
}
if value, ok := os.LookupEnv(lambdaEnvFunctionVersion); ok {
lambdaGroup.append(slog.String(kLambdaFunctionVersion, value))
}
if lc, _ := lambdacontext.FromContext(ctx); lc != nil {
lambdaGroup.append(slog.String(kLambdaRequestId, lc.AwsRequestID))
}
if len(lambdaGroup) > 0 {
value[kLambdaRecord] = lambdaGroup
}
if h.logType != "" {
value[kLambdaLogType] = h.logType
}
if record.PC != 0 && h.source {
frames := runtime.CallersFrames([]uintptr{record.PC})
frame, _ := frames.Next()
value.append(slog.Group(slog.SourceKey,
slog.String("function", frame.Function),
slog.String("file", frame.File),
slog.Int("line", frame.Line),
))
}
gattr := h.gattr
if record.NumAttrs() == 0 {
for len(gattr) > 0 && gattr[len(gattr)-1].group != "" {
gattr = gattr[:len(gattr)-1]
}
}
for _, ga := range gattr {
if ga.group == "" {
for _, a := range ga.attrs {
value.append(a)
}
} else {
group := make(logRecord, 10)
value[ga.group] = group
value = group
}
}
record.Attrs(func(a slog.Attr) bool {
value.append(a)
return true
})
topLevel.clean()
buf := getBuffer()
defer putBuffer(buf)
if h.json {
if err := json.NewEncoder(buf).Encode(topLevel); err != nil {
h.mu.Lock()
defer h.mu.Unlock()
fmt.Fprintf(h.out, `{"level":"ERROR","msg":"failed to encode log record: %v"}`, err)
fmt.Fprintln(h.out)
return err
}
} else {
if err := writeTextRecord(buf, topLevel, ""); err != nil {
h.mu.Lock()
defer h.mu.Unlock()
fmt.Fprintf(h.out, `level=ERROR msg="failed to encode log record: %v"`, err)
fmt.Fprintln(h.out)
return err
}
// Remove the last trailing space
buf.Truncate(buf.Len() - 1)
buf.Write([]byte("\n"))
}
h.mu.Lock()
defer h.mu.Unlock()
_, err := io.Copy(h.out, buf)
return err
}
var _ slog.Handler = (*Handler)(nil)
type logRecord map[string]any
func (r logRecord) append(attr slog.Attr) {
if attr.Equal(slog.Attr{}) {
return
}
if attr.Value.Kind() == slog.KindGroup {
group := attr.Value.Group()
if len(group) == 0 {
return
}
if attr.Key == "" {
for _, a := range group {
r.append(a)
}
} else {
r[attr.Key] = make(logRecord, len(group))
for _, a := range group {
r[attr.Key].(logRecord).append(a)
}
}
} else {
r[attr.Key] = normalizeValue(attr.Value)
}
}
func (r logRecord) clean() {
for k, v := range r {
if lr, ok := v.(logRecord); ok {
if len(lr) == 0 {
delete(r, k)
} else {
lr.clean()
}
}
}
}
func (r logRecord) keys() []string {
keys := make([]string, 0, len(r))
for k := range r {
keys = append(keys, k)
}
return keys
}
var bufferPool = sync.Pool{
New: func() any {
b := bytes.NewBuffer(nil)
b.Grow(1024)
return b
},
}
func getBuffer() *bytes.Buffer {
return bufferPool.Get().(*bytes.Buffer)
}
func putBuffer(b *bytes.Buffer) {
const maxBufferSize = 16 << 10
if b.Cap() <= maxBufferSize {
b.Reset()
bufferPool.Put(b)
}
}
type groupOrAttrs struct {
group string // group name if non-empty
attrs []slog.Attr // attrs if non-empty
}
func writeTextRecord(w io.Writer, record logRecord, path string) error {
if record == nil {
return nil
}
keys := record.keys()
slices.Sort(keys)
for _, key := range keys {
value := record[key]
if path != "" {
key = path + "." + key
}
if _, ok := value.(logRecord); !ok {
w.Write([]byte(key))
w.Write([]byte("="))
}
switch v := value.(type) {
case logRecord:
if err := writeTextRecord(w, v, key); err != nil {
return err
}
case string:
w.Write([]byte(strconv.Quote(v)))
case fmt.Stringer:
// This is here because nilaway can't figure out that v is not nil
if v != nil {
w.Write([]byte(v.String()))
}
default:
fmt.Fprintf(w, "%v", v)
}
if _, ok := value.(logRecord); !ok {
w.Write([]byte(" "))
}
}
return nil
}
func normalizeValue(v slog.Value) any {
switch v.Kind() {
case slog.KindTime:
return v.Time().Format(time.RFC3339Nano)
case slog.KindBool:
return v.Bool()
case slog.KindDuration:
return v.Duration().String()
case slog.KindFloat64:
return v.Float64()
case slog.KindInt64:
return v.Int64()
case slog.KindString:
return v.String()
case slog.KindUint64:
return v.Uint64()
case slog.KindLogValuer:
return normalizeValue(v.Resolve())
case slog.KindAny:
return normalizeAnyValue(v.Any())
case slog.KindGroup:
attrs := v.Group()
if len(attrs) == 0 {
return nil
}
rec := make(logRecord, len(attrs))
for _, a := range attrs {
rec.append(a)
}
return rec
default:
return v.String()
}
}
func normalizeAnyValue(val any) any {
switch v := val.(type) {
case error:
return v.Error()
case json.Marshaler:
b, err := v.MarshalJSON()
if err != nil {
return err.Error()
}
return string(b)
default:
return val
}
}