From a0e01b97a4e501524c6802f6d8deeb2d4d99f57a Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Sat, 21 Dec 2024 00:30:10 +0500 Subject: [PATCH 1/4] 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...) } } From a19ecba519b0eb8ecc0806479a0d25143acae7b4 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Mon, 23 Dec 2024 05:33:18 +0500 Subject: [PATCH 2/4] update uuid respective to https request --- utils/logger.go | 83 ++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/utils/logger.go b/utils/logger.go index 7178f5c86..768b689d2 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -1,81 +1,78 @@ package utils import ( + "context" "github.com/google/uuid" "log" + "net/http" "os" "strings" "sync" ) +type contextKey string + type Logger struct { - infoLogger *log.Logger - warningLogger *log.Logger - errorLogger *log.Logger - debugLogger *log.Logger - machineLogger *log.Logger - logLevel string - mu sync.Mutex - requestUUID string + infoLogger *log.Logger + warningLogger *log.Logger + errorLogger *log.Logger + debugLogger *log.Logger + machineLogger *log.Logger + logLevel string + mu sync.Mutex + requestUUIDKey contextKey } var Log = Logger{ - infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile), - warningLogger: log.New(os.Stdout, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile), - errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), - debugLogger: log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile), - machineLogger: log.New(os.Stdout, "MACHINE: ", log.Ldate|log.Ltime|log.Lshortfile), - logLevel: strings.ToUpper(os.Getenv("LOG_LEVEL")), + infoLogger: log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile), + warningLogger: log.New(os.Stdout, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile), + errorLogger: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), + debugLogger: log.New(os.Stdout, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile), + machineLogger: log.New(os.Stdout, "MACHINE: ", log.Ldate|log.Ltime|log.Lshortfile), + logLevel: strings.ToUpper(os.Getenv("LOG_LEVEL")), + requestUUIDKey: contextKey("requestUUID"), } -func (l *Logger) SetRequestUUID() { - l.mu.Lock() - defer l.mu.Unlock() - l.requestUUID = uuid.NewString() +func (l *Logger) RequestUUIDMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestUUID := uuid.NewString() + ctx := context.WithValue(r.Context(), l.requestUUIDKey, requestUUID) + next.ServeHTTP(w, r.WithContext(ctx)) + }) } -func (l *Logger) ClearRequestUUID() { - l.mu.Lock() - defer l.mu.Unlock() - l.requestUUID = "" +func (l *Logger) getRequestUUID(ctx context.Context) string { + if uuid, ok := ctx.Value(l.requestUUIDKey).(string); ok { + return uuid + } + return "no request uuid" } -func (l *Logger) logWithPrefix(logger *log.Logger, format string, v ...interface{}) { - l.mu.Lock() - - requestUUID := l.requestUUID - l.mu.Unlock() +func (l *Logger) logWithPrefix(ctx context.Context, logger *log.Logger, format string, v ...interface{}) { + requestUUID := l.getRequestUUID(ctx) logger.Printf("["+requestUUID+"] "+format, v...) } -func (l *Logger) Info(format string, v ...interface{}) { - l.SetRequestUUID() - defer l.ClearRequestUUID() +func (l *Logger) Info(ctx context.Context, format string, v ...interface{}) { if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" { - l.logWithPrefix(l.infoLogger, format, v...) + l.logWithPrefix(ctx, l.infoLogger, format, v...) } } -func (l *Logger) Debug(format string, v ...interface{}) { - l.SetRequestUUID() - defer l.ClearRequestUUID() +func (l *Logger) Debug(ctx context.Context, format string, v ...interface{}) { if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" { - l.logWithPrefix(l.debugLogger, format, v...) + l.logWithPrefix(ctx, l.debugLogger, format, v...) } } -func (l *Logger) Warning(format string, v ...interface{}) { - l.SetRequestUUID() - defer l.ClearRequestUUID() +func (l *Logger) Warning(ctx context.Context, format string, v ...interface{}) { if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" { - l.logWithPrefix(l.warningLogger, format, v...) + l.logWithPrefix(ctx, l.warningLogger, format, v...) } } -func (l *Logger) Error(format string, v ...interface{}) { - l.SetRequestUUID() - defer l.ClearRequestUUID() +func (l *Logger) Error(ctx context.Context, format string, v ...interface{}) { if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" || l.logLevel == "ERROR" { - l.logWithPrefix(l.errorLogger, format, v...) + l.logWithPrefix(ctx, l.errorLogger, format, v...) } } From e3e21bef5df3a76bf56f600e539d56dbbf1ecd48 Mon Sep 17 00:00:00 2001 From: MahtabBukhari Date: Mon, 23 Dec 2024 20:49:28 +0500 Subject: [PATCH 3/4] unique uuid for route base --- routes/index.go | 1 + utils/logger.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/routes/index.go b/routes/index.go index 58e085611..9f17d1a73 100644 --- a/routes/index.go +++ b/routes/index.go @@ -231,6 +231,7 @@ func initChi() *chi.Mux { r.Use(middleware.RequestID) r.Use(middleware.Logger) r.Use(middleware.Recoverer) + r.Use(utils.RouteBasedUUIDMiddleware) r.Use(internalServerErrorHandler) cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, diff --git a/utils/logger.go b/utils/logger.go index 54ec48168..37529eede 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -1,8 +1,11 @@ package utils import ( + "crypto/sha1" + "encoding/hex" "github.com/stakwork/sphinx-tribes/config" "log" + "net/http" "os" "sync" ) @@ -37,6 +40,23 @@ func (l *Logger) ClearRequestUUID() { l.requestUUID = "" } +func RouteBasedUUIDMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + routeIdentifier := r.Method + ":" + r.URL.Path + + hash := sha1.New() + hash.Write([]byte(routeIdentifier)) + uuid := hex.EncodeToString(hash.Sum(nil)) + + Log.SetRequestUUID(uuid) + + defer Log.ClearRequestUUID() + + next.ServeHTTP(w, r) + }) +} + func (l *Logger) logWithPrefix(logger *log.Logger, format string, v ...interface{}) { l.mu.Lock() From 3ed5b0df2cc968fa973faba2b9d494cafaab7e69 Mon Sep 17 00:00:00 2001 From: kevkevinpal Date: Mon, 23 Dec 2024 11:24:26 -0500 Subject: [PATCH 4/4] removed route based uuid and made it unique based on request --- utils/logger.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/utils/logger.go b/utils/logger.go index 37529eede..a0ba76c0a 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -1,8 +1,7 @@ package utils import ( - "crypto/sha1" - "encoding/hex" + "github.com/google/uuid" "github.com/stakwork/sphinx-tribes/config" "log" "net/http" @@ -42,13 +41,7 @@ func (l *Logger) ClearRequestUUID() { func RouteBasedUUIDMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - - routeIdentifier := r.Method + ":" + r.URL.Path - - hash := sha1.New() - hash.Write([]byte(routeIdentifier)) - uuid := hex.EncodeToString(hash.Sum(nil)) - + uuid := uuid.NewString() Log.SetRequestUUID(uuid) defer Log.ClearRequestUUID()