Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unique uuid for each request in log #2261

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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...)
}
}
Loading