-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
163 lines (139 loc) · 3.43 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package env_logger
import (
"runtime"
"os"
"strings"
logrus "github.com/Sirupsen/logrus"
)
var (
internalLogger = logrus.New()
defaultLogger *logrus.Logger
loggers = make(map[string]*logrus.Logger)
)
const (
DebugV = iota
InfoV = iota
WarnV = iota
)
type Logger interface {
// New() Logger // used to instantiate a new logger
Debug(...interface{})
Info(...interface{})
Warn(...interface{})
Fatal(...interface{})
}
func toEnum(s string) int {
switch strings.ToLower(s) {
case "warn":
return WarnV
case "debug":
return DebugV
case "info":
return InfoV
default:
return InfoV
}
}
func configurePackageLogger(log *logrus.Logger, value int) *logrus.Logger {
switch value {
case WarnV:
log.SetLevel(logrus.WarnLevel)
case InfoV:
log.SetLevel(logrus.InfoLevel)
case DebugV:
log.SetLevel(logrus.DebugLevel)
default:
log.SetLevel(logrus.InfoLevel)
}
return log
}
// ConfigureDefaultLogger instantiates a default logger instance
func ConfigureInternalLogger(newInternalLogger *logrus.Logger) {
internalLogger = newInternalLogger
}
// ConfigureDefaultLogger instantiates a default logger instance
func ConfigureDefaultLogger() {
defaultLogger = logrus.New()
ConfigureLogger(defaultLogger)
}
// ConfigureLogger takes in a prefix and a logger object and configures the logger depending on environment variables.
// Configured based on the GOLANG_DEBUG environment variable
func ConfigureLogger(newDefaultLogger *logrus.Logger) {
levels := make(map[string]int)
if debugRaw, ok := os.LookupEnv("GOLANG_LOG"); ok {
packages := strings.Split(debugRaw, ",")
for _, pkg := range packages {
// check if a package name has been specified, if not default to main
tmp := strings.Split(pkg, "=")
if len(tmp) == 1 {
levels["main"] = toEnum(tmp[0])
} else if len(tmp) == 2 {
levels[tmp[0]] = toEnum(tmp[1])
} else {
newDefaultLogger.Fatal("line: '", pkg, "' is formatted incorrectly, please refer to the documentation for correct usage")
}
}
}
for key, value := range levels {
loggers[key] = configurePackageLogger(logrus.New(), value)
}
// configure main logger
if value, ok := loggers["main"]; ok {
defaultLogger = value
} else {
defaultLogger = newDefaultLogger
}
}
// Props to https://stackoverflow.com/a/35213181 for the code
func getPackage () string {
// we get the callers as uintptrs - but we just need 1
fpcs := make([]uintptr, 1)
// skip 4 levels to get to the caller of whoever called getPackage()
n := runtime.Callers(4, fpcs)
if n == 0 {
return "" // proper error her would be better
}
// get the info of the actual function that's in the pointer
fun := runtime.FuncForPC(fpcs[0]-1)
if fun == nil {
return ""
}
name := fun.Name()
// return its name
return strings.Split(name, ".")[0]
}
type F func(Logger)
func printLog(f F) {
pkg := getPackage()
internalLogger.Debug("pkg: ", pkg)
if log, ok := loggers[pkg]; ok {
f(log)
return
}
f(defaultLogger)
}
// Warn prints a warning...
func Warn(args ...interface{}) {
lambda := func(log Logger) {
log.Warn(args...)
}
printLog(lambda)
}
func Info(args ...interface{}) {
lambda := func(log Logger) {
log.Info(args...)
}
printLog(lambda)
}
func Debug(args ...interface{}) {
lambda := func(log Logger) {
log.Debug(args...)
}
printLog(lambda)
}
func Fatal(args ...interface{}) {
lambda := func(log Logger) {
log.Fatal(args...)
}
printLog(lambda)
}