From 8969849086f1da070e547f0ec69434442752ae73 Mon Sep 17 00:00:00 2001 From: "M.Abubakar" <156602406+saithsab877@users.noreply.github.com> Date: Mon, 23 Dec 2024 03:05:39 +0500 Subject: [PATCH 1/2] refactor: replace fmt.Print with structured logger (#2259) --- utils/helpers.go | 13 ++++++------- utils/twitter.go | 2 +- utils/workflow_processor.go | 3 ++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/utils/helpers.go b/utils/helpers.go index 2b51ef58c..5785643e7 100644 --- a/utils/helpers.go +++ b/utils/helpers.go @@ -3,7 +3,6 @@ package utils import ( "crypto/rand" "encoding/base32" - "fmt" "strconv" "strings" "time" @@ -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] } @@ -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 } @@ -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 } @@ -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 @@ -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 } @@ -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()) } diff --git a/utils/twitter.go b/utils/twitter.go index d1a94fca4..b74a0b641 100644 --- a/utils/twitter.go +++ b/utils/twitter.go @@ -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 } diff --git a/utils/workflow_processor.go b/utils/workflow_processor.go index f820f24be..e3da5bade 100644 --- a/utils/workflow_processor.go +++ b/utils/workflow_processor.go @@ -3,6 +3,7 @@ package utils import ( "encoding/json" "fmt" + "github.com/google/uuid" ) @@ -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 } From 7d1fee91fdd5a738485ea23b5b2c7f45d8e512e5 Mon Sep 17 00:00:00 2001 From: kevkevin Date: Sun, 22 Dec 2024 18:35:05 -0600 Subject: [PATCH 2/2] fix: logger to print by config val (#2267) --- config/config.go | 2 +- utils/logger.go | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 860c66005..a1662fe49 100644 --- a/config/config.go +++ b/config/config.go @@ -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) diff --git a/utils/logger.go b/utils/logger.go index e32d9f06f..46616f016 100644 --- a/utils/logger.go +++ b/utils/logger.go @@ -3,7 +3,8 @@ package utils import ( "log" "os" - "strings" + + "github.com/stakwork/sphinx-tribes/config" ) type Logger struct { @@ -12,7 +13,6 @@ type Logger struct { errorLogger *log.Logger debugLogger *log.Logger machineLogger *log.Logger - logLevel string } var Log = Logger{ @@ -21,35 +21,34 @@ var Log = Logger{ 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")), } func (l *Logger) Machine(format string, v ...interface{}) { - if l.logLevel == "MACHINE" { + if config.LogLevel == "MACHINE" { l.machineLogger.Printf(format, v...) } } func (l *Logger) Debug(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" { l.debugLogger.Printf(format, v...) } } func (l *Logger) Info(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" { l.infoLogger.Printf(format, v...) } } func (l *Logger) Warning(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" { l.warningLogger.Printf(format, v...) } } func (l *Logger) Error(format string, v ...interface{}) { - if l.logLevel == "MACHINE" || l.logLevel == "DEBUG" || l.logLevel == "INFO" || l.logLevel == "WARNING" || l.logLevel == "ERROR" { + if config.LogLevel == "MACHINE" || config.LogLevel == "DEBUG" || config.LogLevel == "INFO" || config.LogLevel == "WARNING" || config.LogLevel == "ERROR" { l.errorLogger.Printf(format, v...) } }