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
- func Any(key string, value any) slog.Attr
- func Bool(key string, value bool) slog.Attr
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Enabled(level Level) bool
- func Err(err error) slog.Attr
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func FromContext(ctx context.Context) *slog.Logger
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func Init(opts Options)
- func Int(key string, value int) slog.Attr
- func Logger() *slog.Logger
- func NewContext(ctx context.Context, l *slog.Logger) context.Context
- func SetLevel(level Level)
- func String(key, value string) slog.Attr
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func With(args ...any) *slog.Logger
- func WithGroup(name string) *slog.Logger
- type Format
- type Level
- type Options
Constants ¶
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 DebugContext ¶
DebugContext logs at debug level with context.
func Err ¶
Err is a helper that returns a slog.Attr for an error. Usage: logger.Error("operation failed", logger.Err(err))
func ErrorContext ¶
ErrorContext logs at error level with context.
func FromContext ¶
FromContext returns the logger from the context, or the default logger.
func InfoContext ¶
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 NewContext ¶
NewContext returns a new context with the logger attached.
func WarnContext ¶
WarnContext logs at warn level with context.
Types ¶
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.