feat: add logs

This commit is contained in:
u80864958
2026-02-13 17:44:56 +01:00
parent 343f6ab165
commit 3b709bc48e
5 changed files with 57 additions and 8 deletions

View File

@@ -3,8 +3,10 @@ package main
import (
"context"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/chatter"
"git.schreifuchs.ch/schreifuchs/pierre-bot/internal/gitadapters/bitbucket"
@@ -46,6 +48,7 @@ type ReviewConfig struct {
}
type Config struct {
LogLevel string `help:"Log verbosity: debug, info, warn, error"`
// Deprecated flags (no prefix). Retained for backward compatibility.
// These will be mapped to the embedded ReviewConfig if provided.
MaxChunkSize int `help:"Deprecated: use --review-max-chunk-size"`
@@ -65,6 +68,7 @@ type Config struct {
}
func main() {
cfg := &Config{}
home, err := os.UserHomeDir()
if err != nil {
@@ -81,6 +85,23 @@ func main() {
kong.Configuration(kongyaml.Loader, "config.yaml", defaultConfig),
)
// Configure global slog logger based on the --log-level flag
lvl := slog.LevelInfo
switch strings.ToLower(cfg.LogLevel) {
case "debug":
lvl = slog.LevelDebug
case "info":
lvl = slog.LevelInfo
case "warn":
lvl = slog.LevelWarn
case "error":
lvl = slog.LevelError
}
// Logs are sent to stderr so that stdout can be safely piped.
// The review output (fmt.Printf) stays on stdout unchanged.
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: lvl}))
slog.SetDefault(logger)
// Backwards compatibility: map deprecated flag values (if any) to the embedded ReviewConfig.
if cfg.MaxChunkSize != 0 {
cfg.Review.MaxChunkSize = cfg.MaxChunkSize