logs-fix-11
This commit is contained in:
parent
348a9426cc
commit
32f903f40a
|
@ -2,28 +2,39 @@ package logs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// LogTextFormatter is a custom log formatter for text output
|
|
||||||
type LogTextFormatter struct {
|
type LogTextFormatter struct {
|
||||||
logrus.Formatter
|
logrus.Formatter
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format helps formatting time to UTC
|
|
||||||
func (u LogTextFormatter) Format(e *logrus.Entry) ([]byte, error) {
|
func (u LogTextFormatter) Format(e *logrus.Entry) ([]byte, error) {
|
||||||
timestamp := e.Time.Format("2006-01-02 15:04:05.000")
|
|
||||||
level := strings.ToUpper(e.Level.String())
|
level := strings.ToUpper(e.Level.String())
|
||||||
message := e.Message
|
message := e.Message
|
||||||
|
|
||||||
|
var color string
|
||||||
|
switch e.Level {
|
||||||
|
case logrus.DebugLevel:
|
||||||
|
color = "\033[36m" // cyan
|
||||||
|
case logrus.InfoLevel:
|
||||||
|
color = "\033[32m" // green
|
||||||
|
case logrus.WarnLevel:
|
||||||
|
color = "\033[33m" // yellow
|
||||||
|
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
|
||||||
|
color = "\033[31m" // red
|
||||||
|
default:
|
||||||
|
color = "\033[0m" // reset
|
||||||
|
}
|
||||||
|
|
||||||
if e.Caller != nil {
|
if e.Caller != nil {
|
||||||
file := filepath.Base(e.Caller.File)
|
file := filepath.Base(e.Caller.File)
|
||||||
line := e.Caller.Line
|
line := e.Caller.Line
|
||||||
return []byte(fmt.Sprintf("%s [%s] %s:%d %s\n", timestamp, level, file, line, message)), nil
|
return []byte(fmt.Sprintf("%s[%s] %s:%d %s%s\033[0m\n", color, level, file, line, message, color)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return []byte(fmt.Sprintf("%s [%s] %s\n", timestamp, level, message)), nil
|
return []byte(fmt.Sprintf("%s[%s] %s%s\033[0m\n", color, level, message, color)), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user