Skip to content

Commit

Permalink
unique uuid for each request in log (stakwork#2261)
Browse files Browse the repository at this point in the history
* unique uuid for each request in log

* update uuid respective to https request

* unique uuid for route base

* removed route based uuid and made it unique based on request

---------

Co-authored-by: kevkevinpal <[email protected]>
  • Loading branch information
MahtabBukhari and kevkevinpal authored Dec 23, 2024
1 parent 6d877d2 commit 11b820b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{"*"},
Expand Down
54 changes: 47 additions & 7 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package utils

import (
"github.com/google/uuid"
"github.com/stakwork/sphinx-tribes/config"
"log"
"net/http"
"os"

"github.com/stakwork/sphinx-tribes/config"
"sync"
)

type Logger struct {
Expand All @@ -13,6 +15,8 @@ type Logger struct {
errorLogger *log.Logger
debugLogger *log.Logger
machineLogger *log.Logger
mu sync.Mutex
requestUUID string
}

var Log = Logger{
Expand All @@ -23,32 +27,68 @@ var Log = Logger{
machineLogger: log.New(os.Stdout, "MACHINE: ", log.Ldate|log.Ltime|log.Lshortfile),
}

func (l *Logger) SetRequestUUID(uuidString string) {
l.mu.Lock()
defer l.mu.Unlock()
l.requestUUID = uuidString
}

func (l *Logger) ClearRequestUUID() {
l.mu.Lock()
defer l.mu.Unlock()
l.requestUUID = ""
}

func RouteBasedUUIDMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
uuid := uuid.NewString()
Log.SetRequestUUID(uuid)

defer Log.ClearRequestUUID()

next.ServeHTTP(w, r)
})
}

func (l *Logger) logWithPrefix(logger *log.Logger, format string, v ...interface{}) {
l.mu.Lock()

requestUUID := l.requestUUID
l.mu.Unlock()

if requestUUID == "" {
logger.Printf(format, v...)
} else {
logger.Printf("["+requestUUID+"] "+format, v...)
}
}

func (l *Logger) Machine(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" {
l.machineLogger.Printf(format, v...)
l.logWithPrefix(l.machineLogger, format, v...)
}
}

func (l *Logger) Debug(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" {
l.debugLogger.Printf(format, v...)
l.logWithPrefix(l.debugLogger, format, v...)
}
}

func (l *Logger) Info(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" {
l.infoLogger.Printf(format, v...)
l.logWithPrefix(l.infoLogger, format, v...)
}
}

func (l *Logger) Warning(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" {
l.warningLogger.Printf(format, v...)
l.logWithPrefix(l.warningLogger, format, v...)
}
}

func (l *Logger) Error(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" || config.LogLevel == "ERROR" {
l.errorLogger.Printf(format, v...)
l.logWithPrefix(l.errorLogger, format, v...)
}
}

0 comments on commit 11b820b

Please sign in to comment.