logging

package
v1.31.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

func NewLoggerWithWriter

func NewLoggerWithWriter(cfg *Config, w io.Writer) (*slog.Logger, error)

NewLoggerWithWriter creates a logger using cfg options but writing to w.

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