forked from lmittmann/tint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom levels and colorizing
Users via the `LevelColorsMap` can now map their (custom) slog.Level => name and color. Fixes: lmittmann#61
- Loading branch information
1 parent
368de75
commit 6c24e0b
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package tint | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/fatih/color" | ||
) | ||
|
||
// LevelColors defines the name as displayed to the user and color of a log level. | ||
type LevelColor struct { | ||
// Name is the name of the log level | ||
Name string | ||
// Color is the color of the log level | ||
Color color.Attribute | ||
serialized string | ||
colored bool | ||
} | ||
|
||
// Level returns the level name, optionally with color applied. | ||
func (lc *LevelColor) Level(colored bool) string { | ||
if len(lc.serialized) == 0 || lc.colored != colored { | ||
if colored { | ||
lc.serialized = color.New(lc.Color).SprintFunc()(lc.Name) | ||
} else { | ||
lc.serialized = lc.Name | ||
} | ||
} | ||
return lc.serialized | ||
} | ||
|
||
// Copy returns a copy of the LevelColor. | ||
func (lc *LevelColor) Copy() *LevelColor { | ||
return &LevelColor{ | ||
Name: lc.Name, | ||
Color: lc.Color, | ||
serialized: lc.serialized, | ||
colored: lc.colored, | ||
} | ||
} | ||
|
||
// LevelColorsMapping is a map of log levels to their colors and is what | ||
// the user defines in their configuration. | ||
type LevelColorsMapping map[slog.Level]LevelColor | ||
|
||
// min returns the mapped minimum index | ||
func (lm *LevelColorsMapping) min() int { | ||
idx := 1000 | ||
for check, _ := range *lm { | ||
if int(check) < idx { | ||
idx = int(check) | ||
} | ||
} | ||
return idx | ||
} | ||
|
||
// size returns the size of the slice needed to store the LevelColors | ||
func (lm *LevelColorsMapping) size(offset int) int { | ||
maxIdx := -1000 | ||
for check, _ := range *lm { | ||
if int(check) > maxIdx { | ||
maxIdx = int(check) | ||
} | ||
} | ||
return offset + maxIdx + 1 | ||
} | ||
|
||
func (lm *LevelColorsMapping) offset() int { | ||
min := lm.min() | ||
if min < 0 { | ||
min = -min | ||
} | ||
return min | ||
} | ||
|
||
func (lm *LevelColorsMapping) LevelColors() *LevelColors { | ||
lcList := make([]*LevelColor, lm.size(lm.offset())) | ||
for idx, lc := range *lm { | ||
lcList[int(idx)+lm.offset()] = lc.Copy() | ||
} | ||
lc := LevelColors{ | ||
levels: lcList, | ||
offset: lm.offset(), | ||
} | ||
return &lc | ||
} | ||
|
||
// LevelColors is our internal representation of the user-defined LevelColorsMapping. | ||
// We map the log levels via their slog.Level to their LevelColor using an offset | ||
// to ensure we can map negative level values to our slice. | ||
type LevelColors struct { | ||
levels []*LevelColor | ||
offset int | ||
} | ||
|
||
// LevelColor returns the LevelColor for the given log level or nil | ||
// indicating if the log level was not found. | ||
func (lc *LevelColors) LevelColor(level slog.Level) *LevelColor { | ||
idx := int(level.Level()) + lc.offset | ||
if len(lc.levels) < idx { | ||
return &LevelColor{} | ||
} | ||
return lc.levels[idx] | ||
} | ||
|
||
// Copy returns a copy of the LevelColors. | ||
func (lc *LevelColors) Copy() *LevelColors { | ||
lcCopy := LevelColors{ | ||
levels: make([]*LevelColor, len(lc.levels)), | ||
offset: lc.offset, | ||
} | ||
copy(lcCopy.levels, lc.levels) | ||
return &lcCopy | ||
} |