Skip to content

Commit 8ef5050

Browse files
committed
make the debug logs a bit easier to read
1 parent 93deea7 commit 8ef5050

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

internal/cmd/logger.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func init() {
1515
logLevel = "info" // default to info level
1616
}
1717

18-
// upcase the log level
18+
// Upcase the log level
1919
logLevel = strings.ToUpper(logLevel)
2020

2121
var level slog.Level
@@ -33,11 +33,26 @@ func init() {
3333
}
3434

3535
opts := &slog.HandlerOptions{
36-
Level: level,
36+
Level: level,
37+
AddSource: true, // Include source file and line number for context
38+
ReplaceAttr: humanFriendlyAttrs, // Customize log attributes for readability
3739
}
3840

3941
handler := slog.NewTextHandler(os.Stdout, opts)
4042

4143
// Configure slog with the specified log level
4244
Logger = slog.New(handler)
4345
}
46+
47+
// humanFriendlyAttrs customizes log attributes for human readability
48+
func humanFriendlyAttrs(groups []string, a slog.Attr) slog.Attr {
49+
switch a.Key {
50+
case slog.TimeKey:
51+
// Format time as a human-readable string
52+
a.Value = slog.StringValue(a.Value.Time().Format("2006-01-02 15:04:05"))
53+
case slog.LevelKey:
54+
// Capitalize log levels for better visibility
55+
a.Value = slog.StringValue(strings.ToUpper(a.Value.String()))
56+
}
57+
return a
58+
}

0 commit comments

Comments
 (0)