Skip to content

Commit

Permalink
Merge pull request #22 from MGTheTrain/refactor/logger-integration
Browse files Browse the repository at this point in the history
ensure thread safety when returning logger instance
  • Loading branch information
MGTheTrain authored Dec 16, 2024
2 parents 001e343 + cc34c2f commit d83cc74
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions internal/infrastructure/logger/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ func (l *FileLogger) Panic(args ...interface{}) {
var (
// Singleton logger instance, shared across the application
loggerInstance Logger
loggerOnce sync.Once // Guarantees that the logger is created only once
loggerOnce sync.Once // Guarantees that the logger is created only once
loggerMutex sync.Mutex // Ensures thread-safe access to the logger instance
)

// GetLogger returns a singleton logger instance, shared across the application.
Expand All @@ -113,7 +114,10 @@ func GetLogger(settings *settings.LoggerSettings) (Logger, error) {
}
})

// If the loggerInstance is already created, just return it
// Lock access to loggerInstance to ensure thread safety when returning it
loggerMutex.Lock()
defer loggerMutex.Unlock()

if loggerInstance != nil {
return loggerInstance, nil
}
Expand Down

0 comments on commit d83cc74

Please sign in to comment.