- Fix stuttering type names (e.g. config.ConfigParams → config.Params) - Add doc comments to all exported types/functions/methods - Add package doc comments to all packages - Fix JSON tags to camelCase - Extract magic numbers to constants - Add blank lines per nlreturn/wsl_v5 rules - Use errors.Is() for error comparison - Unexport NewLoggingResponseWriter (not used externally) - Replace for-range on ctx.Done() with channel receive - Rename unused parameters to _ - AGENTS.md: all changes via feature branches, no direct main commits
74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
// Package logger provides structured logging for the application.
|
|
package logger
|
|
|
|
import (
|
|
"log/slog"
|
|
"os"
|
|
|
|
"git.eeqj.de/sneak/chat/internal/globals"
|
|
"go.uber.org/fx"
|
|
)
|
|
|
|
// Params defines the dependencies for creating a Logger.
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Globals *globals.Globals
|
|
}
|
|
|
|
// Logger wraps slog with application-specific configuration.
|
|
type Logger struct {
|
|
log *slog.Logger
|
|
level *slog.LevelVar
|
|
params Params
|
|
}
|
|
|
|
// New creates a new Logger with appropriate handler based on terminal detection.
|
|
func New(_ fx.Lifecycle, params Params) (*Logger, error) {
|
|
l := new(Logger)
|
|
l.level = new(slog.LevelVar)
|
|
l.level.Set(slog.LevelInfo)
|
|
|
|
tty := false
|
|
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
|
|
tty = true
|
|
}
|
|
|
|
var handler slog.Handler
|
|
if tty {
|
|
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
|
Level: l.level,
|
|
AddSource: true,
|
|
})
|
|
} else {
|
|
handler = slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
|
Level: l.level,
|
|
AddSource: true,
|
|
})
|
|
}
|
|
|
|
l.log = slog.New(handler)
|
|
l.params = params
|
|
|
|
return l, nil
|
|
}
|
|
|
|
// EnableDebugLogging switches the log level to debug.
|
|
func (l *Logger) EnableDebugLogging() {
|
|
l.level.Set(slog.LevelDebug)
|
|
l.log.Debug("debug logging enabled", "debug", true)
|
|
}
|
|
|
|
// Get returns the underlying slog.Logger.
|
|
func (l *Logger) Get() *slog.Logger {
|
|
return l.log
|
|
}
|
|
|
|
// Identify logs the application name and version at startup.
|
|
func (l *Logger) Identify() {
|
|
l.log.Info("starting",
|
|
"appname", l.params.Globals.Appname,
|
|
"version", l.params.Globals.Version,
|
|
)
|
|
}
|