From 0b77db0b90304889105a0a46717605df13eb72b5 Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Thu, 22 Feb 2024 16:40:15 -0500 Subject: [PATCH] Replace logger with slogger in flag controller, subcommands, signal listener, metadata recorder --- cmd/launcher/doctor.go | 17 ++++++-- cmd/launcher/flare.go | 28 ++++++++---- cmd/launcher/internal/record_metadata.go | 35 +++++++++------ cmd/launcher/launcher.go | 6 +-- cmd/launcher/run_socket.go | 9 ++-- cmd/launcher/signal_listener.go | 15 ++++--- cmd/launcher/signal_listener_test.go | 4 +- ee/agent/flags/flag_controller.go | 50 +++++++++++++--------- ee/agent/flags/flag_controller_test.go | 13 +++--- ee/agent/flags/flag_value_duration.go | 22 ++++++---- ee/agent/flags/flag_value_duration_test.go | 4 +- ee/agent/flags/flag_value_float64.go | 22 ++++++---- ee/agent/flags/flag_value_float64_test.go | 4 +- ee/agent/flags/flag_value_int.go | 22 ++++++---- ee/agent/flags/flag_value_int_test.go | 4 +- 15 files changed, 154 insertions(+), 101 deletions(-) diff --git a/cmd/launcher/doctor.go b/cmd/launcher/doctor.go index 4500dcd74..080167aac 100644 --- a/cmd/launcher/doctor.go +++ b/cmd/launcher/doctor.go @@ -2,14 +2,14 @@ package main import ( "context" + "log/slog" "os" - "github.com/go-kit/kit/log" - "github.com/kolide/kit/logutil" "github.com/kolide/launcher/ee/agent/flags" "github.com/kolide/launcher/ee/agent/knapsack" "github.com/kolide/launcher/ee/debug/checkups" "github.com/kolide/launcher/pkg/launcher" + "github.com/kolide/launcher/pkg/log/multislogger" ) func runDoctor(args []string) error { @@ -27,8 +27,17 @@ func runDoctor(args []string) error { } fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)} - logger := log.With(logutil.NewCLILogger(true), "caller", log.DefaultCaller) - flagController := flags.NewFlagController(logger, nil, fcOpts...) + + slogLevel := slog.LevelInfo + if opts.Debug { + slogLevel = slog.LevelDebug + } + + slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + Level: slogLevel, + })).Logger + + flagController := flags.NewFlagController(slogger, nil, fcOpts...) k := knapsack.New(nil, flagController, nil, nil, nil) w := os.Stdout //tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', tabwriter.AlignRight) diff --git a/cmd/launcher/flare.go b/cmd/launcher/flare.go index 1ba299cab..9c60d55fe 100644 --- a/cmd/launcher/flare.go +++ b/cmd/launcher/flare.go @@ -5,12 +5,11 @@ import ( "flag" "fmt" "io" + "log/slog" "os" "path/filepath" "strings" - "github.com/go-kit/kit/log" - "github.com/go-kit/kit/log/level" "github.com/kolide/kit/ulid" "github.com/kolide/launcher/ee/agent/flags" "github.com/kolide/launcher/ee/agent/knapsack" @@ -18,6 +17,7 @@ import ( "github.com/kolide/launcher/ee/debug/checkups" "github.com/kolide/launcher/ee/debug/shipper" "github.com/kolide/launcher/pkg/launcher" + "github.com/kolide/launcher/pkg/log/multislogger" "github.com/peterbourgon/ff/v3" ) @@ -49,9 +49,17 @@ func runFlare(args []string) error { return err } - logger := log.NewLogfmtLogger(os.Stdout) + slogLevel := slog.LevelInfo + if opts.Debug { + slogLevel = slog.LevelDebug + } + + slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + Level: slogLevel, + })).Logger + fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)} - flagController := flags.NewFlagController(logger, inmemory.NewStore(), fcOpts...) + flagController := flags.NewFlagController(slogger, inmemory.NewStore(), fcOpts...) k := knapsack.New(nil, flagController, nil, nil, nil) ctx := context.Background() @@ -70,7 +78,7 @@ func runFlare(args []string) error { return err } flareDest = shipper - successMessage = "Flare uploaded successfully" + successMessage = "flare uploaded successfully" case "local": reportName := fmt.Sprintf("kolide_agent_flare_report_%s.zip", ulid.New()) reportPath := filepath.Join(*flOutputDir, reportName) @@ -81,16 +89,20 @@ func runFlare(args []string) error { } defer flareFile.Close() flareDest = flareFile - successMessage = "Flare saved locally" + successMessage = "flare saved locally" default: return fmt.Errorf(`invalid save option: %s, expected "local" or "upload"`, *flSave) - } if err := checkups.RunFlare(ctx, k, flareDest, checkups.StandaloneEnviroment); err != nil { return err } - level.Info(logger).Log("msg", successMessage, "file", flareDest.Name()) + slogger.Log(ctx, slog.LevelInfo, + "flare creation complete", + "status", successMessage, + "file", flareDest.Name(), + ) + return nil } diff --git a/cmd/launcher/internal/record_metadata.go b/cmd/launcher/internal/record_metadata.go index e7a594d04..987480463 100644 --- a/cmd/launcher/internal/record_metadata.go +++ b/cmd/launcher/internal/record_metadata.go @@ -1,16 +1,15 @@ package internal import ( + "context" "encoding/json" "errors" - "fmt" + "log/slog" "os" "path/filepath" "runtime" "time" - "github.com/go-kit/kit/log" - "github.com/go-kit/kit/log/level" "github.com/groob/plist" "github.com/kolide/kit/version" "github.com/kolide/launcher/ee/agent/types" @@ -21,8 +20,8 @@ type ( // subsystem. whenever new data is received, it will rewrite the metadata.json // and metadata.plist files to our root install directory metadataWriter struct { - logger log.Logger - k types.Knapsack + slogger *slog.Logger + k types.Knapsack } metadata struct { DeviceId string `json:"device_id" plist:"device_id"` @@ -33,21 +32,27 @@ type ( } ) -func NewMetadataWriter(logger log.Logger, k types.Knapsack) *metadataWriter { +func NewMetadataWriter(slogger *slog.Logger, k types.Knapsack) *metadataWriter { return &metadataWriter{ - logger: logger, - k: k, + slogger: slogger.With("component", "metadata_writer"), + k: k, } } func (mw *metadataWriter) Ping() { metadata := newMetadataTemplate() if err := mw.populateLatestServerData(metadata); err != nil { - level.Debug(mw.logger).Log("msg", "unable to collect latest server data, metadata files will be incomplete", "err", err) + mw.slogger.Log(context.TODO(), slog.LevelDebug, + "unable to collect latest server data, metadata files will be incomplete", + "err", err, + ) } if err := mw.recordMetadata(metadata); err != nil { - level.Debug(mw.logger).Log("msg", "unable to write out metadata files", "err", err) + mw.slogger.Log(context.TODO(), slog.LevelDebug, + "unable to write out metadata files", + "err", err, + ) } } @@ -106,8 +111,9 @@ func (mw *metadataWriter) recordMetadata(metadata *metadata) error { func (mw *metadataWriter) getServerDataValue(store types.GetterSetterDeleterIteratorUpdater, key string) string { val, err := store.Get([]byte(key)) if err != nil { - level.Debug(mw.logger).Log( - "msg", fmt.Sprintf("unable to collect value for %s from server_data, will re-attempt on next update", key), + mw.slogger.Log(context.TODO(), slog.LevelDebug, + "unable to collect value for key from server_data, will re-attempt on next update", + "key", key, "err", err, ) @@ -115,8 +121,9 @@ func (mw *metadataWriter) getServerDataValue(store types.GetterSetterDeleterIter } if string(val) == "" { - level.Debug(mw.logger).Log( - "msg", fmt.Sprintf("server_data was missing value for %s, will re-attempt on next update", key), + mw.slogger.Log(context.TODO(), slog.LevelDebug, + "server_data was missing value for key, will re-attempt on next update", + "key", key, "err", err, ) diff --git a/cmd/launcher/launcher.go b/cmd/launcher/launcher.go index 77541cfa7..0edef1fb1 100644 --- a/cmd/launcher/launcher.go +++ b/cmd/launcher/launcher.go @@ -186,7 +186,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl } fcOpts := []flags.Option{flags.WithCmdLineOpts(opts)} - flagController := flags.NewFlagController(logger, stores[storage.AgentFlagsStore], fcOpts...) + flagController := flags.NewFlagController(slogger, stores[storage.AgentFlagsStore], fcOpts...) k := knapsack.New(stores, flagController, db, multiSlogger, systemMultiSlogger) go runOsqueryVersionCheck(ctx, slogger, k.LatestOsquerydPath(ctx)) @@ -275,7 +275,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl sigChannel := make(chan os.Signal, 1) // Add a rungroup to catch things on the sigChannel - signalListener := newSignalListener(sigChannel, cancel, logger) + signalListener := newSignalListener(sigChannel, cancel, slogger) runGroup.Add("sigChannel", signalListener.Execute, signalListener.Interrupt) // For now, remediation is not performed -- we only log the hardware change. @@ -450,7 +450,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl controlService.RegisterSubscriber(authTokensSubsystemName, traceExporter) } - if metadataWriter := internal.NewMetadataWriter(logger, k); metadataWriter == nil { + if metadataWriter := internal.NewMetadataWriter(slogger, k); metadataWriter == nil { slogger.Log(ctx, slog.LevelDebug, "unable to set up metadata writer", "err", err, diff --git a/cmd/launcher/run_socket.go b/cmd/launcher/run_socket.go index dcef8bf8f..737ecff48 100644 --- a/cmd/launcher/run_socket.go +++ b/cmd/launcher/run_socket.go @@ -3,12 +3,12 @@ package main import ( "flag" "fmt" + "log/slog" "os" "os/signal" "path/filepath" "syscall" - "github.com/go-kit/kit/log" "github.com/kolide/kit/env" "github.com/kolide/kit/fsutil" "github.com/kolide/launcher/ee/agent" @@ -16,6 +16,7 @@ import ( "github.com/kolide/launcher/ee/agent/knapsack" "github.com/kolide/launcher/ee/agent/storage/inmemory" "github.com/kolide/launcher/pkg/launcher" + "github.com/kolide/launcher/pkg/log/multislogger" "github.com/kolide/launcher/pkg/osquery/runtime" "github.com/kolide/launcher/pkg/osquery/table" ) @@ -58,9 +59,11 @@ func runSocket(args []string) error { if err != nil { return err } - logger := log.NewLogfmtLogger(os.Stdout) + slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ + Level: slog.LevelDebug, + })).Logger fcOpts := []flags.Option{flags.WithCmdLineOpts(cmdlineopts)} - flagController := flags.NewFlagController(logger, inmemory.NewStore(), fcOpts...) + flagController := flags.NewFlagController(slogger, inmemory.NewStore(), fcOpts...) k := knapsack.New(nil, flagController, nil, nil, nil) runner := runtime.New(k, opts...) go runner.Run() diff --git a/cmd/launcher/signal_listener.go b/cmd/launcher/signal_listener.go index 3b8ada692..99de8549f 100644 --- a/cmd/launcher/signal_listener.go +++ b/cmd/launcher/signal_listener.go @@ -2,34 +2,35 @@ package main import ( "context" + "log/slog" "os" "os/signal" "syscall" - - "github.com/go-kit/kit/log" - "github.com/go-kit/kit/log/level" ) // listens for interrupts type signalListener struct { sigChannel chan os.Signal cancel context.CancelFunc - logger log.Logger + slogger *slog.Logger interrupted bool } -func newSignalListener(sigChannel chan os.Signal, cancel context.CancelFunc, logger log.Logger) *signalListener { +func newSignalListener(sigChannel chan os.Signal, cancel context.CancelFunc, slogger *slog.Logger) *signalListener { return &signalListener{ sigChannel: sigChannel, cancel: cancel, - logger: log.With(logger, "component", "signal_listener"), + slogger: slogger.With("component", "signal_listener"), } } func (s *signalListener) Execute() error { signal.Notify(s.sigChannel, os.Interrupt, syscall.SIGTERM) sig := <-s.sigChannel - level.Info(s.logger).Log("msg", "beginning shutdown via signal", "signal_received", sig) + s.slogger.Log(context.TODO(), slog.LevelInfo, + "beginning shutdown via signal", + "signal_received", sig, + ) return nil } diff --git a/cmd/launcher/signal_listener_test.go b/cmd/launcher/signal_listener_test.go index ebe66dbc7..a12ea59b4 100644 --- a/cmd/launcher/signal_listener_test.go +++ b/cmd/launcher/signal_listener_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "github.com/go-kit/kit/log" + "github.com/kolide/launcher/pkg/log/multislogger" "github.com/stretchr/testify/require" ) @@ -16,7 +16,7 @@ func TestInterrupt_Multiple(t *testing.T) { sigChannel := make(chan os.Signal, 1) _, cancel := context.WithCancel(context.TODO()) - sigListener := newSignalListener(sigChannel, cancel, log.NewNopLogger()) + sigListener := newSignalListener(sigChannel, cancel, multislogger.New().Logger) // Let the signal listener run for a bit go sigListener.Execute() diff --git a/ee/agent/flags/flag_controller.go b/ee/agent/flags/flag_controller.go index b8e41c61c..8c4a7f13b 100644 --- a/ee/agent/flags/flag_controller.go +++ b/ee/agent/flags/flag_controller.go @@ -1,13 +1,13 @@ package flags import ( + "context" "errors" + "log/slog" "strings" "sync" "time" - "github.com/go-kit/kit/log" - "github.com/go-kit/kit/log/level" "github.com/kolide/launcher/ee/agent/flags/keys" "github.com/kolide/launcher/ee/agent/types" "github.com/kolide/launcher/pkg/autoupdate" @@ -18,7 +18,7 @@ import ( // FlagController is responsible for retrieving flag values from the appropriate sources, // determining precedence, sanitizing flag values, and notifying observers of changes. type FlagController struct { - logger log.Logger + slogger *slog.Logger cmdLineOpts *launcher.Options agentFlagsStore types.KVStore overrideMutex sync.RWMutex @@ -27,9 +27,9 @@ type FlagController struct { observersMutex sync.RWMutex } -func NewFlagController(logger log.Logger, agentFlagsStore types.KVStore, opts ...Option) *FlagController { +func NewFlagController(slogger *slog.Logger, agentFlagsStore types.KVStore, opts ...Option) *FlagController { fc := &FlagController{ - logger: log.With(logger, "component", "flag_controller"), + slogger: slogger.With("component", "flag_controller"), cmdLineOpts: &launcher.Options{}, agentFlagsStore: agentFlagsStore, observers: make(map[types.FlagsChangeObserver][]keys.FlagKey), @@ -52,7 +52,11 @@ func (fc *FlagController) getControlServerValue(key keys.FlagKey) []byte { value, err := fc.agentFlagsStore.Get([]byte(key)) if err != nil { - level.Debug(fc.logger).Log("msg", "failed to get control server key", "key", key, "err", err) + fc.slogger.Log(context.TODO(), slog.LevelDebug, + "failed to get control server key", + "key", key, + "err", err, + ) return nil } @@ -67,7 +71,11 @@ func (fc *FlagController) setControlServerValue(key keys.FlagKey, value []byte) err := fc.agentFlagsStore.Set([]byte(key), value) if err != nil { - level.Debug(fc.logger).Log("msg", "failed to set control server key", "key", key, "err", err) + fc.slogger.Log(context.TODO(), slog.LevelDebug, + "failed to set control server key", + "key", key, + "err", err, + ) return err } @@ -123,8 +131,8 @@ func (fc *FlagController) overrideFlag(key keys.FlagKey, duration time.Duration, fc.overrideMutex.Lock() defer fc.overrideMutex.Unlock() - level.Info(fc.logger).Log( - "msg", "overriding flag", + fc.slogger.Log(context.TODO(), slog.LevelInfo, + "overriding flag", "key", key, "value", value, "duration", duration, @@ -207,7 +215,7 @@ func (fc *FlagController) SetLoggingInterval(interval time.Duration) error { return fc.setControlServerValue(keys.LoggingInterval, durationToBytes(interval)) } func (fc *FlagController) LoggingInterval() time.Duration { - return NewDurationFlagValue(fc.logger, keys.LoggingInterval, + return NewDurationFlagValue(fc.slogger, keys.LoggingInterval, WithDefault(fc.cmdLineOpts.LoggingInterval), WithMin(5*time.Second), WithMax(10*time.Minute), @@ -241,7 +249,7 @@ func (fc *FlagController) SetDesktopUpdateInterval(interval time.Duration) error return fc.setControlServerValue(keys.DesktopUpdateInterval, durationToBytes(interval)) } func (fc *FlagController) DesktopUpdateInterval() time.Duration { - return NewDurationFlagValue(fc.logger, keys.DesktopUpdateInterval, + return NewDurationFlagValue(fc.slogger, keys.DesktopUpdateInterval, WithDefault(5*time.Second), WithMin(5*time.Second), WithMax(10*time.Minute), @@ -252,7 +260,7 @@ func (fc *FlagController) SetDesktopMenuRefreshInterval(interval time.Duration) return fc.setControlServerValue(keys.DesktopMenuRefreshInterval, durationToBytes(interval)) } func (fc *FlagController) DesktopMenuRefreshInterval() time.Duration { - return NewDurationFlagValue(fc.logger, keys.DesktopMenuRefreshInterval, + return NewDurationFlagValue(fc.slogger, keys.DesktopMenuRefreshInterval, WithDefault(15*time.Minute), WithMin(5*time.Minute), WithMax(60*time.Minute), @@ -292,7 +300,7 @@ func (fc *FlagController) ControlRequestInterval() time.Duration { fc.overrideMutex.RLock() defer fc.overrideMutex.RUnlock() - return NewDurationFlagValue(fc.logger, keys.ControlRequestInterval, + return NewDurationFlagValue(fc.slogger, keys.ControlRequestInterval, WithOverride(fc.overrides[keys.ControlRequestInterval]), WithDefault(fc.cmdLineOpts.ControlRequestInterval), WithMin(5*time.Second), @@ -363,7 +371,7 @@ func (fc *FlagController) SetWatchdogDelaySec(sec int) error { return fc.setControlServerValue(keys.WatchdogDelaySec, intToBytes(sec)) } func (fc *FlagController) WatchdogDelaySec() int { - return NewIntFlagValue(fc.logger, keys.WatchdogDelaySec, + return NewIntFlagValue(fc.slogger, keys.WatchdogDelaySec, WithIntValueDefault(fc.cmdLineOpts.WatchdogDelaySec), WithIntValueMin(0), WithIntValueMax(600), @@ -374,7 +382,7 @@ func (fc *FlagController) SetWatchdogMemoryLimitMB(limit int) error { return fc.setControlServerValue(keys.WatchdogMemoryLimitMB, intToBytes(limit)) } func (fc *FlagController) WatchdogMemoryLimitMB() int { - return NewIntFlagValue(fc.logger, keys.WatchdogMemoryLimitMB, + return NewIntFlagValue(fc.slogger, keys.WatchdogMemoryLimitMB, WithIntValueDefault(fc.cmdLineOpts.WatchdogMemoryLimitMB), WithIntValueMin(100), WithIntValueMax(10000), // 10 GB appears to be the max that osquery will accept @@ -385,7 +393,7 @@ func (fc *FlagController) SetWatchdogUtilizationLimitPercent(limit int) error { return fc.setControlServerValue(keys.WatchdogUtilizationLimitPercent, intToBytes(limit)) } func (fc *FlagController) WatchdogUtilizationLimitPercent() int { - return NewIntFlagValue(fc.logger, keys.WatchdogUtilizationLimitPercent, + return NewIntFlagValue(fc.slogger, keys.WatchdogUtilizationLimitPercent, WithIntValueDefault(fc.cmdLineOpts.WatchdogUtilizationLimitPercent), WithIntValueMin(5), WithIntValueMax(100), @@ -441,7 +449,7 @@ func (fc *FlagController) SetAutoupdateInterval(interval time.Duration) error { return fc.setControlServerValue(keys.AutoupdateInterval, durationToBytes(interval)) } func (fc *FlagController) AutoupdateInterval() time.Duration { - return NewDurationFlagValue(fc.logger, keys.AutoupdateInterval, + return NewDurationFlagValue(fc.slogger, keys.AutoupdateInterval, WithDefault(fc.cmdLineOpts.AutoupdateInterval), WithMin(1*time.Minute), WithMax(24*time.Hour), @@ -462,7 +470,7 @@ func (fc *FlagController) SetAutoupdateInitialDelay(delay time.Duration) error { return fc.setControlServerValue(keys.AutoupdateInitialDelay, durationToBytes(delay)) } func (fc *FlagController) AutoupdateInitialDelay() time.Duration { - return NewDurationFlagValue(fc.logger, keys.AutoupdateInitialDelay, + return NewDurationFlagValue(fc.slogger, keys.AutoupdateInitialDelay, WithDefault(fc.cmdLineOpts.AutoupdateInitialDelay), WithMin(5*time.Second), WithMax(12*time.Hour), @@ -498,7 +506,7 @@ func (fc *FlagController) SetTraceSamplingRateOverride(value float64, duration t fc.overrideFlag(keys.TraceSamplingRate, duration, value) } func (fc *FlagController) TraceSamplingRate() float64 { - return NewFloat64FlagValue(fc.logger, keys.LoggingInterval, + return NewFloat64FlagValue(fc.slogger, keys.LoggingInterval, WithFloat64ValueOverride(fc.overrides[keys.TraceSamplingRate]), WithFloat64ValueDefault(fc.cmdLineOpts.TraceSamplingRate), WithFloat64ValueMin(0.0), @@ -510,7 +518,7 @@ func (fc *FlagController) SetTraceBatchTimeout(duration time.Duration) error { return fc.setControlServerValue(keys.TraceBatchTimeout, durationToBytes(duration)) } func (fc *FlagController) TraceBatchTimeout() time.Duration { - return NewDurationFlagValue(fc.logger, keys.TraceBatchTimeout, + return NewDurationFlagValue(fc.slogger, keys.TraceBatchTimeout, WithDefault(1*time.Minute), WithMin(5*time.Second), WithMax(1*time.Hour), @@ -585,7 +593,7 @@ func (fc *FlagController) SetOsqueryHealthcheckStartupDelay(delay time.Duration) return fc.setControlServerValue(keys.OsqueryHealthcheckStartupDelay, durationToBytes(delay)) } func (fc *FlagController) OsqueryHealthcheckStartupDelay() time.Duration { - return NewDurationFlagValue(fc.logger, keys.OsqueryHealthcheckStartupDelay, + return NewDurationFlagValue(fc.slogger, keys.OsqueryHealthcheckStartupDelay, WithDefault(fc.cmdLineOpts.OsqueryHealthcheckStartupDelay), WithMin(0*time.Second), WithMax(1*time.Hour), diff --git a/ee/agent/flags/flag_controller_test.go b/ee/agent/flags/flag_controller_test.go index 9501ed845..7fef8e124 100644 --- a/ee/agent/flags/flag_controller_test.go +++ b/ee/agent/flags/flag_controller_test.go @@ -11,6 +11,7 @@ import ( "github.com/kolide/launcher/ee/agent/types" "github.com/kolide/launcher/ee/agent/types/mocks" "github.com/kolide/launcher/pkg/launcher" + "github.com/kolide/launcher/pkg/log/multislogger" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -35,7 +36,7 @@ func TestControllerBoolFlags(t *testing.T) { store, err := storageci.NewStore(t, log.NewNopLogger(), storage.AgentFlagsStore.String()) require.NoError(t, err) - fc := NewFlagController(log.NewNopLogger(), store) + fc := NewFlagController(multislogger.New().Logger, store) assert.NotNil(t, fc) var value bool @@ -111,7 +112,7 @@ func TestControllerStringFlags(t *testing.T) { store, err := storageci.NewStore(t, log.NewNopLogger(), storage.AgentFlagsStore.String()) require.NoError(t, err) - fc := NewFlagController(log.NewNopLogger(), store) + fc := NewFlagController(multislogger.New().Logger, store) assert.NotNil(t, fc) var value string @@ -235,7 +236,7 @@ func TestControllerDurationFlags(t *testing.T) { store, err := storageci.NewStore(t, log.NewNopLogger(), storage.AgentFlagsStore.String()) require.NoError(t, err) - fc := NewFlagController(log.NewNopLogger(), store) + fc := NewFlagController(multislogger.New().Logger, store) assert.NotNil(t, fc) for i, valueToSet := range tt.valuesToSet { @@ -268,7 +269,7 @@ func TestControllerNotify(t *testing.T) { store, err := storageci.NewStore(t, log.NewNopLogger(), storage.AgentFlagsStore.String()) require.NoError(t, err) - fc := NewFlagController(log.NewNopLogger(), store) + fc := NewFlagController(multislogger.New().Logger, store) assert.NotNil(t, fc) mockObserver := mocks.NewFlagsChangeObserver(t) @@ -306,7 +307,7 @@ func TestControllerUpdate(t *testing.T) { store, err := storageci.NewStore(t, log.NewNopLogger(), storage.AgentFlagsStore.String()) require.NoError(t, err) - fc := NewFlagController(log.NewNopLogger(), store) + fc := NewFlagController(multislogger.New().Logger, store) assert.NotNil(t, fc) mockObserver := mocks.NewFlagsChangeObserver(t) @@ -347,7 +348,7 @@ func TestControllerOverride(t *testing.T) { store, err := storageci.NewStore(t, log.NewNopLogger(), storage.AgentFlagsStore.String()) require.NoError(t, err) - fc := NewFlagController(log.NewNopLogger(), store) + fc := NewFlagController(multislogger.New().Logger, store) assert.NotNil(t, fc) mockObserver := mocks.NewFlagsChangeObserver(t) diff --git a/ee/agent/flags/flag_value_duration.go b/ee/agent/flags/flag_value_duration.go index 436d97afd..c5f2dfa8e 100644 --- a/ee/agent/flags/flag_value_duration.go +++ b/ee/agent/flags/flag_value_duration.go @@ -1,12 +1,12 @@ package flags import ( + "context" + "log/slog" "math" "strconv" "time" - "github.com/go-kit/kit/log" - "github.com/go-kit/kit/log/level" "github.com/kolide/launcher/ee/agent/flags/keys" ) @@ -37,7 +37,7 @@ func WithMax(max time.Duration) durationOption { } type durationFlagValue struct { - logger log.Logger + slogger *slog.Logger key keys.FlagKey override FlagValueOverride defaultVal int64 @@ -45,12 +45,12 @@ type durationFlagValue struct { max int64 } -func NewDurationFlagValue(logger log.Logger, key keys.FlagKey, opts ...durationOption) *durationFlagValue { +func NewDurationFlagValue(slogger *slog.Logger, key keys.FlagKey, opts ...durationOption) *durationFlagValue { d := &durationFlagValue{ - logger: logger, - key: key, - min: math.MinInt64, - max: math.MaxInt64, + slogger: slogger, + key: key, + min: math.MinInt64, + max: math.MaxInt64, } for _, opt := range opts { @@ -69,7 +69,11 @@ func (d *durationFlagValue) get(controlServerValue []byte) time.Duration { if err == nil { int64Value = parsedInt } else { - level.Debug(d.logger).Log("msg", "failed to convert stored duration flag value", "key", d.key, "err", err) + d.slogger.Log(context.TODO(), slog.LevelDebug, + "failed to convert stored duration flag value", + "key", d.key, + "err", err, + ) } } diff --git a/ee/agent/flags/flag_value_duration_test.go b/ee/agent/flags/flag_value_duration_test.go index e51892a73..8f63acd3a 100644 --- a/ee/agent/flags/flag_value_duration_test.go +++ b/ee/agent/flags/flag_value_duration_test.go @@ -4,9 +4,9 @@ import ( "testing" "time" - "github.com/go-kit/kit/log" "github.com/kolide/launcher/ee/agent/flags/keys" "github.com/kolide/launcher/ee/agent/flags/mocks" + "github.com/kolide/launcher/pkg/log/multislogger" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -73,7 +73,7 @@ func TestFlagValueDuration(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - d := NewDurationFlagValue(log.NewNopLogger(), keys.ControlRequestInterval, tt.options...) + d := NewDurationFlagValue(multislogger.New().Logger, keys.ControlRequestInterval, tt.options...) require.NotNil(t, d) val := d.get(tt.controlServerValue) diff --git a/ee/agent/flags/flag_value_float64.go b/ee/agent/flags/flag_value_float64.go index 2215efded..90e428139 100644 --- a/ee/agent/flags/flag_value_float64.go +++ b/ee/agent/flags/flag_value_float64.go @@ -1,11 +1,11 @@ package flags import ( + "context" + "log/slog" "math" "strconv" - "github.com/go-kit/kit/log" - "github.com/go-kit/kit/log/level" "github.com/kolide/launcher/ee/agent/flags/keys" ) @@ -36,7 +36,7 @@ func WithFloat64ValueMax(max float64) float64Option { } type float64FlagValue struct { - logger log.Logger + slogger *slog.Logger key keys.FlagKey override FlagValueOverride defaultVal float64 @@ -44,12 +44,12 @@ type float64FlagValue struct { max float64 } -func NewFloat64FlagValue(logger log.Logger, key keys.FlagKey, opts ...float64Option) *float64FlagValue { +func NewFloat64FlagValue(slogger *slog.Logger, key keys.FlagKey, opts ...float64Option) *float64FlagValue { f := &float64FlagValue{ - logger: logger, - key: key, - min: -1 * math.MaxFloat64, - max: math.MaxFloat64, + slogger: slogger, + key: key, + min: -1 * math.MaxFloat64, + max: math.MaxFloat64, } for _, opt := range opts { @@ -68,7 +68,11 @@ func (f *float64FlagValue) get(controlServerValue []byte) float64 { if err == nil { float64Value = parsedFloat } else { - level.Debug(f.logger).Log("msg", "failed to convert stored float flag value", "key", f.key, "err", err) + f.slogger.Log(context.TODO(), slog.LevelDebug, + "failed to convert stored float flag value", + "key", f.key, + "err", err, + ) } } diff --git a/ee/agent/flags/flag_value_float64_test.go b/ee/agent/flags/flag_value_float64_test.go index 4d8332764..2ac678768 100644 --- a/ee/agent/flags/flag_value_float64_test.go +++ b/ee/agent/flags/flag_value_float64_test.go @@ -3,9 +3,9 @@ package flags import ( "testing" - "github.com/go-kit/kit/log" "github.com/kolide/launcher/ee/agent/flags/keys" "github.com/kolide/launcher/ee/agent/flags/mocks" + "github.com/kolide/launcher/pkg/log/multislogger" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -84,7 +84,7 @@ func TestFloat64FlagValue(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - d := NewFloat64FlagValue(log.NewNopLogger(), keys.ControlRequestInterval, tt.options...) + d := NewFloat64FlagValue(multislogger.New().Logger, keys.ControlRequestInterval, tt.options...) require.NotNil(t, d) val := d.get(tt.controlServerValue) diff --git a/ee/agent/flags/flag_value_int.go b/ee/agent/flags/flag_value_int.go index 2000d0810..010c49c71 100644 --- a/ee/agent/flags/flag_value_int.go +++ b/ee/agent/flags/flag_value_int.go @@ -1,11 +1,11 @@ package flags import ( + "context" + "log/slog" "math" "strconv" - "github.com/go-kit/kit/log" - "github.com/go-kit/kit/log/level" "github.com/kolide/launcher/ee/agent/flags/keys" ) @@ -36,7 +36,7 @@ func WithIntValueMax(max int) intOption { } type intFlagValue struct { - logger log.Logger + slogger *slog.Logger key keys.FlagKey override FlagValueOverride defaultVal int @@ -44,12 +44,12 @@ type intFlagValue struct { max int } -func NewIntFlagValue(logger log.Logger, key keys.FlagKey, opts ...intOption) *intFlagValue { +func NewIntFlagValue(slogger *slog.Logger, key keys.FlagKey, opts ...intOption) *intFlagValue { i := &intFlagValue{ - logger: logger, - key: key, - min: -1 * math.MaxInt, - max: math.MaxInt, + slogger: slogger, + key: key, + min: -1 * math.MaxInt, + max: math.MaxInt, } for _, opt := range opts { @@ -68,7 +68,11 @@ func (i *intFlagValue) get(controlServerValue []byte) int { if err == nil { intValue = parsedInt } else { - level.Debug(i.logger).Log("msg", "failed to convert stored int flag value", "key", i.key, "err", err) + i.slogger.Log(context.TODO(), slog.LevelDebug, + "failed to convert stored int flag value", + "key", i.key, + "err", err, + ) } } diff --git a/ee/agent/flags/flag_value_int_test.go b/ee/agent/flags/flag_value_int_test.go index 480307ca4..8fbcd7695 100644 --- a/ee/agent/flags/flag_value_int_test.go +++ b/ee/agent/flags/flag_value_int_test.go @@ -3,9 +3,9 @@ package flags import ( "testing" - "github.com/go-kit/kit/log" "github.com/kolide/launcher/ee/agent/flags/keys" "github.com/kolide/launcher/ee/agent/flags/mocks" + "github.com/kolide/launcher/pkg/log/multislogger" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -84,7 +84,7 @@ func TestIntFlagValue(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() - d := NewIntFlagValue(log.NewNopLogger(), keys.ControlRequestInterval, tt.options...) + d := NewIntFlagValue(multislogger.New().Logger, keys.ControlRequestInterval, tt.options...) require.NotNil(t, d) val := d.get(tt.controlServerValue)