File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ func init() {
15
15
logLevel = "info" // default to info level
16
16
}
17
17
18
- // upcase the log level
18
+ // Upcase the log level
19
19
logLevel = strings .ToUpper (logLevel )
20
20
21
21
var level slog.Level
@@ -33,11 +33,26 @@ func init() {
33
33
}
34
34
35
35
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
37
39
}
38
40
39
41
handler := slog .NewTextHandler (os .Stdout , opts )
40
42
41
43
// Configure slog with the specified log level
42
44
Logger = slog .New (handler )
43
45
}
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
+ }
You can’t perform that action at this time.
0 commit comments