Documentation ¶
Index ¶
- Constants
- func ActiveTraceIdValue(c context.Context) string
- func AddTo(c context.Context, args ...any) context.Context
- func AddToR(c context.Context, args ...any) (context.Context, *slog.Logger)
- func DefaultIdProvider() string
- func IsTty() bool
- func Logger(c context.Context) *slog.Logger
- func LoggerOrNil(c context.Context) *slog.Logger
- func NewLogger(cfg NewLoggerInput) (*slog.Logger, error)
- func NewTracingHandler(h slog.Handler) slog.Handler
- func ParseLevel(s string) (slog.Level, error)
- func UnconfiguredLogger() *slog.Logger
- func WithLogger(c context.Context, logger *slog.Logger) context.Context
- func WithTraceId(c context.Context, key TraceIdKey) context.Context
- func WithTracingLogger(c context.Context) context.Context
- type Hook
- func (t *Hook) AttrMap() map[string]any
- func (t *Hook) Enabled(context.Context, slog.Level) bool
- func (t *Hook) Handle(_ context.Context, r slog.Record) error
- func (t *Hook) LastRecord() *HookRecord
- func (t *Hook) Records() []HookRecord
- func (t *Hook) WithAttrs(attrs []slog.Attr) slog.Handler
- func (t *Hook) WithGroup(group string) slog.Handler
- type HookRecord
- type IdProviderT
- type NewLoggerInput
- type TraceIdKey
- type TracingHandler
Constants ¶
const LoggerKey = "logger"
Variables ¶
This section is empty.
Functions ¶
func ActiveTraceIdValue ¶
ActiveTraceIdValue returns the value part of ActiveTraceId (does not return the TradeIdKey type part).
func DefaultIdProvider ¶ added in v2.1.0
func DefaultIdProvider() string
func UnconfiguredLogger ¶
func WithLogger ¶
WithLogger returns a new context that adds a logger which can be retrieved with Logger(Context).
func WithTraceId ¶
func WithTraceId(c context.Context, key TraceIdKey) context.Context
func WithTracingLogger ¶
WithTracingLogger stiches together WithTraceId and WithLogger. It extracts the ActiveTraceId and sets it on the logger. In this way you can do WithTracingLogger(WithTraceId(WithLogger(ctx, logger))) to get a logger in the context with a trace id, and then Logger to get the logger back.
Types ¶
type Hook ¶
type Hook struct {
// contains filtered or unexported fields
}
Hook is a hook designed for dealing with logs in test scenarios.
func NewNullLogger ¶
func WithNullLogger ¶
WithNullLogger adds the logger from test.NewNullLogger into the given context (default c to context.Background). Use the hook to get the log messages. See https://github.com/sirupsen/logrus#testing for examples, though this doesn't use logrus the ideas still apply.
func (*Hook) LastRecord ¶
func (t *Hook) LastRecord() *HookRecord
LastRecord returns the last record that was logged or nil.
func (*Hook) Records ¶
func (t *Hook) Records() []HookRecord
Records returns all records that were logged.
type HookRecord ¶
func (HookRecord) AttrMap ¶
func (r HookRecord) AttrMap() map[string]any
type IdProviderT ¶ added in v2.1.0
type IdProviderT func() string
var IdProvider IdProviderT = DefaultIdProvider
type NewLoggerInput ¶
type NewLoggerInput struct { Level string Format string File string BuildSha string BuildTime string // Called with the derived handler options, // and the result of the default handler logic. // Allows the replacement or wrapping of the calculated handler // with a custom handler. // For example, use NewTracingHandler(h) to wrap the handler // in one that will log the span and trace ids in the context. MakeHandler func(*slog.HandlerOptions, slog.Handler) slog.Handler Fields []any }
type TraceIdKey ¶
type TraceIdKey string
const JobTraceIdKey TraceIdKey = "job_trace_id"
JobTraceIdKey is the trace ID key for when we run jobs in the background, like cron jobs.
const MissingTraceIdKey TraceIdKey = "missing_trace_id"
MissingTraceIdKey is the key that will be present to indicate tracing is misconfigured.
const ProcessTraceIdKey TraceIdKey = "process_trace_id"
ProcessTraceIdKey is the trace ID key for the overall process.
const RequestTraceIdKey TraceIdKey = "trace_id"
RequestTraceIdKey is the trace ID key for requests.
const SpanIdKey TraceIdKey = "span_id"
func ActiveTraceId ¶
func ActiveTraceId(c context.Context) (TraceIdKey, string)
ActiveTraceId returns the first valid trace value and type from the given context, or MissingTraceIdKey if there is none.
type TracingHandler ¶ added in v2.1.0
type TracingHandler struct { TraceIdLogKey string SpanIdLogKey string GetTraceId func(context.Context) any GetSpanId func(context.Context) any // contains filtered or unexported fields }