-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
45 lines (39 loc) · 1.56 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
package cloudrunner
import (
"context"
"go.einride.tech/cloudrunner/cloudrequestlog"
"go.einride.tech/cloudrunner/cloudzap"
"go.uber.org/zap"
)
// Logger returns the logger for the current context.
func Logger(ctx context.Context) *zap.Logger {
logger, ok := cloudzap.GetLogger(ctx)
if !ok {
panic("cloudrunner.Logger must be called with a context from cloudrunner.Run")
}
return logger
}
// WithLoggerFields attaches structured fields to a new logger in the returned child context.
func WithLoggerFields(ctx context.Context, fields ...zap.Field) context.Context {
logger, ok := cloudzap.GetLogger(ctx)
if !ok {
panic("cloudrunner.WithLoggerFields must be called with a context from cloudrunner.Run")
}
return cloudzap.WithLogger(ctx, logger.With(fields...))
}
// AddRequestLogFields adds fields to the current request log, and is safe to call concurrently.
func AddRequestLogFields(ctx context.Context, args ...any) {
requestLogFields, ok := cloudrequestlog.GetAdditionalFields(ctx)
if !ok {
panic("cloudrunner.AddRequestLogFields must be called with a context from cloudrequestlog.Middleware")
}
requestLogFields.Add(args...)
}
// AddRequestLogFieldsToArray appends objects to an array field in the request log and is safe to call concurrently.
func AddRequestLogFieldsToArray(ctx context.Context, key string, objects ...any) {
additionalFields, ok := cloudrequestlog.GetAdditionalFields(ctx)
if !ok {
panic("cloudrunner.AddRequestLogFieldsToArray must be called with a context from cloudrequestlog.Middleware")
}
additionalFields.AddToArray(key, objects...)
}