NewWithLogger makes a new SpanLogger with a custom log.Logger to send logs to. The provided context will have the logger attached to it and can be retrieved with FromContext or FromContextWithFallback.
(ctx context.Context, l log.Logger, method string, kvps ...any)
| 38 | // to. The provided context will have the logger attached to it and can be |
| 39 | // retrieved with FromContext or FromContextWithFallback. |
| 40 | func NewWithLogger(ctx context.Context, l log.Logger, method string, kvps ...any) (*SpanLogger, context.Context) { |
| 41 | span, ctx := opentracing.StartSpanFromContext(ctx, method) |
| 42 | if ids, _ := users.TenantIDs(ctx); len(ids) > 0 { |
| 43 | span.SetTag(TenantIDTagName, ids) |
| 44 | } |
| 45 | logger := &SpanLogger{ |
| 46 | Logger: log.With(util_log.WithContext(ctx, l), "method", method), |
| 47 | Span: span, |
| 48 | } |
| 49 | if len(kvps) > 0 { |
| 50 | level.Debug(logger).Log(kvps...) |
| 51 | } |
| 52 | |
| 53 | ctx = context.WithValue(ctx, loggerCtxKey, l) |
| 54 | return logger, ctx |
| 55 | } |
| 56 | |
| 57 | // FromContext returns a span logger using the current parent span. If there |
| 58 | // is no parent span, the SpanLogger will only log to the logger |