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
|
package logger
import (
"fmt"
"strings"
"time"
)
// DefaultLogger is used if no logger has been set up.
type defaultLogger struct {
namespaces []string
}
// log simply logs the given message to stdout if the message
// caller is allowed to log.
func (d *defaultLogger) log(level, caller, msg string) {
if !d.shouldLog(caller) {
// return
}
t := time.Now()
fmt.Println(
"["+level+"]",
t.Format(time.RFC3339),
caller,
"▶ ",
msg,
)
}
// shouldLog determines whether or not the given caller should
// be allowed to log messages.
func (d *defaultLogger) shouldLog(caller string) bool {
shouldLog := false
d.ensureNamespaces()
for _, namespace := range d.namespaces {
if namespace == "all" {
shouldLog = true
}
if strings.Contains(caller, namespace) {
shouldLog = true
}
}
return shouldLog
}
// ensureNamespaces checks to see if our list of loggable namespaces
// has been initialized or not. If not, it defaults to log all.
func (d *defaultLogger) ensureNamespaces() {
if d.namespaces == nil {
d.namespaces = []string{"all"}
}
}
// Debug is used to log debug messages.
func (d *defaultLogger) Debug(caller, msg string) {
//d.log("DEBUG", caller, msg)
}
// Info is used to log info messages.
func (d *defaultLogger) Info(caller, msg string) {
d.log("INFO", caller, msg)
}
// Warning is used to log warning messages.
func (d *defaultLogger) Warning(caller, msg string) {
d.log("WARNING", caller, msg)
}
// Error is used to log error messages.
func (d *defaultLogger) Error(caller, msg string) {
d.log("ERROR", caller, msg)
}
// Configure takes a configuration string separated by commas
// that contains all the callers that should be logged. This
// allows granular logging of different go files.
//
// Example:
// logger.Configure("RootKey.go,Curve.go")
// logger.Configure("all")
//
func (d *defaultLogger) Configure(settings string) {
d.namespaces = strings.Split(settings, ",")
}
|