1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Drop time precision below seconds in log output. (#450)

This commit is contained in:
Sascha L. Teichmann 2023-08-31 17:07:34 +02:00 committed by GitHub
parent 018e0e55f7
commit 24151345f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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"
@ -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
@ -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)