logging

package
v1.24.3 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package logging provides logging helpers.

Index

Constants

View Source
const (
	// LevelDPanic panics in development builds.
	LevelDPanic = slog.LevelError + 1

	// LevelPanic always panics.
	LevelPanic = slog.LevelError + 2

	// LevelFatal exits with a non-zero status.
	LevelFatal = slog.LevelError + 3
)

Variables

This section is empty.

Functions

func Error

func Error(err error) slog.Attr

Error returns slog.Attr for the given error (that can be nil) with error's message as a value.

func LazyDecoder

func LazyDecoder(raw wirebson.RawDocument) slog.LogValuer

LazyDecoder is a lazily evaluated slog.LogValuer for wirebson.RawDocument that tries to decode the document.

func LazyDeepDecoder

func LazyDeepDecoder(raw wirebson.RawDocument) slog.LogValuer

LazyDeepDecoder is a lazily evaluated slog.LogValuer for wirebson.RawDocument that tries to deeply decode the document.

func Logger

func Logger(out io.Writer, opts *NewHandlerOpts, uuid string) *slog.Logger

Logger creates a new slog handler and logger with the given output, options and UUID.

func NewPgxLogger

func NewPgxLogger(l *slog.Logger) tracelog.Logger

NewPgxLogger creates a new tracelog.Logger that uses given slog.Logger.

func SetupDefault

func SetupDefault(opts *NewHandlerOpts, uuid string)

SetupDefault initializes global slog logging with given options and UUID.

func WithName

func WithName(l *slog.Logger, name string) *slog.Logger

WithName returns a logger with a given period-separated name.

How this name is used depends on the handler.

TODO https://github.com/hanzoai/docdb/issues/4431

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is a slog.Handler that wraps another handler with support for:

  • additional log levels (DPanic/ERROR+1 panics in development builds, Panic/ERROR+2 always panics, Fatal/ERROR+3 exits with a non-zero status);
  • shorter source locations;
  • removal of time, level, and source attributes;
  • message checks for leading/trailing spaces and ending punctuation;
  • collecting recent log entries for `getLog` command.

func NewHandler

func NewHandler(out io.Writer, opts *NewHandlerOpts) *Handler

NewHandler creates a new handler with the given options.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, l slog.Level) bool

Enabled implements slog.Handler.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle implements slog.Handler.

func (*Handler) RecentEntries

func (h *Handler) RecentEntries() (*wirebson.Array, error)

RecentEntries returns recent log entries.

func (*Handler) WithAttrs

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

WithAttrs implements slog.Handler.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup implements slog.Handler.

type LazyString

type LazyString func() string

LazyString is a lazily evaluated slog.LogValuer for string.

func (LazyString) LogValue

func (ls LazyString) LogValue() slog.Value

LogValue implements slog.LogValuer.

type MongoLogRecord

type MongoLogRecord struct {
	Timestamp time.Time      `bson:"t"`
	Severity  string         `bson:"s"`
	Component string         `bson:"c"` // TODO https://github.com/hanzoai/docdb/issues/4431
	ID        int            `bson:"id,omitempty"`
	Ctx       string         `bson:"ctx"`
	Msg       string         `bson:"msg"`
	Attr      map[string]any `bson:"attr,omitempty"`
	Tags      []string       `bson:"tags,omitempty"`
}

MongoLogRecord represents a single log message in mongo structured JSON format. For now it lacks the fields that are not used by any caller.

func (*MongoLogRecord) Marshal

func (log *MongoLogRecord) Marshal() ([]byte, error)

Marshal returns the mongo structured JSON encoding of log.

type NewHandlerOpts

type NewHandlerOpts struct {
	Base         string // base handler to create: "console", "text", "json" or "mongo"
	Level        slog.Leveler
	RemoveTime   bool
	RemoveLevel  bool
	RemoveSource bool

	// When unset, causes handler to panic on messages with leading/trailing spaces or ending punctuation.
	// We can't enable checks everywhere because we don't control messages from third-party packages.
	// But we should check our own messages.
	SkipChecks bool
	// contains filtered or unexported fields
}

NewHandlerOpts represents NewHandler options.

Jump to

Keyboard shortcuts

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