Skip to content

Commit

Permalink
Drop time precision below seconds in log output. (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
s-l-teichmann authored Aug 31, 2023
1 parent 018e0e5 commit 2415134
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmd/csaf_downloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"os"
"path/filepath"
"time"

"github.com/csaf-poc/csaf_distribution/v2/internal/certs"
"github.com/csaf-poc/csaf_distribution/v2/internal/filter"
Expand Down Expand Up @@ -196,6 +197,15 @@ func (cfg *config) prepareDirectory() error {
return nil
}

// dropSubSeconds drops all parts below resolution of seconds.
func dropSubSeconds(_ []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey {
t := a.Value.Time()
a.Value = slog.TimeValue(t.Truncate(time.Second))
}
return a
}

// prepareLogging sets up the structured logging.
func (cfg *config) prepareLogging() error {
var w io.Writer
Expand All @@ -218,7 +228,8 @@ func (cfg *config) prepareLogging() error {
}
ho := slog.HandlerOptions{
//AddSource: true,
Level: cfg.LogLevel.slogLevel(),
Level: cfg.LogLevel.slogLevel(),
ReplaceAttr: dropSubSeconds,
}
handler := slog.NewJSONHandler(w, &ho)
logger := slog.New(handler)
Expand Down

0 comments on commit 2415134

Please sign in to comment.