diff --git a/cmd/aws-k8s-agent/main.go b/cmd/aws-k8s-agent/main.go index 1fde848113..b914657d65 100644 --- a/cmd/aws-k8s-agent/main.go +++ b/cmd/aws-k8s-agent/main.go @@ -30,11 +30,7 @@ func main() { func _main() int { //Do not add anything before initializing logger - logConfig := logger.Configuration{ - LogLevel: logger.GetLogLevel(), - LogLocation: logger.GetLogLocation(), - } - log := logger.New(&logConfig) + log := logger.Get() log.Infof("Starting L-IPAMD %s ...", version.Version) version.RegisterMetric() diff --git a/pkg/utils/logger/logger.go b/pkg/utils/logger/logger.go index 40b22e9a7b..f59ed67987 100644 --- a/pkg/utils/logger/logger.go +++ b/pkg/utils/logger/logger.go @@ -47,22 +47,17 @@ type Logger interface { // Get returns an default instance of the zap logger func Get() Logger { - var logf = &structuredLogger{} - if logf.isEmpty() { + if log == nil { logConfig := LoadLogConfig() log = New(logConfig) - return log + log.Info("Initialized new logger as an existing instance was not found") } - log = logf return log } -func (logf *structuredLogger) isEmpty() bool { - return logf.zapLogger == nil -} - //New logger initializes logger func New(inputLogConfig *Configuration) Logger { log = inputLogConfig.newZapLogger() + log.Info("Constructed new logger instance") return log } diff --git a/pkg/utils/logger/logger_test.go b/pkg/utils/logger/logger_test.go index 0e93ab5888..5b085e2ccd 100644 --- a/pkg/utils/logger/logger_test.go +++ b/pkg/utils/logger/logger_test.go @@ -30,6 +30,21 @@ func TestEnvLogFilePath(t *testing.T) { assert.Equal(t, path, GetLogLocation()) } +func TestLoggerGetSameInstance(t *testing.T) { + log1 := Get() + log2 := Get() + + assert.True(t, log1 == log2) +} + +func TestLoggerNewAndGetSameInstance(t *testing.T) { + logConfig := LoadLogConfig() + log1 := New(logConfig) + log2 := Get() + + assert.True(t, log1 == log2) +} + func TestGetLogFileLocationReturnsDefaultPath(t *testing.T) { defaultPath := "/host/var/log/aws-routed-eni/ipamd.log" assert.Equal(t, defaultPath, GetLogLocation())