logger

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package logger provides structured logging with file and console output.

Index

Constants

View Source
const (
	// DefaultLogMaxSize is the default maximum size in megabytes before log rotation
	DefaultLogMaxSize = 2

	// DefaultLogMaxBackups is the default number of old log files to retain
	DefaultLogMaxBackups = 3

	// DefaultLogMaxAge is the default maximum number of days to retain old log files
	DefaultLogMaxAge = 28

	// LevelTrace is a custom log level below Debug, only logged to file
	LevelTrace = slog.LevelDebug - 4
)

Variables

This section is empty.

Functions

func GetLogPath

func GetLogPath(opts LoggerOptions) string

GetLogPath returns the path where logs will be written based on options

func PrintLogFile

func PrintLogFile(w io.Writer, opts LoggerOptions) error

PrintLogFile prints the current log file to the provided writer If writer is nil, prints to stdout. Returns error if log file doesn't exist or can't be read.

Types

type ConsoleHandler

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

ConsoleHandler is a simple handler that outputs clean messages to console

func (*ConsoleHandler) Enabled

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

func (*ConsoleHandler) Handle

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

func (*ConsoleHandler) WithAttrs

func (h *ConsoleHandler) WithAttrs(_ []slog.Attr) slog.Handler

func (*ConsoleHandler) WithGroup

func (h *ConsoleHandler) WithGroup(_ string) slog.Handler

type Logger

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

Logger handles dual output logging (file + console)

func NewLogger

func NewLogger(opts LoggerOptions) (*Logger, error)

NewLogger creates a new logger instance

func (*Logger) Close

func (l *Logger) Close()

Close closes the log file and flushes any buffered data

func (*Logger) Debug

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

Debug logs a debug message

func (*Logger) Error

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

Error logs an error message

func (*Logger) GetLogPath

func (l *Logger) GetLogPath() string

GetLogPath returns the path to the current log file

func (*Logger) Info

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

Info logs an info message

func (*Logger) Trace

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

Trace logs a trace message (file only, never to console)

func (*Logger) Warn

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

Warn logs a warning message

type LoggerInterface

type LoggerInterface interface {
	Trace(msg string, args ...any) // Only logs to file, never to console
	Debug(msg string, args ...any)
	Info(msg string, args ...any)
	Warn(msg string, args ...any)
	Error(msg string, args ...any)
	Close()
	GetLogPath() string
}

LoggerInterface defines the logging methods

type LoggerOptions

type LoggerOptions struct {
	Verbose    bool
	LogDir     string // If empty, uses %LOCALAPPDATA%\smpc
	MaxSize    int    // Max size in megabytes before rotation (default: 10)
	MaxBackups int    // Max number of old log files to keep (default: 3)
	MaxAge     int    // Max days to keep old log files (default: 28)
	Compress   bool   // Whether to compress rotated logs (default: true)
}

LoggerOptions configures the logger

type NoOpLogger

type NoOpLogger struct{}

NoOpLogger is a logger that does nothing - useful for tests

func NewNoOpLogger

func NewNoOpLogger() *NoOpLogger

NewNoOpLogger creates a new no-op logger for testing

func (*NoOpLogger) Close

func (n *NoOpLogger) Close()

func (*NoOpLogger) Debug

func (n *NoOpLogger) Debug(msg string, args ...any)

func (*NoOpLogger) Error

func (n *NoOpLogger) Error(msg string, args ...any)

func (*NoOpLogger) GetLogPath

func (n *NoOpLogger) GetLogPath() string

func (*NoOpLogger) Info

func (n *NoOpLogger) Info(msg string, args ...any)

func (*NoOpLogger) Trace

func (n *NoOpLogger) Trace(msg string, args ...any)

func (*NoOpLogger) Warn

func (n *NoOpLogger) Warn(msg string, args ...any)

Jump to

Keyboard shortcuts

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