logctx

package
v2.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const LoggerKey = "logger"

Variables

This section is empty.

Functions

func ActiveTraceIdValue

func ActiveTraceIdValue(c context.Context) string

ActiveTraceIdValue returns the value part of ActiveTraceId (does not return the TradeIdKey type part).

func AddTo

func AddTo(c context.Context, args ...any) context.Context

func AddToR

func AddToR(c context.Context, args ...any) (context.Context, *slog.Logger)

func DefaultIdProvider added in v2.1.0

func DefaultIdProvider() string

func IsTty

func IsTty() bool

func Logger

func Logger(c context.Context) *slog.Logger

func LoggerOrNil

func LoggerOrNil(c context.Context) *slog.Logger

func NewLogger

func NewLogger(cfg NewLoggerInput) (*slog.Logger, error)

func NewTracingHandler added in v2.1.0

func NewTracingHandler(h slog.Handler) slog.Handler

func ParseLevel

func ParseLevel(s string) (slog.Level, error)

func UnconfiguredLogger

func UnconfiguredLogger() *slog.Logger

func WithLogger

func WithLogger(c context.Context, logger *slog.Logger) context.Context

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

func WithTracingLogger(c context.Context) context.Context

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 NewHook

func NewHook() *Hook

func NewNullLogger

func NewNullLogger() (*slog.Logger, *Hook)

func WithNullLogger

func WithNullLogger(c context.Context) (context.Context, *Hook)

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) AttrMap

func (t *Hook) AttrMap() map[string]any

func (*Hook) Enabled

func (t *Hook) Enabled(context.Context, slog.Level) bool

func (*Hook) Handle

func (t *Hook) Handle(_ context.Context, r slog.Record) error

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.

func (*Hook) WithAttrs

func (t *Hook) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Hook) WithGroup

func (t *Hook) WithGroup(group string) slog.Handler

type HookRecord

type HookRecord struct {
	Record slog.Record
	Attrs  []slog.Attr
	Group  string
}

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
}

func (*TracingHandler) Enabled added in v2.1.0

func (t *TracingHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*TracingHandler) Handle added in v2.1.0

func (t *TracingHandler) Handle(ctx context.Context, record slog.Record) error

func (*TracingHandler) WithAttrs added in v2.1.0

func (t *TracingHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*TracingHandler) WithGroup added in v2.1.0

func (t *TracingHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL