logger

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package logger provides structured logging for FoGit using Go's log/slog.

This package wraps slog to provide a consistent logging interface across the application. It separates diagnostic logging (debug, info, warn, error) from user-facing output which continues to use fmt for direct terminal display.

Usage:

// Initialize at startup
logger.Init(logger.Options{Level: logger.LevelDebug})

// Use throughout the application
logger.Debug("processing feature", "id", feature.ID)
logger.Info("feature created", "name", feature.Name)
logger.Warn("config not found, using defaults", "path", configPath)
logger.Error("failed to save", "error", err)

// With context
logger.With("command", "feature").Info("starting command")

Index

Constants

View Source
const (
	LevelDebug = slog.LevelDebug // -4
	LevelInfo  = slog.LevelInfo  // 0
	LevelWarn  = slog.LevelWarn  // 4
	LevelError = slog.LevelError // 8
)

Log levels matching slog levels.

Variables

This section is empty.

Functions

func Any

func Any(key string, value any) slog.Attr

Any is a helper that returns a slog.Attr for any value.

func Bool

func Bool(key string, value bool) slog.Attr

Bool is a helper that returns a slog.Attr for a bool.

func Debug

func Debug(msg string, args ...any)

Debug logs a message at debug level.

func DebugContext

func DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs at debug level with context.

func Enabled

func Enabled(level Level) bool

Enabled returns true if the given level is enabled.

func Err

func Err(err error) slog.Attr

Err is a helper that returns a slog.Attr for an error. Usage: logger.Error("operation failed", logger.Err(err))

func Error

func Error(msg string, args ...any)

Error logs a message at error level.

func ErrorContext

func ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs at error level with context.

func FromContext

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

FromContext returns the logger from the context, or the default logger.

func Info

func Info(msg string, args ...any)

Info logs a message at info level.

func InfoContext

func InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs at info level with context.

func Init

func Init(opts Options)

Init initializes the package-level logger with the given options. This should be called early in main() before any logging occurs. If called multiple times, subsequent calls will reconfigure the logger.

func Int

func Int(key string, value int) slog.Attr

Int is a helper that returns a slog.Attr for an int.

func Logger

func Logger() *slog.Logger

Logger returns the underlying slog.Logger for advanced usage.

func NewContext

func NewContext(ctx context.Context, l *slog.Logger) context.Context

NewContext returns a new context with the logger attached.

func SetLevel

func SetLevel(level Level)

SetLevel changes the logging level at runtime.

func String

func String(key, value string) slog.Attr

String is a helper that returns a slog.Attr for a string.

func Warn

func Warn(msg string, args ...any)

Warn logs a message at warn level.

func WarnContext

func WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs at warn level with context.

func With

func With(args ...any) *slog.Logger

With returns a new Logger with the given attributes added.

func WithGroup

func WithGroup(name string) *slog.Logger

WithGroup returns a new Logger with the given group name.

Types

type Format

type Format string

Format represents the output format for logs.

const (
	// FormatText produces human-readable text output.
	FormatText Format = "text"
	// FormatJSON produces structured JSON output.
	FormatJSON Format = "json"
)

type Level

type Level = slog.Level

Level represents the logging level.

type Options

type Options struct {
	// Level is the minimum level to log. Default: LevelWarn
	Level Level

	// Format is the output format. Default: FormatText
	Format Format

	// Output is where logs are written. Default: os.Stderr
	Output io.Writer

	// AddSource adds source file and line to log entries.
	// Only enabled when Level is LevelDebug.
	AddSource bool
}

Options configures the logger.

Jump to

Keyboard shortcuts

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