Skip to content

Commit 35c6fb8

Browse files
committed
Update logger
1 parent 9eb4ec7 commit 35c6fb8

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

logger.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77
"os"
88
)
99

10-
func InitLogger() {
11-
jsonHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
12-
Level: slog.LevelInfo,
13-
})
10+
func InitLogger(options *slog.HandlerOptions) {
11+
if options == nil {
12+
options = &slog.HandlerOptions{
13+
Level: slog.LevelInfo,
14+
}
15+
}
16+
jsonHandler := slog.NewJSONHandler(os.Stdout, options)
1417
// Add span context attributes when Context is passed to logging calls.
1518
instrumentedHandler := handlerWithSpanContext(jsonHandler)
1619
// Set this handler as the global slog handler.
@@ -33,16 +36,16 @@ func (t *spanContextLogHandler) Handle(ctx context.Context, record slog.Record)
3336
// Get the SpanContext from the golang Context.
3437
spanContext := trace.SpanContextFromContext(ctx)
3538
if spanContext.IsValid() {
36-
// Add trace context attributes following Cloud Logging structured log format described
37-
// in https://cloud.google.com/logging/docs/structured-logging#special-payload-fields
39+
// Add trace context attributes following OpenTelemetry structured log format described
40+
// in https://opentelemetry.io/docs/concepts/signals/traces/
3841
record.AddAttrs(
39-
slog.Any("logging.googleapis.com/trace", spanContext.TraceID()),
42+
slog.Any("trace_id", spanContext.TraceID()),
4043
)
4144
record.AddAttrs(
42-
slog.Any("logging.googleapis.com/spanId", spanContext.SpanID()),
45+
slog.Any("span_id", spanContext.SpanID()),
4346
)
4447
record.AddAttrs(
45-
slog.Bool("logging.googleapis.com/trace_sampled", spanContext.TraceFlags().IsSampled()),
48+
slog.Bool("trace_sampled", spanContext.TraceFlags().IsSampled()),
4649
)
4750
}
4851
return t.Handler.Handle(ctx, record)

0 commit comments

Comments
 (0)