From efdcbc9f7b7f0a0856554f2b9dba04c831d695cb Mon Sep 17 00:00:00 2001 From: Syed Mahtab Ali <98140673+MahtabBukhari@users.noreply.github.com> Date: Thu, 19 Dec 2024 18:29:02 +0500 Subject: [PATCH] Create a logger util (#2225) * Create a logger util * modified the logger so we dont need to instantiate first --------- Co-authored-by: kevkevinpal --- routes/index.go | 5 +++-- utils/logger.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 utils/logger.go diff --git a/routes/index.go b/routes/index.go index e1a80aeef..15ef89e72 100644 --- a/routes/index.go +++ b/routes/index.go @@ -22,6 +22,7 @@ import ( "github.com/stakwork/sphinx-tribes/utils" ) + // NewRouter creates a chi router func NewRouter() *http.Server { r := initChi() @@ -205,7 +206,7 @@ func internalServerErrorHandler(next http.Handler) http.Handler { // Format stack trace to edge list edgeList := utils.FormatStacktraceToEdgeList(stackTrace, err) - fmt.Printf("Internal Server Error: %s %s\nError: %v\nStack Trace:\n%s\nEdge List:\n%+v\n", + utils.Log.Error("Internal Server Error: %s %s\nError: %v\nStack Trace:\n%s\nEdge List:\n%+v\n", r.Method, r.URL.Path, err, @@ -215,7 +216,7 @@ func internalServerErrorHandler(next http.Handler) http.Handler { go func() { if err := sendEdgeListToJarvis(edgeList); err != nil { - fmt.Printf("Error sending to Jarvis: %v\n", err) + utils.Log.Error("Error sending to Jarvis: %v\n", err) } }() diff --git a/utils/logger.go b/utils/logger.go new file mode 100644 index 000000000..15c61b0b6 --- /dev/null +++ b/utils/logger.go @@ -0,0 +1,30 @@ +package utils + +import ( + "log" + "os" +) + +type Logger struct { + infoLogger *log.Logger + errorLogger *log.Logger + debugLogger *log.Logger +} + +var Log = Logger{ + infoLogger: log.New(os.Stdout, "INFO: ", 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), +} + +func (l *Logger) Info(format string, v ...interface{}) { + l.infoLogger.Printf(format, v...) +} + +func (l *Logger) Error(format string, v ...interface{}) { + l.errorLogger.Printf(format, v...) +} + +func (l *Logger) Debug(format string, v ...interface{}) { + l.debugLogger.Printf(format, v...) +}