-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlogger.go
More file actions
51 lines (45 loc) · 2.05 KB
/
logger.go
File metadata and controls
51 lines (45 loc) · 2.05 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
package cloudrunner
import (
"context"
"go.einride.tech/cloudrunner/cloudrequestlog"
"go.einride.tech/cloudrunner/cloudzap" //nolint:staticcheck // SA1019: internal use of deprecated package pending removal
"go.uber.org/zap" //nolint:gomodguard // legacy zap dependency for backwards compatibility
)
// Logger returns the logger for the current context.
//
// Deprecated: Use slog.InfoContext, slog.WarnContext, slog.ErrorContext, etc. instead.
// The default slog logger is configured with cloudslog.Handler which automatically
// handles trace correlation and Cloud Logging field formatting.
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.
//
// Deprecated: Use cloudslog.With to attach slog attributes to the context instead.
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...)
}