Skip to content

Commit

Permalink
update log with uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtabBukhari committed Dec 23, 2024
2 parents a19ecba + 7d1fee9 commit 0743215
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 56 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func InitConfig() {
V2BotUrl = os.Getenv("V2_BOT_URL")
V2BotToken = os.Getenv("V2_BOT_TOKEN")
FfWebsocket = os.Getenv("FF_WEBSOCKET") == "true"
LogLevel = os.Getenv("LOG_LEVEL")
LogLevel = strings.ToUpper(os.Getenv("LOG_LEVEL"))

// Add to super admins
SuperAdmins = StripSuperAdmins(AdminStrings)
Expand Down
13 changes: 6 additions & 7 deletions utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package utils
import (
"crypto/rand"
"encoding/base32"
"fmt"
"strconv"
"strings"
"time"
Expand All @@ -15,7 +14,7 @@ func GetRandomToken(length int) string {
randomBytes := make([]byte, 32)
_, err := rand.Read(randomBytes)
if err != nil {
fmt.Println("Random token erorr ==", err)
Log.Error("Random token error: %v", err)
}
return base32.StdEncoding.EncodeToString(randomBytes)[:length]
}
Expand All @@ -24,7 +23,7 @@ func ConvertStringToUint(number string) (uint, error) {
numberParse, err := strconv.ParseUint(number, 10, 32)

if err != nil {
fmt.Println("could not parse string to uint")
Log.Error("could not parse string to uint: %v", err)
return 0, err
}

Expand All @@ -35,7 +34,7 @@ func ConvertStringToInt(number string) (int, error) {
numberParse, err := strconv.ParseInt(number, 10, 32)

if err != nil {
fmt.Println("could not parse string to uint")
Log.Error("could not parse string to int: %v", err)
return 0, err
}

Expand All @@ -46,7 +45,7 @@ func GetInvoiceAmount(paymentRequest string) uint {
decodedInvoice, err := decodepay.Decodepay(paymentRequest)

if err != nil {
fmt.Println("Could not Decode Invoice", err)
Log.Error("Could not Decode Invoice: %v", err)
return 0
}
amountInt := decodedInvoice.MSatoshi / 1000
Expand All @@ -58,7 +57,7 @@ func GetInvoiceAmount(paymentRequest string) uint {
func GetInvoiceExpired(paymentRequest string) bool {
decodedInvoice, err := decodepay.Decodepay(paymentRequest)
if err != nil {
fmt.Println("Could not Decode Invoice", err)
Log.Error("Could not Decode Invoice: %v", err)
return false
}

Expand All @@ -83,7 +82,7 @@ func ConvertTimeToTimestamp(date string) int {

t, err := time.Parse(format, dateTouse)
if err != nil {
fmt.Println("Parse string to timestamp", err)
Log.Error("Parse string to timestamp: %v", err)
} else {
return int(t.Unix())
}
Expand Down
95 changes: 49 additions & 46 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -1,78 +1,81 @@
package utils

import (
"context"
"github.com/google/uuid"
"github.com/stakwork/sphinx-tribes/config"
"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
requestUUIDKey contextKey
infoLogger *log.Logger
warningLogger *log.Logger
errorLogger *log.Logger
debugLogger *log.Logger
machineLogger *log.Logger
mu sync.Mutex
requestUUID string
}

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")),
requestUUIDKey: contextKey("requestUUID"),
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),
}

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

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
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...)
}
return "no request uuid"
}

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) Machine(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" {
l.logWithPrefix(l.machineLogger, format, v...)
}
}

func (l *Logger) Info(ctx context.Context, format string, v ...interface{}) {
if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" {
l.logWithPrefix(ctx, l.infoLogger, format, v...)
func (l *Logger) Debug(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" {
l.logWithPrefix(l.debugLogger, format, v...)
}
}

func (l *Logger) Debug(ctx context.Context, format string, v ...interface{}) {
if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" {
l.logWithPrefix(ctx, l.debugLogger, format, v...)
func (l *Logger) Info(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" {
l.logWithPrefix(l.infoLogger, format, v...)
}
}

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(ctx, l.warningLogger, format, v...)
func (l *Logger) Warning(format string, v ...interface{}) {
if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" {
l.logWithPrefix(l.warningLogger, format, v...)
}
}

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(ctx, l.errorLogger, 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.logWithPrefix(l.errorLogger, format, v...)
}
}
2 changes: 1 addition & 1 deletion utils/twitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func ConfirmIdentityTweet(username string) (string, error) {
if err != nil {
return "", err
}
// fmt.Println("tok", token)
Log.Info("Twitter verification token: %s", token)
pubkey, err := auth.VerifyArbitrary(token, "Sphinx Verification")
return pubkey, err
}
Expand Down
3 changes: 2 additions & 1 deletion utils/workflow_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"encoding/json"
"fmt"

"github.com/google/uuid"
)

Expand Down Expand Up @@ -32,6 +33,6 @@ func lookupProcessingConfig(source string) error {
}

func processWithHandler(requestID string) (string, error) {
fmt.Println("Processing with default handler")
Log.Info("Processing with default handler")
return fmt.Sprintf("Processed with default handler, RequestID: %s", requestID), nil
}

0 comments on commit 0743215

Please sign in to comment.