log.go

 1package log
 2
 3import (
 4	"log/slog"
 5	"path/filepath"
 6
 7	"github.com/charmbracelet/crush/pkg/config"
 8
 9	"gopkg.in/natefinch/lumberjack.v2"
10)
11
12func Init(cfg *config.Config) {
13	logRotator := &lumberjack.Logger{
14		Filename:   filepath.Join(cfg.Options.DataDirectory, "logs", "crush.log"),
15		MaxSize:    10,    // Max size in MB
16		MaxBackups: 0,     // Number of backups
17		MaxAge:     30,    // Days
18		Compress:   false, // Enable compression
19	}
20
21	level := slog.LevelInfo
22	if cfg.Options.Debug {
23		level = slog.LevelDebug
24	}
25
26	logger := slog.NewJSONHandler(logRotator, &slog.HandlerOptions{
27		Level:     level,
28		AddSource: true,
29	})
30
31	slog.SetDefault(slog.New(logger))
32}