Add custom level color functionality to TextFormatter

This commit is contained in:
Randy 2023-10-23 14:36:24 +02:00
parent dd1b4c2e81
commit c8f3704d51
2 changed files with 87 additions and 7 deletions

View File

@ -319,6 +319,40 @@ environment if your application has that.
Note: If you want different log levels for global (`log.SetLevel(...)`) and syslog logging, please check the [syslog hook README](hooks/syslog/README.md#different-log-levels-for-local-and-remote-logging). Note: If you want different log levels for global (`log.SetLevel(...)`) and syslog logging, please check the [syslog hook README](hooks/syslog/README.md#different-log-levels-for-local-and-remote-logging).
#### Level colors
The `TextFormatter` level colors itself can be customized as follows:
Individual overrides:
```go
log.SetFormatter(&log.TextFormatter{
LevelColors: log.DefaultLevelColors(&log.LevelColors{
Debug: 31, // Red
Default: 36, // Blue
}),
ForceColors: true, // Optional
})
```
Or you can provide a complete `log.LevelColors` struct:
```go
log.SetFormatter(&log.TextFormatter{
LevelColors: &log.LevelColors{
Trace: 37, // Gray
Debug: 37, // Gray
Warn: 33, // Yellow
Error: 31, // Red
Fatal: 31, // Red
Panic: 31, // Red
Info: 36, // Blue
Default: 36, // Blue
},
ForceColors: true, // Optional
})
```
#### Entries #### Entries
Besides the fields added with `WithField` or `WithFields` some fields are Besides the fields added with `WithField` or `WithFields` some fields are

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"reflect"
"runtime" "runtime"
"sort" "sort"
"strconv" "strconv"
@ -26,8 +27,43 @@ func init() {
baseTimestamp = time.Now() baseTimestamp = time.Now()
} }
type LevelColors struct {
Trace, Debug, Warn, Error, Fatal, Panic, Info, Default int
}
func DefaultLevelColors(custom *LevelColors) *LevelColors {
levelColors := &LevelColors{
Trace: gray,
Debug: gray,
Warn: yellow,
Error: red,
Fatal: red,
Panic: red,
Info: blue,
Default: blue,
}
if custom != nil {
dstVal := reflect.ValueOf(levelColors).Elem()
srcVal := reflect.ValueOf(custom).Elem()
for i := 0; i < dstVal.NumField(); i++ {
dstField := dstVal.Field(i)
srcField := srcVal.Field(i)
if dstField.Kind() == reflect.Int && srcField.Int() != 0 {
dstField.SetInt(srcField.Int())
}
}
}
return levelColors
}
// TextFormatter formats logs into text // TextFormatter formats logs into text
type TextFormatter struct { type TextFormatter struct {
// Set level colors
LevelColors *LevelColors
// Set to true to bypass checking for a TTY before outputting colors. // Set to true to bypass checking for a TTY before outputting colors.
ForceColors bool ForceColors bool
@ -230,18 +266,28 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
} }
func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) { func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) {
if f.LevelColors == nil {
f.LevelColors = DefaultLevelColors(nil)
}
var levelColor int var levelColor int
switch entry.Level { switch entry.Level {
case DebugLevel, TraceLevel: case DebugLevel:
levelColor = gray levelColor = f.LevelColors.Debug
case TraceLevel:
levelColor = f.LevelColors.Trace
case WarnLevel: case WarnLevel:
levelColor = yellow levelColor = f.LevelColors.Warn
case ErrorLevel, FatalLevel, PanicLevel: case ErrorLevel:
levelColor = red levelColor = f.LevelColors.Error
case FatalLevel:
levelColor = f.LevelColors.Fatal
case PanicLevel:
levelColor = f.LevelColors.Panic
case InfoLevel: case InfoLevel:
levelColor = blue levelColor = f.LevelColors.Info
default: default:
levelColor = blue levelColor = f.LevelColors.Default
} }
levelText := strings.ToUpper(entry.Level.String()) levelText := strings.ToUpper(entry.Level.String())