mylog

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: Apache-2.0 Imports: 12 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultLogger = NewDiscardLogger()
)

Functions

func LoggerFromContext

func LoggerFromContext(ctx context.Context) *slog.Logger

LoggerFromContext returns the logger stored in ctx. If ctx is nil or does not contain a logger, slog.Default() is returned.

func NewDiscardLogger

func NewDiscardLogger() *slog.Logger

NewDiscardLogger returns a logger whose output is discarded. This is useful for tests where log output should be suppressed.

func NewLogger

func NewLogger(cfg LoggerConfig) *slog.Logger

NewLogger creates a configured *slog.Logger and a shutdown function. The shutdown function is a no-op in this implementation but is returned to make it easy to add asynchronous or file-based writers later. Call the shutdown function on process exit if you add asynchronous writers.

func ParseLevel

func ParseLevel(s string) slog.Level

ParseLevel maps common textual level names to slog.Level. The function is case-insensitive and ignores surrounding whitespace. If an unrecognized value is provided, slog.LevelInfo is returned.

func WithLogger

func WithLogger(ctx context.Context, lg *slog.Logger) context.Context

ContextWithLogger returns a copy of ctx that carries the provided logger. Use this to associate a logger with a context for downstream callers.

Types

type LoggedEntry

type LoggedEntry struct {
	Time   time.Time      `json:"time"`
	Level  slog.Level     `json:"level"`
	Msg    string         `json:"msg"`
	Attrs  map[string]any `json:"attrs"`
	Source *slog.Source   `json:"source"`
	PC     uintptr        `json:"pc"`
}

LoggedEntry represents a captured structured log entry for assertions in tests. It contains the timestamp, level, message, attributes, source location, and performance details.

func FindEntries

func FindEntries(th *TestHandler,
	pred func(LoggedEntry) bool) []LoggedEntry

FindEntries returns a copy of entries from the TestHandler that satisfy the provided predicate. The handler's internal slice is copied under lock to avoid races.

func RequireEntry

func RequireEntry(t *testing.T, th *TestHandler,
	pred func(LoggedEntry) bool, timeout time.Duration) LoggedEntry

RequireEntry fails the test if a matching entry isn't observed within the given timeout. When a matching entry is found it is returned. If the timeout elapses, the test is failed and the captured entries are included in the failure message to aid debugging.

type LoggerConfig

type LoggerConfig struct {
	Version string

	// If Out is nil, stdout is used.
	Out io.Writer

	Level  slog.Level
	JSON   bool // true => JSON output, false => text
	Source bool
}

LoggerConfig is a minimal, convenient set of options for creating a new slog.Logger.

Fields:

  • Version: application or build version included with each log entry.
  • Out: destination writer for log output. If nil, os.Stdout is used.
  • Level: minimum logging level.
  • JSON: when true, output is JSON; otherwise, human-readable text is used.

type SlogWriter

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

func (SlogWriter) Write

func (w SlogWriter) Write(p []byte) (int, error)

type TestHandler

type TestHandler struct {
	Entries []LoggedEntry
	T       testingT
	// contains filtered or unexported fields
}

TestHandler captures structured entries so tests can assert on logs. It is safe for concurrent use.

func NewTestHandler

func NewTestHandler(t testingT) *TestHandler

NewTestHandler creates an empty TestHandler. Optionally pass a testing.T to have the handler echo captured entries to the test log (via Logf).

func NewTestLogger

func NewTestLogger(t testingT, level slog.Level) (
	*slog.Logger, *TestHandler)

NewTestLogger returns a *slog.Logger that writes to a TestHandler and the handler itself for assertions. The returned logger has a default attribute ("test"="true") to make it easier to identify test logs.

func (*TestHandler) Enabled

func (h *TestHandler) Enabled(_ context.Context, _ slog.Level) bool

Enabled returns true for all levels. Filtering is expected to be handled by the caller or the logger's handler options.

func (*TestHandler) Handle

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

Handle captures the provided record as a LoggedEntry and appends it to the handler's Entries slice. If a testingT was provided, a human-readable line is also logged to the test output.

func (*TestHandler) WithAttrs

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

WithAttrs returns the handler unchanged. Attributes are captured per record in Handle, so no additional state is needed here.

func (*TestHandler) WithGroup

func (h *TestHandler) WithGroup(_ string) slog.Handler

WithGroup returns the handler unchanged. Grouping is not modeled by this simple test handler.

Jump to

Keyboard shortcuts

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