Make little better logging

This commit is contained in:
2025-03-27 18:51:22 +01:00
parent 9a70c9696e
commit e847e5c3ce
3 changed files with 187 additions and 34 deletions

View File

@@ -212,7 +212,25 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
var caller string
if l.flag&log.Lshortfile != 0 || l.flag&log.Llongfile != 0 {
_, file, line, ok := runtime.Caller(3 + l.callerOffset)
// Find the actual caller by scanning up the stack
// until we find a function outside the logger package
var file string
var line int
var ok bool
// Start at a reasonable depth and scan up to 10 frames
for depth := 4; depth < 15; depth++ {
_, file, line, ok = runtime.Caller(depth)
if !ok {
break
}
// If the caller is not in the logger package, we found our caller
if !strings.Contains(file, "logger/logger.go") {
break
}
}
if !ok {
file = "???"
line = 0
@@ -239,7 +257,7 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
}
}
return fmt.Sprintf("%s%s%s%s[%s%s%s]%s %s\n",
return fmt.Sprintf("%s%14s%-30s%s[%s%s%s]%s %s\n",
l.prefix, timeStr, caller, levelColor, levelNames[level], resetColor, fields, resetColor, msg)
}