Skip to content

Commit

Permalink
Add file and line number tracking for warn, error, and panic loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
fossyy committed Sep 21, 2024
1 parent 3cc0322 commit 2d6a22a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"io"
"log"
"os"
"path/filepath"
"runtime"
"time"
)

Expand Down Expand Up @@ -46,30 +48,33 @@ func Logger() *AggregatedLogger {

slug := log.Ldate | log.Ltime
infoLogger := log.New(infoLoggerWriter, "INFO: ", slug)
warnLogger := log.New(multiLogger, "WARN: ", slug)
errorLogger := log.New(multiLogger, "ERROR: ", slug)
panicLogger := log.New(multiLogger, "PANIC: ", slug)

return &AggregatedLogger{
infoLogger: infoLogger,
warnLogger: warnLogger,
errorLogger: errorLogger,
panicLogger: panicLogger,
warnLogger: log.New(multiLogger, "WARN: ", slug),
errorLogger: log.New(multiLogger, "ERROR: ", slug),
panicLogger: log.New(multiLogger, "PANIC: ", slug),
}
}

func getFileAndLine() string {
_, file, line, _ := runtime.Caller(2)
filename := filepath.Base(file)
return fmt.Sprintf("%s:%d", filename, line)
}

func (l *AggregatedLogger) Info(v ...interface{}) {
l.infoLogger.Println(v...)
}

func (l *AggregatedLogger) Warn(v ...interface{}) {
l.warnLogger.Println(v...)
l.warnLogger.Println(getFileAndLine(), fmt.Sprint(v...))
}

func (l *AggregatedLogger) Error(v ...interface{}) {
l.errorLogger.Println(v...)
l.errorLogger.Println(getFileAndLine(), fmt.Sprint(v...))
}

func (l *AggregatedLogger) Panic(v ...interface{}) {
l.panicLogger.Panic(v...)
l.panicLogger.Panic(getFileAndLine(), fmt.Sprint(v...))
}

0 comments on commit 2d6a22a

Please sign in to comment.