Skip to content

Commit

Permalink
added slog-multi + rewritten code to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
miampf committed Feb 19, 2024
1 parent 5aa03ac commit 2b237d5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
16 changes: 16 additions & 0 deletions bazel/toolchains/go_module_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4435,6 +4435,22 @@ def go_dependencies():
sum = "h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=",
version = "v1.0.0",
)
go_repository(
name = "com_github_samber_lo",
build_file_generation = "on",
build_file_proto_mode = "disable_global",
importpath = "github.com/samber/lo",
sum = "h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=",
version = "v1.38.1",
)
go_repository(
name = "com_github_samber_slog_multi",
build_file_generation = "on",
build_file_proto_mode = "disable_global",
importpath = "github.com/samber/slog-multi",
sum = "h1:6BVH9uHGAsiGkbbtQgAOQJMpKgV8unMrHhhJaw+X1EQ=",
version = "v1.0.2",
)
go_repository(
name = "com_github_sassoftware_relic",
build_file_generation = "on",
Expand Down
1 change: 1 addition & 0 deletions cli/internal/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ go_library(
"@com_github_google_uuid//:uuid",
"@com_github_mattn_go_isatty//:go-isatty",
"@com_github_rogpeppe_go_internal//diff",
"@com_github_samber_slog_multi//:slog-multi",
"@com_github_siderolabs_talos_pkg_machinery//config/encoder",
"@com_github_spf13_afero//:afero",
"@com_github_spf13_cobra//:cobra",
Expand Down
40 changes: 33 additions & 7 deletions cli/internal/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"fmt"
"io"
"io/fs"
"log/slog"
"net"
"os"
"path/filepath"
"slices"
"strings"
Expand All @@ -38,6 +40,7 @@ import (
"github.com/edgelesssys/constellation/v2/internal/kms/uri"
"github.com/edgelesssys/constellation/v2/internal/semver"
"github.com/edgelesssys/constellation/v2/internal/versions"
"github.com/samber/slog-multi"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -224,7 +227,10 @@ func runApply(cmd *cobra.Command, _ []string) error {
}

fileHandler := file.NewHandler(afero.NewOsFs())
logger := debugFileLogger{fileHandler: fileHandler, log: log}
logger, err := newDebugFileLogger(cmd, fileHandler)
if err != nil {
return err
}

newDialer := func(validator atls.Validator) *dialer.Dialer {
return dialer.New(nil, validator, &net.Dialer{})
Expand Down Expand Up @@ -860,13 +866,33 @@ type imageFetcher interface {
) (string, error)
}

type debugFileLogger struct {
fileHandler file.Handler
log debugLog
func newDebugFileLogger(cmd *cobra.Command, fileHandler file.Handler) (debugLog, error) {
logLvl := slog.LevelInfo
debugLog, err := cmd.Flags().GetBool("debug")
if err != nil {
return nil, err
}
if debugLog {
logLvl = slog.LevelDebug
}

fileWriter := &fileWriter{
fileHandler: fileHandler,
}
return slog.New(
slogmulti.Fanout(
slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{AddSource: true, Level: logLvl}), // first handler: stderr at log level
slog.NewJSONHandler(fileWriter, &slog.HandlerOptions{AddSource: true, Level: slog.LevelDebug}), // second handler: debug JSON log to file
),
), nil
}

func (l debugFileLogger) Debug(msg string, args ...any) {
l.log.Debug(msg, args...)
type fileWriter struct {
fileHandler file.Handler
}

_ = l.fileHandler.Write(constants.CLIDebugLogFile, []byte(msg+"\n"), file.OptAppend)
// Write satisfies the io.Writer interface by writing a message to file.
func (l *fileWriter) Write(msg []byte) (int, error) {
err := l.fileHandler.Write(constants.CLIDebugLogFile, msg, file.OptAppend)
return len(msg), err
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/regclient/regclient v0.5.5
github.com/rogpeppe/go-internal v1.11.0
github.com/samber/slog-multi v1.0.2
github.com/schollz/progressbar/v3 v3.13.1
github.com/siderolabs/talos/pkg/machinery v1.4.6
github.com/sigstore/rekor v1.2.2
Expand Down Expand Up @@ -152,6 +153,8 @@ require (
sigs.k8s.io/yaml v1.4.0
)

require github.com/samber/lo v1.38.1 // indirect

require (
cloud.google.com/go v0.110.8 // indirect
cloud.google.com/go/iam v1.1.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,10 @@ github.com/rubenv/sql-migrate v1.5.2 h1:bMDqOnrJVV/6JQgQ/MxOpU+AdO8uzYYA/TxFUBzF
github.com/rubenv/sql-migrate v1.5.2/go.mod h1:H38GW8Vqf8F0Su5XignRyaRcbXbJunSWxs+kmzlg0Is=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/slog-multi v1.0.2 h1:6BVH9uHGAsiGkbbtQgAOQJMpKgV8unMrHhhJaw+X1EQ=
github.com/samber/slog-multi v1.0.2/go.mod h1:uLAvHpGqbYgX4FSL0p1ZwoLuveIAJvBECtE07XmYvFo=
github.com/sassoftware/relic v7.2.1+incompatible h1:Pwyh1F3I0r4clFJXkSI8bOyJINGqpgjJU3DYAZeI05A=
github.com/sassoftware/relic v7.2.1+incompatible/go.mod h1:CWfAxv73/iLZ17rbyhIEq3K9hs5w6FpNMdUT//qR+zk=
github.com/sassoftware/relic/v7 v7.5.5 h1:2ZUM6ovo3STCAp0hZnO9nQY9lOB8OyfneeYIi4YUxMU=
Expand Down

0 comments on commit 2b237d5

Please sign in to comment.