Target comments by Chris Hines
Tamás Gulácsi
8 years ago
4 | 4 | "fmt" |
5 | 5 | "io" |
6 | 6 | "reflect" |
7 | "sync" | |
7 | 8 | ) |
8 | 9 | |
9 | 10 | type jsonLogger struct { |
10 | 11 | io.Writer |
12 | mu sync.RWMutex | |
11 | 13 | } |
12 | 14 | |
13 | 15 | // NewJSONLogger returns a Logger that encodes keyvals to the Writer as a |
14 | 16 | // single JSON object. |
15 | 17 | func NewJSONLogger(w io.Writer) Logger { |
16 | return &jsonLogger{w} | |
18 | return &jsonLogger{Writer: w} | |
17 | 19 | } |
18 | 20 | |
19 | 21 | func (l *jsonLogger) Log(keyvals ...interface{}) error { |
27 | 29 | } |
28 | 30 | merge(m, k, v) |
29 | 31 | } |
30 | return json.NewEncoder(l.Writer).Encode(m) | |
32 | l.mu.RLock() | |
33 | w := l.Writer | |
34 | l.mu.RUnlock() | |
35 | return json.NewEncoder(w).Encode(m) | |
31 | 36 | } |
32 | 37 | |
33 | 38 | func merge(dst map[string]interface{}, k, v interface{}) { |
75 | 80 | } |
76 | 81 | |
77 | 82 | func (l *jsonLogger) Hijack(f func(io.Writer) io.Writer) { |
83 | l.mu.Lock() | |
78 | 84 | l.Writer = f(l.Writer) |
85 | l.mu.Unlock() | |
79 | 86 | } |
21 | 21 | // Hijacker allows accessing the Logger's underlying io.Writer with Hijack. |
22 | 22 | type Hijacker interface { |
23 | 23 | // Hijack gets the Logger's underlying Writer, and replaces it with the returned one. |
24 | // The implementor must ensure safe concurrent calls! | |
24 | 25 | Hijack(func(io.Writer) io.Writer) |
25 | 26 | } |
26 | 27 |
1 | 1 | |
2 | 2 | import ( |
3 | 3 | "io" |
4 | "sync" | |
4 | 5 | |
5 | 6 | "gopkg.in/logfmt.v0" |
6 | 7 | ) |
7 | 8 | |
8 | 9 | type logfmtLogger struct { |
9 | w io.Writer | |
10 | w io.Writer | |
11 | mu sync.RWMutex | |
10 | 12 | } |
11 | 13 | |
12 | 14 | // NewLogfmtLogger returns a logger that encodes keyvals to the Writer in |
13 | 15 | // logfmt format. The passed Writer must be safe for concurrent use by |
14 | 16 | // multiple goroutines if the returned Logger will be used concurrently. |
15 | 17 | func NewLogfmtLogger(w io.Writer) Logger { |
16 | return &logfmtLogger{w} | |
18 | return &logfmtLogger{w: w} | |
17 | 19 | } |
18 | 20 | |
19 | 21 | func (l logfmtLogger) Log(keyvals ...interface{}) error { |
26 | 28 | return err |
27 | 29 | } |
28 | 30 | b = append(b, '\n') |
29 | if _, err := l.w.Write(b); err != nil { | |
31 | l.mu.RLock() | |
32 | w := l.w | |
33 | l.mu.RUnlock() | |
34 | if _, err := w.Write(b); err != nil { | |
30 | 35 | return err |
31 | 36 | } |
32 | 37 | return nil |
33 | 38 | } |
34 | 39 | |
35 | 40 | func (l *logfmtLogger) Hijack(f func(io.Writer) io.Writer) { |
41 | l.mu.Lock() | |
36 | 42 | l.w = f(l.w) |
43 | l.mu.Unlock() | |
37 | 44 | } |
30 | 30 | return c.Fg == NoColor && c.Bg == NoColor |
31 | 31 | } |
32 | 32 | |
33 | type ColorOption struct { | |
34 | Key FgBgColor | |
35 | Value func(interface{}) FgBgColor | |
36 | } | |
37 | ||
38 | 33 | var _ = log.Logger((*colorLogger)(nil)) |
39 | 34 | |
40 | // NewColorLogger returns a log.Logger which prouces nice colored logs. | |
35 | // NewColorLogger returns a log.Logger which produces nice colored logs. | |
41 | 36 | // It colors whole records based on the FgBgColor returned by the color function. |
42 | 37 | // |
43 | 38 | // For example for such a function, see LevelColor. |
60 | 55 | // logger.Log("c", "c is uncolored value", "err", nil) |
61 | 56 | // logger.Log("c", "c is colored 'cause err colors it", "err", errors.New("coloring error")) |
62 | 57 | func NewColorLogger(logger log.Logger, color func(keyvals ...interface{}) FgBgColor) log.Logger { |
58 | // FIXME(tgulacsi): I don't understand why can't I use log.Hijacker here... | |
59 | hj, ok := logger.(interface { | |
60 | Hijack(func(io.Writer) io.Writer) | |
61 | }) | |
62 | if !ok { | |
63 | return logger | |
64 | } | |
63 | 65 | cl := &colorLogger{ |
64 | 66 | logger: logger, |
65 | 67 | } |
66 | if hj, ok := logger.(log.Hijacker); ok { | |
67 | cl.color = color // otherwise, no coloring is possible! | |
68 | hj.Hijack(func(w io.Writer) io.Writer { | |
69 | cl.w = w | |
70 | return cl | |
71 | }) | |
72 | } | |
68 | cl.color = color // otherwise, no coloring is possible! | |
69 | hj.Hijack(func(w io.Writer) io.Writer { | |
70 | cl.w = w | |
71 | return cl | |
72 | }) | |
73 | 73 | return cl |
74 | 74 | } |
75 | 75 | |
77 | 77 | logger log.Logger |
78 | 78 | color func(keyvals ...interface{}) FgBgColor |
79 | 79 | w io.Writer |
80 | mu sync.RWMutex // protects w | |
80 | 81 | |
81 | 82 | actColor FgBgColor |
82 | 83 | actColorMu sync.RWMutex // protects actColor |
118 | 119 | return n + m, err |
119 | 120 | } |
120 | 121 | n += m |
121 | m, err = l.w.Write([]byte("\x1b[0m")) | |
122 | l.mu.RLock() | |
123 | w := l.w | |
124 | l.mu.RUnlock() | |
125 | m, err = w.Write([]byte("\x1b[0m")) | |
122 | 126 | return n + m, err |
123 | 127 | } |
124 | 128 | |
125 | 129 | func (l *colorLogger) Hijack(f func(io.Writer) io.Writer) { |
130 | l.mu.Lock() | |
126 | 131 | l.w = f(l.w) |
132 | l.mu.Unlock() | |
127 | 133 | } |
128 | 134 | |
129 | type ColoredValue struct { | |
130 | FgBgColor | |
131 | Value interface{} | |
132 | } | |
133 | ||
134 | func (cv ColoredValue) String() string { | |
135 | // http://invisible-island.net/xterm/ctlseqs/ctlseqs.html | |
136 | if cv.Fg == 0 && cv.Bg == 0 { | |
137 | return fmt.Sprintf("%v", cv.Value) | |
138 | } | |
139 | if cv.Bg == 0 { | |
140 | return fmt.Sprintf("\x1b[%dm%v\x1b[0m", 30+cv.Fg, cv.Value) | |
141 | } | |
142 | if cv.Fg == 0 { | |
143 | return fmt.Sprintf("\x1b[%dm%v\x1b[0m", 40+cv.Bg, cv.Value) | |
144 | } | |
145 | return fmt.Sprintf("\x1b[%dm\x1b[%dm%v\x1b[0m", 30+cv.Fg, 40+cv.Bg, cv.Value) | |
146 | } | |
147 | ||
148 | func AsString(v interface{}) string { | |
135 | func asString(v interface{}) string { | |
149 | 136 | switch x := v.(type) { |
150 | 137 | case string: |
151 | 138 | return x |
157 | 144 | return fmt.Sprintf("%v", x) |
158 | 145 | } |
159 | 146 | } |
147 | ||
148 | // LevelColor returns the color for the record based on the value of the "level" key, if exists. | |
149 | func LevelColor(keyvals ...interface{}) FgBgColor { | |
150 | for i := 0; i < len(keyvals); i += 2 { | |
151 | if asString(keyvals[i]) != "level" { | |
152 | continue | |
153 | } | |
154 | switch asString(keyvals[i+1]) { | |
155 | case "debug": | |
156 | return FgBgColor{Fg: Green} | |
157 | case "info": | |
158 | return FgBgColor{Fg: White} | |
159 | case "warn": | |
160 | return FgBgColor{Fg: Yellow} | |
161 | case "error": | |
162 | return FgBgColor{Fg: Red} | |
163 | case "crit": | |
164 | return FgBgColor{Fg: Default, Bg: Red} | |
165 | default: | |
166 | return FgBgColor{} | |
167 | } | |
168 | } | |
169 | return FgBgColor{} | |
170 | } |
2 | 2 | import ( |
3 | 3 | "bytes" |
4 | 4 | "errors" |
5 | "fmt" | |
5 | 6 | "io" |
6 | 7 | "io/ioutil" |
7 | 8 | "strconv" |
10 | 11 | |
11 | 12 | "github.com/go-kit/kit/log" |
12 | 13 | "github.com/go-kit/kit/log/term" |
13 | "gopkg.in/logfmt.v0" | |
14 | 14 | ) |
15 | 15 | |
16 | 16 | type mymap map[int]int |
19 | 19 | |
20 | 20 | func TestColorLogger(t *testing.T) { |
21 | 21 | var buf bytes.Buffer |
22 | logger := newColorLogger(t, &buf) | |
22 | logger := newColorLogger(&buf) | |
23 | 23 | |
24 | 24 | if err := logger.Log("hello", "world"); err != nil { |
25 | 25 | t.Fatal(err) |
35 | 35 | if want, have := "\u001b[32m\u001b[48ma=1 err=error\n\u001b[0m", buf.String(); want != have { |
36 | 36 | t.Errorf("want %#v, have %#v", want, have) |
37 | 37 | } |
38 | ||
39 | buf.Reset() | |
40 | if err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}); err != nil { | |
41 | t.Fatal(err) | |
42 | } | |
43 | if want, have := "std_map=\""+logfmt.ErrUnsupportedValueType.Error()+"\" my_map=special_behavior\n", buf.String(); want != have { | |
44 | t.Errorf("want %#v, have %#v", want, have) | |
45 | } | |
46 | 38 | } |
47 | 39 | |
48 | func newColorLogger(t testing.TB, w io.Writer) log.Logger { | |
40 | func newColorLogger(w io.Writer) log.Logger { | |
49 | 41 | return term.NewColorLogger(log.NewLogfmtLogger(w), |
50 | 42 | func(keyvals ...interface{}) term.FgBgColor { |
51 | 43 | for i := 0; i < len(keyvals); i += 2 { |
52 | key := term.AsString(keyvals[i]) | |
44 | key := asString(keyvals[i]) | |
53 | 45 | if key == "a" { |
54 | 46 | return term.FgBgColor{Fg: term.Green, Bg: term.Default} |
55 | 47 | } |
62 | 54 | } |
63 | 55 | |
64 | 56 | func BenchmarkColorLoggerSimple(b *testing.B) { |
65 | benchmarkRunner(b, newColorLogger(b, ioutil.Discard), baseMessage) | |
57 | benchmarkRunner(b, newColorLogger(ioutil.Discard), baseMessage) | |
66 | 58 | } |
67 | 59 | |
68 | 60 | func BenchmarkColorLoggerContextual(b *testing.B) { |
69 | benchmarkRunner(b, newColorLogger(b, ioutil.Discard), withMessage) | |
61 | benchmarkRunner(b, newColorLogger(ioutil.Discard), withMessage) | |
70 | 62 | } |
71 | 63 | |
72 | 64 | func TestColorLoggerConcurrency(t *testing.T) { |
73 | testConcurrency(t, newColorLogger(t, ioutil.Discard)) | |
65 | testConcurrency(t, newColorLogger(ioutil.Discard)) | |
74 | 66 | } |
75 | 67 | |
76 | 68 | // copied from log/benchmark_test.go |
105 | 97 | logger.Log("key", strconv.FormatInt(int64(i), 10)) |
106 | 98 | } |
107 | 99 | } |
100 | ||
101 | func asString(v interface{}) string { | |
102 | switch x := v.(type) { | |
103 | case string: | |
104 | return x | |
105 | case fmt.Stringer: | |
106 | return x.String() | |
107 | case fmt.Formatter: | |
108 | return fmt.Sprint(x) | |
109 | default: | |
110 | return fmt.Sprintf("%v", x) | |
111 | } | |
112 | } |