logging

package
v1.15.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package logging provides structured logging using log/slog with configurable output format, color support, and output destinations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidFormat is returned when the log format is invalid.
	ErrInvalidFormat = errors.New("invalid format: must be text or json")
	// ErrEmptyOutput is returned when the output destination is empty.
	ErrEmptyOutput = errors.New("output cannot be empty")
)
View Source
var (
	// ErrUnsupportedFormat is returned when the format is not supported.
	ErrUnsupportedFormat = errors.New("unsupported format")
)

Functions

func NewLogger

func NewLogger(cfg *Config) (*slog.Logger, io.Closer, error)

NewLogger creates a new structured logger based on the provided configuration. Returns the logger and an optional closer (non-nil if logging to a file). The caller is responsible for calling closer.Close() when done.

Types

type Config

type Config struct {
	// Format specifies the log output format (text or json).
	Format Format

	// Level specifies the minimum log level to output.
	Level slog.Level

	// Color enables colored output for text format.
	// nil (default) = auto-detect based on TTY, true = force on, false = force off.
	Color *bool

	// Output specifies where to write logs (stderr, stdout, or a file path).
	Output Output

	// AddSource adds source code position to log records.
	AddSource bool
}

Config holds the logging configuration.

func NewConfig

func NewConfig() *Config

NewConfig creates a new Config with sensible defaults.

func (*Config) GetWriter

func (c *Config) GetWriter() (io.Writer, io.Closer, error)

GetWriter returns the appropriate io.Writer for the configured output destination.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if the configuration is valid.

type Format

type Format string

Format represents the log output format.

const (
	// FormatText outputs logs in human-readable text format.
	FormatText Format = "text"
	// FormatJSON outputs logs in JSON format.
	FormatJSON Format = "json"
)

type Output

type Output string

Output represents the log output destination.

const (
	// OutputStderr writes logs to stderr (default).
	OutputStderr Output = "stderr"
	// OutputStdout writes logs to stdout.
	OutputStdout Output = "stdout"
)

Directories

Path Synopsis
Package cliflags provides urfave/cli/v3 flag definitions for the logging package.
Package cliflags provides urfave/cli/v3 flag definitions for the logging package.

Jump to

Keyboard shortcuts

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