even more colors

This commit is contained in:
Roberto Hidalgo 2023-03-20 00:34:09 -06:00
parent eea360d7d6
commit 7fab6d66d8
1 changed files with 22 additions and 2 deletions

View File

@ -15,13 +15,25 @@ type Formatter struct {
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) { func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
prefix := "" prefix := ""
colorEnabled := runtime.ColorEnabled() colorEnabled := runtime.ColorEnabled()
message := entry.Message
if runtime.VerboseEnabled() { if runtime.VerboseEnabled() {
date := strings.Replace(entry.Time.Local().Format(time.DateTime), " ", "T", 1) date := strings.Replace(entry.Time.Local().Format(time.DateTime), " ", "T", 1)
component := "" component := ""
if c, ok := entry.Data[componentKey]; ok { if c, ok := entry.Data[componentKey]; ok {
component = " " + c.(string) component = " " + c.(string)
} }
prefix = fmt.Sprintf("\033[2m%s %s%s\033[0m\t", date, entry.Level.String(), component) level := entry.Level.String()
if colorEnabled {
if entry.Level <= logrus.ErrorLevel {
level = "\033[31m\033[1m" + level + "\033[0m"
} else if entry.Level == logrus.WarnLevel {
level = "\033[33m\033[1m" + level + "\033[0m"
} else if entry.Level >= logrus.DebugLevel && colorEnabled {
message = "\033[2m" + message + "\033[0m"
}
}
prefix = fmt.Sprintf("\033[2m%s %s%s\033[0m\t", date, level, component)
} else if entry.Level == logrus.ErrorLevel { } else if entry.Level == logrus.ErrorLevel {
if colorEnabled { if colorEnabled {
prefix = "\033[41m\033[1m ERROR \033[0m " prefix = "\033[41m\033[1m ERROR \033[0m "
@ -31,9 +43,17 @@ func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
} else if entry.Level == logrus.WarnLevel { } else if entry.Level == logrus.WarnLevel {
if colorEnabled { if colorEnabled {
prefix = "\033[43m\033[31m warning \033[0m " prefix = "\033[43m\033[31m warning \033[0m "
message = "\033[33m" + message + "\033[0m"
} else { } else {
prefix = "WARNING: " prefix = "WARNING: "
} }
} else if entry.Level >= logrus.DebugLevel {
if colorEnabled {
prefix = "\033[2m" + entry.Level.String() + ":\033[0m "
message = "\033[2m" + message + "\033[0m"
} else {
prefix = strings.ToUpper(entry.Level.String()) + ": "
} }
return []byte(prefix + entry.Message + "\n"), nil }
return []byte(prefix + message + "\n"), nil
} }