Documentation
¶
Overview ¶
Package logger provides a leveled logger backed by Go's stdlib slog (Go 1.21+). It exposes Debug/Info/Warn/Error helpers for new code AND bridges the stdlib log.Printf API so existing call sites keep working at info level without modification — both paths flow through the same slog handler and respect the configured level.
Usage:
f, _ := os.OpenFile("/path/to/quil.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o600)
logger.Init("debug", f) // accepts: debug, info, warn, error (case-insensitive)
logger.Debug("clipboard: read %d bytes", n)
log.Printf("legacy call still works") // routed through slog at info level
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Debug ¶
Debug logs at debug level. Use for verbose diagnostics that should be off by default. The variadic args are passed to fmt.Sprintf — keep the format string Printf-style to match the existing log.Printf idiom.
func Info ¶
Info logs at info level. Replaces log.Printf for new code that wants the level to be explicit; existing log.Printf calls still work via the bridge.
func Init ¶
Init configures the global logger to write to w at the given level. Acceptable level strings: "debug", "info", "warn"/"warning", "error"/"err" (case-insensitive). Unknown levels default to "info".
Init also bridges the stdlib log package so existing log.Printf / log.Println call sites are routed through the same slog handler at info level. They are dropped if the configured level is warn or error, just like a logger.Info call.
Init is safe to call multiple times — later calls replace the active handler.
All three pieces of global state — the package's `sl`, slog's default logger, and the stdlib log writer — are mutated under the same lock so a concurrent reader can't observe a half-initialized configuration. In practice Init is called once at startup before any goroutines spin up, so this is belt-and-braces.
func ParseLevel ¶
ParseLevel converts a string to a slog.Level. Unknown values yield slog.LevelInfo. Exported so tests and config validation can reuse it.
Types ¶
type RotatingWriter ¶ added in v1.17.0
type RotatingWriter struct {
// contains filtered or unexported fields
}
RotatingWriter is an io.WriteCloser that writes to dir/base and, when the active file would exceed maxSize bytes, rotates it to a timestamped archive (stem-YYYYMMDD-HHMMSS.ext) and opens a fresh base file. At most maxFiles archives are kept; older ones are pruned by modification time. Safe for concurrent Write — the logger fans in from many goroutines.
func NewRotatingWriter ¶ added in v1.17.0
func NewRotatingWriter(dir, base string, maxSizeBytes int64, maxFiles int) (*RotatingWriter, error)
NewRotatingWriter opens dir/base for appending. If the existing file already exceeds maxSizeBytes it is rotated immediately. maxSizeBytes <= 0 or maxFiles <= 0 are coerced to safe minimums so a misconfigured value can never disable writing entirely.
func (*RotatingWriter) Close ¶ added in v1.17.0
func (w *RotatingWriter) Close() error