(cfg CLIConfig)
| 14 | ) |
| 15 | |
| 16 | func NewLogger(cfg CLIConfig) log.Logger { |
| 17 | l, err := levelFromString(cfg.Level) |
| 18 | if err != nil { |
| 19 | panic(err) |
| 20 | } |
| 21 | |
| 22 | // Select output (enable Windows color support if needed) |
| 23 | output := io.Writer(os.Stdout) |
| 24 | useColor := cfg.Color && (isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd())) && os.Getenv("TERM") != "dumb" |
| 25 | |
| 26 | // Enable cross-platform color support for Windows terminals using go-colorable wrapper |
| 27 | if useColor { |
| 28 | output = colorable.NewColorableStdout() |
| 29 | } |
| 30 | |
| 31 | var h slog.Handler |
| 32 | switch cfg.Format { |
| 33 | case "terminal": |
| 34 | // Terminal format with timestamp and optional colors |
| 35 | h = log.NewTerminalHandlerWithLevel(output, l, useColor) |
| 36 | case "text", "logfmt": |
| 37 | // slog text handler is logfmt-like: key=value pairs per line |
| 38 | h = slog.NewTextHandler(output, &slog.HandlerOptions{Level: l}) |
| 39 | case "json", "json-pretty": |
| 40 | // Pretty JSON is not natively supported; fall back to compact JSON |
| 41 | h = slog.NewJSONHandler(output, &slog.HandlerOptions{Level: l}) |
| 42 | default: |
| 43 | h = log.NewTerminalHandlerWithLevel(output, l, useColor) |
| 44 | } |
| 45 | |
| 46 | return log.NewLogger(h) |
| 47 | } |
| 48 | |
| 49 | func DefaultLogger() log.Logger { |
| 50 | return NewLogger(defaultCLIConfig) |
no test coverage detected