From fc85ba05f774dffe392cb223450cdf067dddd6cb Mon Sep 17 00:00:00 2001 From: in-void Date: Mon, 6 Feb 2023 23:00:30 +0100 Subject: [PATCH] fix logger concurrent map iteration --- internal/common/logging/logger.go | 32 +++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/internal/common/logging/logger.go b/internal/common/logging/logger.go index dba4ca4..f00abd5 100644 --- a/internal/common/logging/logger.go +++ b/internal/common/logging/logger.go @@ -41,38 +41,55 @@ func WithDefaultField(key, value string) *logrus.Logger { } func Info(args ...interface{}) { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + customLogger.WithFields(defaultFields).Info(args) } func Error(args ...interface{}) { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + customLogger.WithFields(defaultFields).Error(args) } func Warning(args ...interface{}) { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + customLogger.WithFields(defaultFields).Warning(args) } -func Debug(args ...interface{}) { - customLogger.WithFields(defaultFields).Debug(args) -} - func Fatal(args ...interface{}) { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + customLogger.WithFields(defaultFields).Fatal(args) } func WithField(key string, value interface{}) *logrus.Entry { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + return customLogger. - WithField(key, value). - WithFields(defaultFields) + WithFields(defaultFields). + WithField(key, value) } func WithFields(fields Fields) *logrus.Entry { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + return customLogger. WithFields(logrus.Fields(fields)). WithFields(defaultFields) } func LogCommand(command interface{}) { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + context, _ := json.Marshal(command) commandName := reflect.TypeOf(command).Elem().Name() @@ -89,6 +106,9 @@ func LogCommand(command interface{}) { } func LogCommandFailed(command interface{}, err error) { + defaultFieldsMutex.Lock() + defer defaultFieldsMutex.Unlock() + commandName := reflect.TypeOf(command).Elem().Name() customLogger.