/
console.go
119 lines (95 loc) · 2.97 KB
/
console.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package logger
import (
"context"
"log"
"net/http"
)
type color int
const (
red color = 31
yellow color = 33
blue color = 34
gray color = 37
)
// ConsoleExporter implements exporting to Google Cloud Logging
type ConsoleExporter struct {
noColor bool
}
// NewConsoleExporter returns a configured ConsoleExporter
func NewConsoleExporter() *ConsoleExporter {
return &ConsoleExporter{}
}
// NoColor controls if this logger will use color to highlight log level
func (e *ConsoleExporter) NoColor(v bool) *ConsoleExporter {
e.noColor = v
return e
}
// Middleware returns a middleware that exports logs to Google Cloud Logging
func (e *ConsoleExporter) Middleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return &consoleHandler{
next: next,
noColor: e.noColor,
}
}
}
type consoleHandler struct {
next http.Handler
noColor bool
}
func (c *consoleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(newContext(r.Context(), newConsoleLogger(r, c.noColor)))
c.next.ServeHTTP(w, r)
}
type consoleLogger struct {
r *http.Request
noColor bool
}
// newConsoleLogger logs all output to console
func newConsoleLogger(r *http.Request, noColor bool) *consoleLogger {
return &consoleLogger{r: r, noColor: noColor}
}
// Debug logs a debug message.
func (l *consoleLogger) Debug(_ context.Context, v interface{}) {
l.console("DEBUG", gray, v)
}
// Debugf logs a debug message with format.
func (l *consoleLogger) Debugf(_ context.Context, format string, v ...interface{}) {
l.consolef("DEBUG", gray, format, v...)
}
// Info logs a info message.
func (l *consoleLogger) Info(_ context.Context, v interface{}) {
l.console("INFO ", blue, v)
}
// Infof logs a info message with format.
func (l *consoleLogger) Infof(_ context.Context, format string, v ...interface{}) {
l.consolef("INFO ", blue, format, v...)
}
// Warn logs a warning message.
func (l *consoleLogger) Warn(_ context.Context, v interface{}) {
l.console("WARN ", yellow, v)
}
// Warnf logs a warning message with format.
func (l *consoleLogger) Warnf(_ context.Context, format string, v ...interface{}) {
l.consolef("WARN ", yellow, format, v...)
}
// Error logs an error message.
func (l *consoleLogger) Error(_ context.Context, v interface{}) {
l.console("ERROR", red, v)
}
// Errorf logs an error message with format.
func (l *consoleLogger) Errorf(_ context.Context, format string, v ...interface{}) {
l.consolef("ERROR", red, format, v...)
}
func (l *consoleLogger) console(level string, c color, v interface{}) {
log.Printf(l.colorPrint(level, c)+": %s %s", l.r.URL.Path, v)
}
func (l *consoleLogger) consolef(level string, c color, format string, v ...interface{}) {
log.Printf(l.colorPrint(level, c)+": "+l.r.Method+" "+l.r.URL.Path+" "+format, v...)
}
func (l *consoleLogger) colorPrint(s string, c color) string {
if l.noColor {
return s
}
return string([]byte{0x1b, '[', byte('0' + c/10), byte('0' + c%10), 'm'}) + s + "\x1b[0m"
}