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

Downloader: Remove verbose flag (#464)

* Remove verbose flag from downloader.

* Do structured http logging in forwarder, too.

* Use structured logging to separate http traffic of downloader from forwarder.
This commit is contained in:
Sascha L. Teichmann 2023-09-27 11:30:24 +02:00 committed by GitHub
parent 49da14d47f
commit 7cd076d4f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 166 additions and 78 deletions

33
internal/options/log.go Normal file
View file

@ -0,0 +1,33 @@
// This file is Free Software under the MIT License
// without warranty, see README.md and LICENSES/MIT.txt for details.
//
// SPDX-License-Identifier: MIT
//
// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2023 Intevation GmbH <https://intevation.de>
package options
import (
"log/slog"
"strings"
)
// LogLevel implements a helper type to be used in configurations.
type LogLevel struct{ slog.Level }
// MarshalFlag implements [flags.Marshaler].
func (ll LogLevel) MarshalFlag() (string, error) {
t, err := ll.MarshalText()
return strings.ToLower(string(t)), err
}
// UnmarshalFlag implements [flags.Unmarshaler].
func (ll *LogLevel) UnmarshalFlag(value string) error {
var l slog.Level
if err := l.UnmarshalText([]byte(value)); err != nil {
return err
}
*ll = LogLevel{Level: l}
return nil
}

View file

@ -0,0 +1,49 @@
// This file is Free Software under the MIT License
// without warranty, see README.md and LICENSES/MIT.txt for details.
//
// SPDX-License-Identifier: MIT
//
// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2023 Intevation GmbH <https://intevation.de>
package options
import (
"log/slog"
"testing"
)
func TestMarshalFlag(t *testing.T) {
ll := LogLevel{Level: slog.LevelInfo}
got, err := ll.MarshalFlag()
if err != nil {
t.Fatal(err)
}
if got != "info" {
t.Fatalf("got %q expected \"info\"", got)
}
}
func TestUnmarshalFlag(t *testing.T) {
for _, x := range []struct {
input string
expect slog.Level
}{
{input: "debug", expect: slog.LevelDebug},
{input: "info", expect: slog.LevelInfo},
{input: "warn", expect: slog.LevelWarn},
{input: "error", expect: slog.LevelError},
} {
var ll LogLevel
if err := ll.UnmarshalFlag(x.input); err != nil {
t.Fatalf("%q error: %v", x.input, err)
}
if ll.Level != x.expect {
t.Fatalf("%q: got %s expected %s", x.input, ll.Level, x.expect)
}
}
var ll LogLevel
if err := ll.UnmarshalFlag("invalid"); err == nil {
t.Fatal(`"invalid" should return an error`)
}
}