Neatly align log columns
This commit is contained in:
27
cmd/log_format_test/main.go
Normal file
27
cmd/log_format_test/main.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"modify/logger"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Initialize logger with DEBUG level
|
||||||
|
logger.Init(logger.LevelDebug)
|
||||||
|
|
||||||
|
// Test different log levels
|
||||||
|
logger.Info("This is an info message")
|
||||||
|
logger.Debug("This is a debug message")
|
||||||
|
logger.Warning("This is a warning message")
|
||||||
|
logger.Error("This is an error message")
|
||||||
|
logger.Trace("This is a trace message (not visible at DEBUG level)")
|
||||||
|
|
||||||
|
// Test with a goroutine
|
||||||
|
logger.SafeGo(func() {
|
||||||
|
time.Sleep(10 * time.Millisecond)
|
||||||
|
logger.Info("Message from goroutine")
|
||||||
|
})
|
||||||
|
|
||||||
|
// Wait for goroutine to complete
|
||||||
|
time.Sleep(20 * time.Millisecond)
|
||||||
|
}
|
@@ -8,6 +8,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -269,9 +270,10 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
|
|||||||
if l.flag&log.Lshortfile != 0 {
|
if l.flag&log.Lshortfile != 0 {
|
||||||
file = filepath.Base(file)
|
file = filepath.Base(file)
|
||||||
}
|
}
|
||||||
caller = fmt.Sprintf("%s:%d ", file, line)
|
caller = fmt.Sprintf("%-25s ", file+":"+strconv.Itoa(line))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Format the timestamp with fixed width
|
||||||
var timeStr string
|
var timeStr string
|
||||||
if l.flag&(log.Ldate|log.Ltime|log.Lmicroseconds) != 0 {
|
if l.flag&(log.Ldate|log.Ltime|log.Lmicroseconds) != 0 {
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
@@ -283,19 +285,24 @@ func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{
|
|||||||
if l.flag&log.Lmicroseconds != 0 {
|
if l.flag&log.Lmicroseconds != 0 {
|
||||||
timeStr += fmt.Sprintf(".%06d", t.Nanosecond()/1000)
|
timeStr += fmt.Sprintf(".%06d", t.Nanosecond()/1000)
|
||||||
}
|
}
|
||||||
timeStr += " "
|
|
||||||
}
|
}
|
||||||
|
timeStr = fmt.Sprintf("%-15s ", timeStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add goroutine ID if enabled
|
// Add goroutine ID if enabled, with fixed width
|
||||||
var goroutineStr string
|
var goroutineStr string
|
||||||
if l.showGoroutine {
|
if l.showGoroutine {
|
||||||
goroutineID := GetGoroutineID()
|
goroutineID := GetGoroutineID()
|
||||||
goroutineStr = fmt.Sprintf("[g:%s] ", goroutineID)
|
goroutineStr = fmt.Sprintf("[g:%-4s] ", goroutineID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%s%s%s%s%s[%s%s%s]%s %s\n",
|
// Create a colored level indicator with both brackets colored
|
||||||
l.prefix, timeStr, caller, goroutineStr, levelColor, levelNames[level], resetColor, fields, resetColor, msg)
|
levelStr := fmt.Sprintf("%s[%s]%s", levelColor, levelNames[level], levelColor)
|
||||||
|
// Add a space after the level and before the reset color
|
||||||
|
levelColumn := fmt.Sprintf("%s %s", levelStr, resetColor)
|
||||||
|
|
||||||
|
return fmt.Sprintf("%s%s%s%s%s%s%s\n",
|
||||||
|
l.prefix, timeStr, caller, goroutineStr, levelColumn, msg, fields)
|
||||||
}
|
}
|
||||||
|
|
||||||
// log logs a message at the specified level
|
// log logs a message at the specified level
|
||||||
|
Reference in New Issue
Block a user