forked from jpillora/go-tcp-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
71 lines (57 loc) · 1.56 KB
/
log.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
package proxy
import (
"fmt"
"github.com/mgutz/ansi"
)
// Logger - Interface to pass into Proxy for it to log messages
type Logger interface {
Trace(f string, args ...interface{})
Debug(f string, args ...interface{})
Info(f string, args ...interface{})
Warn(f string, args ...interface{})
}
// NullLogger - An empty logger that ignores everything
type NullLogger struct{}
// Trace - no-op
func (l NullLogger) Trace(f string, args ...interface{}) {}
// Debug - no-op
func (l NullLogger) Debug(f string, args ...interface{}) {}
// Info - no-op
func (l NullLogger) Info(f string, args ...interface{}) {}
// Warn - no-op
func (l NullLogger) Warn(f string, args ...interface{}) {}
// ColorLogger - A Logger that logs to stdout in color
type ColorLogger struct {
VeryVerbose bool
Verbose bool
Prefix string
Color bool
}
// Trace - Log a very verbose trace message
func (l ColorLogger) Trace(f string, args ...interface{}) {
if !l.VeryVerbose {
return
}
l.output("blue", f, args...)
}
// Debug - Log a debug message
func (l ColorLogger) Debug(f string, args ...interface{}) {
if !l.Verbose {
return
}
l.output("green", f, args...)
}
// Info - Log a general message
func (l ColorLogger) Info(f string, args ...interface{}) {
l.output("green", f, args...)
}
// Warn - Log a warning
func (l ColorLogger) Warn(f string, args ...interface{}) {
l.output("red", f, args...)
}
func (l ColorLogger) output(color, f string, args ...interface{}) {
if l.Color && color != "" {
f = ansi.Color(f, color)
}
fmt.Printf(fmt.Sprintf("%s%s\n", l.Prefix, f), args...)
}