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 AsString(o any) (string, bool)
- 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 AsString ¶ added in v2.1.4
AsString returns o as a string and true if o is a string, a fmt.Stringer, or a reflect.String kind (subtype of string). Otherwise, return "" and false.
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 is the logging level name. Should match slog.Level strings
// ('debug', 'info', 'warning', 'error').
// Case independent.
Level string
// Format should be empty, 'json' or 'text'.
// If empty, use 'json' if File is set, colored text/console if IsTty,
// or 'json' otherwise.
Format string
// File is the filename to log to.
File string
// Out specifies the stream to log to.
// If File is set, log to that file.
// If IsTty, log to os.Stderr.
// Otherwise, log to os.Stdout.
Out io.Writer
// BuildSha will add "build_sha" to the logger fields, if not empty.
BuildSha string
// BuildTime will add "build_time" to the logger fields, it not empty.
BuildTime string
// MakeHandler can override the slog.Handler assigned to the logger.
// 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 are additional fields to add to the logger.
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. The returned trace value will always be a string; if the value is string-like it'll be used, but if it is not string-like, it will have '!BADVALUE-' prepended. For example, ActiveTraceId(context.WithValue(ctx, RequestTraceIdKey, 5)) would return (RequestTraceIdKey, "!BADVALUE-5").
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
}