Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-use logger instance (#2029) #2031

Merged
merged 1 commit into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions cmd/aws-k8s-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,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()
Expand Down
11 changes: 3 additions & 8 deletions pkg/utils/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions pkg/utils/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down