Skip to content

Commit

Permalink
Fix time for test.
Browse files Browse the repository at this point in the history
  • Loading branch information
koenbollen committed Aug 11, 2023
1 parent aa94d2b commit 40d19d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
11 changes: 10 additions & 1 deletion examples/mocks/slog_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ import (
"io"
"log/slog"
"os"
"time"
)

func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ReplaceAttr: FixedTime}))
logger.Info("hello", "count", 3)
logger.Warn("failed", "err", io.EOF)
}

func FixedTime(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && len(groups) == 0 {
t, _ := time.Parse(time.DateTime, time.DateTime)
a.Value = slog.TimeValue(t)
}
return a
}
8 changes: 4 additions & 4 deletions examples/slog.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Go's log/slog json handler

$ slog_example
{"time":"2023-08-11T16:35:31.162712808+02:00","level":"INFO","msg":"hello","count":3}
{"time":"2023-08-11T16:35:31.162796194+02:00","level":"WARN","msg":"failed","err":"EOF"}
{"time":"2006-01-02T15:04:05Z","level":"INFO","msg":"hello","count":3}
{"time":"2006-01-02T15:04:05Z","level":"WARN","msg":"failed","err":"EOF"}

$ slog_example | jl
[2023-08-11 16:34:29] INFO: hello [count=3]
[2023-08-11 16:34:29] WARNING: failed [err=EOF]
[2006-01-02 15:04:05] INFO: hello [count=3]
[2006-01-02 15:04:05] WARNING: failed [err=EOF]

0 comments on commit 40d19d1

Please sign in to comment.