Skip to content

Commit

Permalink
Create a logger util (stakwork#2225)
Browse files Browse the repository at this point in the history
* Create a logger util

* modified the logger so we dont need to instantiate first

---------

Co-authored-by: kevkevinpal <[email protected]>
  • Loading branch information
MahtabBukhari and kevkevinpal authored Dec 19, 2024
1 parent 48986e8 commit efdcbc9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stakwork/sphinx-tribes/utils"
)


// NewRouter creates a chi router
func NewRouter() *http.Server {
r := initChi()
Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
}()

Expand Down
30 changes: 30 additions & 0 deletions utils/logger.go
Original file line number Diff line number Diff line change
@@ -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...)
}

0 comments on commit efdcbc9

Please sign in to comment.