From a0e01b97a4e501524c6802f6d8deeb2d4d99f57a Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Sat, 21 Dec 2024 00:30:10 +0500 Subject: [PATCH] unique uuid for each request in log --- utils/logger.go | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/utils/logger.go b/utils/logger.go index e32d9f06f..7178f5c86 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -1,9 +1,11 @@ package utils import ( + "github.com/google/uuid" "log" "os" "strings" + "sync" ) type Logger struct { @@ -13,6 +15,8 @@ type Logger struct { debugLogger *log.Logger machineLogger *log.Logger logLevel string + mu sync.Mutex + requestUUID string } var Log = Logger{ @@ -24,32 +28,54 @@ var Log = Logger{ logLevel: strings.ToUpper(os.Getenv("LOG_LEVEL")), } -func (l *Logger) Machine(format string, v ...interface{}) { - if l.logLevel == "MACHINE" { - l.machineLogger.Printf(format, v...) - } +func (l *Logger) SetRequestUUID() { + l.mu.Lock() + defer l.mu.Unlock() + l.requestUUID = uuid.NewString() } -func (l *Logger) Debug(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" { - l.debugLogger.Printf(format, v...) - } +func (l *Logger) ClearRequestUUID() { + l.mu.Lock() + defer l.mu.Unlock() + l.requestUUID = "" +} + +func (l *Logger) logWithPrefix(logger *log.Logger, format string, v ...interface{}) { + l.mu.Lock() + + requestUUID := l.requestUUID + l.mu.Unlock() + logger.Printf("["+requestUUID+"] "+format, v...) } func (l *Logger) Info(format string, v ...interface{}) { + l.SetRequestUUID() + defer l.ClearRequestUUID() if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" { - l.infoLogger.Printf(format, v...) + l.logWithPrefix(l.infoLogger, format, v...) + } +} + +func (l *Logger) Debug(format string, v ...interface{}) { + l.SetRequestUUID() + defer l.ClearRequestUUID() + if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" { + l.logWithPrefix(l.debugLogger, format, v...) } } func (l *Logger) Warning(format string, v ...interface{}) { + l.SetRequestUUID() + defer l.ClearRequestUUID() if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" { - l.warningLogger.Printf(format, v...) + l.logWithPrefix(l.warningLogger, format, v...) } } func (l *Logger) Error(format string, v ...interface{}) { + l.SetRequestUUID() + defer l.ClearRequestUUID() if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" || l.logLevel == "ERROR" { - l.errorLogger.Printf(format, v...) + l.logWithPrefix(l.errorLogger, format, v...) } }