-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
example_test.go
57 lines (49 loc) · 1.36 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package term_test
import (
"errors"
"os"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/term"
)
func ExampleNewLogger_redErrors() {
// Color errors red
colorFn := func(keyvals ...interface{}) term.FgBgColor {
for i := 1; i < len(keyvals); i += 2 {
if _, ok := keyvals[i].(error); ok {
return term.FgBgColor{Fg: term.White, Bg: term.Red}
}
}
return term.FgBgColor{}
}
logger := term.NewLogger(os.Stdout, log.NewLogfmtLogger, colorFn)
logger.Log("msg", "default color", "err", nil)
logger.Log("msg", "colored because of error", "err", errors.New("coloring error"))
}
func ExampleNewLogger_levelColors() {
// Color by level value
colorFn := func(keyvals ...interface{}) term.FgBgColor {
for i := 0; i < len(keyvals)-1; i += 2 {
if keyvals[i] != "level" {
continue
}
switch keyvals[i+1] {
case "debug":
return term.FgBgColor{Fg: term.DarkGray}
case "info":
return term.FgBgColor{Fg: term.Gray}
case "warn":
return term.FgBgColor{Fg: term.Yellow}
case "error":
return term.FgBgColor{Fg: term.Red}
case "crit":
return term.FgBgColor{Fg: term.Gray, Bg: term.DarkRed}
default:
return term.FgBgColor{}
}
}
return term.FgBgColor{}
}
logger := term.NewLogger(os.Stdout, log.NewJSONLogger, colorFn)
logger.Log("level", "warn", "msg", "yellow")
logger.Log("level", "debug", "msg", "dark gray")
}