Testing colormap

This commit is contained in:
Kris Crawford 2024-03-25 09:39:33 -04:00
parent c2292de89b
commit 74926d1078
4 changed files with 31 additions and 16 deletions

8
go.mod
View File

@ -1,9 +1,15 @@
module git.kcrawford.net/kcrawford/logrus
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
go 1.22

2
go.sum
View File

@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=

View File

@ -184,3 +184,10 @@ type Ext1FieldLogger interface {
Trace(args ...interface{})
Traceln(args ...interface{})
}
type ColorMap map[string]int
var colorMap = ColorMap{
"INFO": RED,
"DEBUG": BLUE,
}

View File

@ -14,11 +14,12 @@ import (
)
const (
red = 31
yellow = 33
blue = 34
cyan = 36
gray = 37
RED = 31
YELLOW = 33
BLUE = 34
CYAN = 36
GRAY = 37
GREEN = 42
)
var baseTimestamp time.Time
@ -27,13 +28,6 @@ func init() {
baseTimestamp = time.Now()
}
type ColorMap map[string]int
var colorMap = ColorMap{
"Info": red,
"Debug": blue,
}
// TextFormatter formats logs into text
type TextFormatter struct {
// Set to true to bypass checking for a TTY before outputting colors.
@ -108,6 +102,14 @@ type TextFormatter struct {
// The max length of the level text, generated dynamically on init
levelTextMaxLength int
// ColorMap allows users to control colors for logging levels.
// As an example:
// formatter := &TextFormatter{
// ColorMap: ColorMap{
// "Debug": red,
// "Info": blue,
// },
// }
ColorMap ColorMap
}
@ -240,10 +242,8 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
}
func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, data Fields, timestampFormat string) {
var levelColor int
levelColor = colorMap[entry.Level]
levelText := strings.ToUpper(entry.Level.String())
levelColor := colorMap[levelText]
if !f.DisableLevelTruncation && !f.PadLevelText {
levelText = levelText[0:4]
}