FromContext returns the global Logger or the one stored by a prior call to NewContext.
(ctx context.Context)
| 31 | // FromContext returns the global Logger or the one stored by a prior call |
| 32 | // to NewContext. |
| 33 | func FromContext(ctx context.Context) Logger { |
| 34 | log, err := logr.FromContext(ctx) |
| 35 | if err != nil { |
| 36 | log = global |
| 37 | } |
| 38 | |
| 39 | // Add trace context, if any, according to OpenTelemetry recommendations. |
| 40 | // - https://github.com/open-telemetry/opentelemetry-specification/blob/v1.39.0/specification/compatibility/logging_trace_context.md |
| 41 | if sc := trace.SpanFromContext(ctx).SpanContext(); sc.IsValid() { |
| 42 | log = log.WithValues( |
| 43 | "span_id", sc.SpanID(), |
| 44 | "trace_id", sc.TraceID(), |
| 45 | "trace_flags", sc.TraceFlags()) |
| 46 | } |
| 47 | |
| 48 | return log |
| 49 | } |
| 50 | |
| 51 | // sink implements logr.LogSink using two function pointers. |
| 52 | type sink struct { |