mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Merge pull request #530 from oxisto/slog
Added support for structured logging in `csaf_aggregator`
This commit is contained in:
commit
617deb4c17
23 changed files with 135 additions and 91 deletions
|
|
@ -10,14 +10,14 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
|
||||
"github.com/csaf-poc/csaf_distribution/v3/csaf"
|
||||
"github.com/csaf-poc/csaf_distribution/v3/util"
|
||||
|
||||
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
||||
)
|
||||
|
||||
type processor struct {
|
||||
|
|
@ -26,6 +26,9 @@ type processor struct {
|
|||
|
||||
// remoteValidator is a globally configured remote validator.
|
||||
remoteValidator csaf.RemoteValidator
|
||||
|
||||
// log is the structured logger for the whole processor.
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
type summary struct {
|
||||
|
|
@ -48,6 +51,7 @@ type worker struct {
|
|||
dir string // Directory to store data to.
|
||||
summaries map[string][]summary // the summaries of the advisories.
|
||||
categories map[string]util.Set[string] // the categories per label.
|
||||
log *slog.Logger // the structured logger, supplied with the worker number.
|
||||
}
|
||||
|
||||
func newWorker(num int, processor *processor) *worker {
|
||||
|
|
@ -55,6 +59,7 @@ func newWorker(num int, processor *processor) *worker {
|
|||
num: num,
|
||||
processor: processor,
|
||||
expr: util.NewPathEval(),
|
||||
log: processor.log.With(slog.Int("worker", num)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -86,9 +91,10 @@ func (w *worker) locateProviderMetadata(domain string) error {
|
|||
|
||||
if w.processor.cfg.Verbose {
|
||||
for i := range lpmd.Messages {
|
||||
log.Printf(
|
||||
"Loading provider-metadata.json of %q: %s\n",
|
||||
domain, lpmd.Messages[i].Message)
|
||||
w.log.Info(
|
||||
"Loading provider-metadata.json",
|
||||
"domain", domain,
|
||||
"message", lpmd.Messages[i].Message)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -141,7 +147,7 @@ func (p *processor) removeOrphans() error {
|
|||
|
||||
fi, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Printf("error: %v\n", err)
|
||||
p.log.Error("Could not retrieve file info", "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -153,13 +159,13 @@ func (p *processor) removeOrphans() error {
|
|||
d := filepath.Join(path, entry.Name())
|
||||
r, err := filepath.EvalSymlinks(d)
|
||||
if err != nil {
|
||||
log.Printf("error: %v\n", err)
|
||||
p.log.Error("Could not evaluate symlink", "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
fd, err := os.Stat(r)
|
||||
if err != nil {
|
||||
log.Printf("error: %v\n", err)
|
||||
p.log.Error("Could not retrieve file stats", "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
@ -169,18 +175,18 @@ func (p *processor) removeOrphans() error {
|
|||
}
|
||||
|
||||
// Remove the link.
|
||||
log.Printf("removing link %s -> %s\n", d, r)
|
||||
p.log.Info("Removing link", "path", fmt.Sprintf("%s -> %s", d, r))
|
||||
if err := os.Remove(d); err != nil {
|
||||
log.Printf("error: %v\n", err)
|
||||
p.log.Error("Could not remove symlink", "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Only remove directories which are in our folder.
|
||||
if rel, err := filepath.Rel(prefix, r); err == nil &&
|
||||
rel == filepath.Base(r) {
|
||||
log.Printf("removing directory %s\n", r)
|
||||
p.log.Info("Remove directory", "path", r)
|
||||
if err := os.RemoveAll(r); err != nil {
|
||||
log.Printf("error: %v\n", err)
|
||||
p.log.Error("Could not remove directory", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue