logger

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package logger provides structured logging capabilities for the doppel duplicate file finder.

This package wraps Go's slog package to provide consistent logging across the application with support for multiple output formats:

  • Pretty: Human-readable colored output for development
  • JSON: Structured JSON output for automation and monitoring
  • Text: Plain text output for simple logging needs

The package supports configurable log levels, output destinations, and provides both global logger functions and instance-based logging.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

Debug logs a debug message.

func DebugAttrs

func DebugAttrs(ctx context.Context, message string, attrs ...slog.Attr)

DebugAttrs logs a debug message with attributes.

func DebugCtx

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

DebugCtx logs a debug message with context.

func Error

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

Error logs an error message.

func ErrorAttrs

func ErrorAttrs(ctx context.Context, message string, attrs ...slog.Attr)

ErrorAttrs logs an error message with attributes.

func ErrorCtx

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

ErrorCtx logs an error message with context.

func Info

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

Info logs an informational message.

func InfoAttrs

func InfoAttrs(ctx context.Context, message string, attrs ...slog.Attr)

InfoAttrs logs an informational message with attributes.

func InfoCtx

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

InfoCtx logs an informational message with context.

func NewDefault

func NewDefault(config *Config) error

NewDefault creates a new default logger with the provided configuration.

func ResetDefault

func ResetDefault()

ResetDefault resets the default logger to the standard slog.Default logger.

func SetDefault

func SetDefault(logger *Logger) error

SetDefault sets the default logger to the provided logger.

func Warn

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

Warn logs a warning message.

func WarnAttrs

func WarnAttrs(ctx context.Context, message string, attrs ...slog.Attr)

WarnAttrs logs a warning message with attributes.

func WarnCtx

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

WarnCtx logs a warning message with context.

Types

type Config

type Config struct {
	// Format specifies the log format (e.g., "text", "json", "pretty", etc.)
	Format string

	// Writer is the output destination for the logs (e.g., [os.Stdout], [os.Stderr], or a file)
	Writer io.Writer

	// Options holds additional options for the slog.Handler
	Options *slog.HandlerOptions
}

Config holds the configuration for the logger.

func NewConfig

func NewConfig(opts *slog.HandlerOptions, format, output string) (Config, io.Closer, error)

NewConfig creates a new Config instance based on the provided parameters. If the output is a file, it is opened and an io.Closer is returned. The closer can be used to close the file when done.

type JSONHandler

type JSONHandler struct {
	// contains filtered or unexported fields
}

JSONHandler is a slog.JSONHandler with the corrected source position of the caller.

func NewJSONHandler

func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) *JSONHandler

NewJSONHandler creates a JSONHandler that writes to w, using the given options. If opts is nil, the default options are used.

func (*JSONHandler) Enabled

func (h *JSONHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*JSONHandler) Handle

func (h *JSONHandler) Handle(ctx context.Context, r slog.Record) error

Handle formats its slog.Record argument as a JSON object on a single line.

func (*JSONHandler) WithAttrs

func (h *JSONHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new JSONHandler whose attributes consists of h's attributes followed by attrs.

func (*JSONHandler) WithGroup

func (h *JSONHandler) WithGroup(name string) slog.Handler

WithGroup returns a new JSONHandler with the given group appended to h's groups.

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

Logger is a wrapper around slog.Logger with additional configuration and convenience methods for logging at different levels.

func Default

func Default() *Logger

Default returns the default logger.

func New

func New(config *Config) (*Logger, error)

New creates a new Logger instance with the provided configuration.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs a debug message.

func (*Logger) DebugAttrs

func (l *Logger) DebugAttrs(ctx context.Context, message string, attrs ...slog.Attr)

DebugAttrs logs a debug message with attributes.

func (*Logger) DebugContext

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

DebugContext logs a debug message with context.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error logs an error message.

func (*Logger) ErrorAttrs

func (l *Logger) ErrorAttrs(ctx context.Context, message string, attrs ...slog.Attr)

ErrorAttrs logs an error message with attributes.

func (*Logger) ErrorContext

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

ErrorContext logs an error message with context.

func (*Logger) Handler

func (l *Logger) Handler() slog.Handler

Handler returns the underlying slog.Handler used by the logger.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info logs an informational message.

func (*Logger) InfoAttrs

func (l *Logger) InfoAttrs(ctx context.Context, message string, attrs ...slog.Attr)

InfoAttrs logs an informational message with attributes.

func (*Logger) InfoContext

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

InfoContext logs an informational message with context.

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log logs a message at the specified level.

func (*Logger) LogAttrs

func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, message string, attrs ...slog.Attr)

LogAttrs logs a message with attributes at the specified level.

func (*Logger) Logger

func (l *Logger) Logger() *slog.Logger

Logger returns the underlying slog.Logger instance.

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any)

Warn logs a warning message.

func (*Logger) WarnAttrs

func (l *Logger) WarnAttrs(ctx context.Context, message string, attrs ...slog.Attr)

WarnAttrs logs a warning message with attributes.

func (*Logger) WarnContext

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

WarnContext logs a warning message with context.

func (*Logger) With

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

With returns a Logger that includes the given attributes in each output operation.

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

WithGroup returns a Logger that starts a group if name is non-empty.

type PrettyHandler

type PrettyHandler struct {
	// contains filtered or unexported fields
}

PrettyHandler implements slog.Handler for human-friendly terminal output.

func NewPrettyHandler

func NewPrettyHandler(w io.Writer, opts *slog.HandlerOptions) *PrettyHandler

NewPrettyHandler creates a new pretty handler.

func (*PrettyHandler) Enabled

func (h *PrettyHandler) Enabled(_ context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*PrettyHandler) Handle

func (h *PrettyHandler) Handle(_ context.Context, r slog.Record) error

Handle formats and outputs a log record.

func (*PrettyHandler) WithAttrs

func (h *PrettyHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new PrettyHandler whose attributes consists of h's attributes followed by attrs.

func (*PrettyHandler) WithGroup

func (h *PrettyHandler) WithGroup(name string) slog.Handler

WithGroup returns a new PrettyHandler with the given group appended to h's groups.

type TextHandler

type TextHandler struct {
	// contains filtered or unexported fields
}

TextHandler is a slog.TextHandler with the corrected source position of the caller.

func NewTextHandler

func NewTextHandler(w io.Writer, opts *slog.HandlerOptions) *TextHandler

NewTextHandler creates a TextHandler that writes to w, using the given options. If opts is nil, the default options are used.

func (*TextHandler) Enabled

func (h *TextHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.

func (*TextHandler) Handle

func (h *TextHandler) Handle(ctx context.Context, r slog.Record) error

Handle formats its slog.Record argument as a single line of space-separated key=value items.

func (*TextHandler) WithAttrs

func (h *TextHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new TextHandler whose attributes consists of h's attributes followed by attrs.

func (*TextHandler) WithGroup

func (h *TextHandler) WithGroup(name string) slog.Handler

WithGroup returns a new TextHandler with the given group appended to h's groups.

Jump to

Keyboard shortcuts

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