log

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package log provides a configurable constructor for the standard slog.Logger, allowing for easy setup using the functional options pattern.

It simplifies the creation of a structured logger by abstracting away the handler setup and providing flexible options for setting the level, format, and output from common types like strings.

Usage:

Create a logger that outputs JSON at a debug level to standard error:

logger := log.New(
	log.WithLevel("debug"),
	log.WithFormat("json"),
	log.WithWriter(os.Stderr),
	log.WithAddSource(true), // Include file and line number.
)

slog.SetDefault(logger)
slog.Debug("This is a debug message")

Index

Constants

View Source
const (
	DefaultLevel     = slog.LevelInfo
	DefaultAddSource = false
	DefaultFormat    = FormatText
)

Default configuration values for a new logger.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) *slog.Logger

New creates and configures a new slog.Logger. By default, it logs at slog.LevelInfo in plain text to os.Stdout, without source information. These defaults can be overridden by passing in one or more Option functions.

func ParseLevel

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

ParseLevel converts a case-insensitive string into a slog.Level. It accepts standard level names like "debug", "info", "warn", and "error". It returns an error if the string is not a valid level.

func Silent added in v1.1.1

func Silent() *slog.Logger

Silent creates a logger that discards all output.

Types

type Format

type Format uint8

Format defines the log output format, such as JSON or plain text.

const (
	FormatText Format = iota // Human-readable text format.
	FormatJSON               // JSON format suitable for machine parsing.
)

func ParseFormat

func ParseFormat(s string) (format Format, err error)

ParseFormat converts a case-insensitive string into a Format. Valid inputs are "text" and "json". It returns an error for any other value.

func (Format) String

func (f Format) String() string

String returns the lower-case string representation of the log format.

type Option

type Option func(*config)

Option defines a function that modifies the logger configuration.

func WithAddSource

func WithAddSource(add bool) Option

WithAddSource configures the logger to include the source code position (file and line number) in each log entry.

Note that this has a performance cost and should be used judiciously, often enabled only during development or at debug levels.

func WithFormat

func WithFormat(v any) Option

WithFormat sets the log output format. It accepts either a Format constant (FormatText or FormatJSON) or a case-insensitive string ("text" or "json") as handled by ParseFormat. If an invalid string or type is provided, the option is a no-op.

func WithLevel

func WithLevel(v any) Option

WithLevel sets the minimum log level. It accepts either a slog.Level constant (e.g., slog.LevelDebug) or a case-insensitive string (e.g., "debug") as handled by ParseLevel. If an invalid string or type is provided, the option is a no-op.

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter returns an Option that sets the output destination for the logs. If the provided io.Writer is nil, it is ignored.

Jump to

Keyboard shortcuts

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