Skip to content

Commit 145b50a

Browse files
authored
fix(promslog): always use UTC for time (#735)
@SuperQ and I have both called this out in a few places; finally remembering to fix it. Prometheus has a UTC policy for logging because UTC is also used for metrics and so they should correlate. Before: ``` === RUN TestDynamicLevels/slog_log_style time=2024-12-02T13:51:08.463-05:00 level=INFO source=slog_test.go:120 msg=info hello=world time=2024-12-02T13:51:08.463-05:00 level=DEBUG source=slog_test.go:121 msg=debug hello=world ``` After: ``` === RUN TestDynamicLevels/slog_log_style time=2024-12-04T19:51:59.813Z level=INFO source=slog_test.go:120 msg=info hello=world time=2024-12-04T19:51:59.813Z level=DEBUG source=slog_test.go:121 msg=debug hello=world ``` Signed-off-by: TJ Hoplock <[email protected]>
1 parent 39a62f7 commit 145b50a

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

promslog/slog.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,16 @@ var (
6868

6969
return a
7070
}
71-
truncateSourceAttrFunc = func(groups []string, a slog.Attr) slog.Attr {
72-
if a.Key != slog.SourceKey {
73-
return a
74-
}
75-
76-
if src, ok := a.Value.Any().(*slog.Source); ok {
71+
defaultReplaceAttrFunc = func(groups []string, a slog.Attr) slog.Attr {
72+
key := a.Key
73+
switch key {
74+
case slog.TimeKey:
75+
t := a.Value.Time()
76+
a.Value = slog.TimeValue(t.UTC())
77+
case slog.SourceKey:
78+
src, _ := a.Value.Any().(*slog.Source)
7779
a.Value = slog.StringValue(filepath.Base(src.File) + ":" + strconv.Itoa(src.Line))
80+
default:
7881
}
7982

8083
return a
@@ -178,7 +181,7 @@ func New(config *Config) *slog.Logger {
178181
logHandlerOpts := &slog.HandlerOptions{
179182
Level: config.Level.lvl,
180183
AddSource: true,
181-
ReplaceAttr: truncateSourceAttrFunc,
184+
ReplaceAttr: defaultReplaceAttrFunc,
182185
}
183186

184187
if config.Style == GoKitStyle {

0 commit comments

Comments
 (0)