Skip to content

Commit 785f10a

Browse files
committed
Support customisation of default colours in unix
Add a field to LogBackend which specifies a ColorConfig If using colour, ensure that we use the value for the corresponding level in the backend's ColorConfig if specified, falling back to the default colors. Expose the colour constants found in log_nix.go so that applications can reference them in specifying their own colour config. Add a new ConvertColors func to log_nix as a convenient way of converting a list of numbers representing ansi codes into the strings needed, without having to wrap each one in colorSeq or colorSeqBold individually. Also expose ColorSeq and ColorSeqBold to enable individual levels to be easily set bold or otherwise if that behaviour is desired, however.
1 parent dfaf3df commit 785f10a

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

log_nix.go

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,38 @@ import (
1616
type color int
1717

1818
const (
19-
colorBlack = iota + 30
20-
colorRed
21-
colorGreen
22-
colorYellow
23-
colorBlue
24-
colorMagenta
25-
colorCyan
26-
colorWhite
19+
ColorBlack = iota + 30
20+
ColorRed
21+
ColorGreen
22+
ColorYellow
23+
ColorBlue
24+
ColorMagenta
25+
ColorCyan
26+
ColorWhite
2727
)
2828

2929
var (
3030
colors = []string{
31-
CRITICAL: colorSeq(colorMagenta),
32-
ERROR: colorSeq(colorRed),
33-
WARNING: colorSeq(colorYellow),
34-
NOTICE: colorSeq(colorGreen),
35-
DEBUG: colorSeq(colorCyan),
31+
CRITICAL: ColorSeq(ColorMagenta),
32+
ERROR: ColorSeq(ColorRed),
33+
WARNING: ColorSeq(ColorYellow),
34+
NOTICE: ColorSeq(ColorGreen),
35+
DEBUG: ColorSeq(ColorCyan),
3636
}
3737
boldcolors = []string{
38-
CRITICAL: colorSeqBold(colorMagenta),
39-
ERROR: colorSeqBold(colorRed),
40-
WARNING: colorSeqBold(colorYellow),
41-
NOTICE: colorSeqBold(colorGreen),
42-
DEBUG: colorSeqBold(colorCyan),
38+
CRITICAL: ColorSeqBold(ColorMagenta),
39+
ERROR: ColorSeqBold(ColorRed),
40+
WARNING: ColorSeqBold(ColorYellow),
41+
NOTICE: ColorSeqBold(ColorGreen),
42+
DEBUG: ColorSeqBold(ColorCyan),
4343
}
4444
)
4545

4646
// LogBackend utilizes the standard log module.
4747
type LogBackend struct {
48-
Logger *log.Logger
49-
Color bool
48+
Logger *log.Logger
49+
Color bool
50+
ColorConfig []string
5051
}
5152

5253
// NewLogBackend creates a new LogBackend.
@@ -57,8 +58,13 @@ func NewLogBackend(out io.Writer, prefix string, flag int) *LogBackend {
5758
// Log implements the Backend interface.
5859
func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
5960
if b.Color {
61+
col := colors[level]
62+
if len(b.ColorConfig) > int(level) && b.ColorConfig[level] != "" {
63+
col = b.ColorConfig[level]
64+
}
65+
6066
buf := &bytes.Buffer{}
61-
buf.Write([]byte(colors[level]))
67+
buf.Write([]byte(col))
6268
buf.Write([]byte(rec.Formatted(calldepth + 1)))
6369
buf.Write([]byte("\033[0m"))
6470
// For some reason, the Go logger arbitrarily decided "2" was the correct
@@ -69,11 +75,26 @@ func (b *LogBackend) Log(level Level, calldepth int, rec *Record) error {
6975
return b.Logger.Output(calldepth+2, rec.Formatted(calldepth+1))
7076
}
7177

72-
func colorSeq(color color) string {
78+
// ConvertColors takes a list of ints representing colors for log levels and
79+
// converts them into strings for ANSI color formatting
80+
func ConvertColors(colors []int, bold bool) []string {
81+
converted := []string{}
82+
for _, i := range colors {
83+
if bold {
84+
converted = append(converted, ColorSeqBold(color(i)))
85+
} else {
86+
converted = append(converted, ColorSeq(color(i)))
87+
}
88+
}
89+
90+
return converted
91+
}
92+
93+
func ColorSeq(color color) string {
7394
return fmt.Sprintf("\033[%dm", int(color))
7495
}
7596

76-
func colorSeqBold(color color) string {
97+
func ColorSeqBold(color color) string {
7798
return fmt.Sprintf("\033[%d;1m", int(color))
7899
}
79100

0 commit comments

Comments
 (0)