-
Notifications
You must be signed in to change notification settings - Fork 221
/
logging.go
149 lines (123 loc) · 2.42 KB
/
logging.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
package terminal
import (
"fmt"
"os"
"strings"
"github.com/logrusorgru/aurora"
)
type LogLevel int
const (
LevelDebug LogLevel = iota
LevelInfo
LevelWarn
LevelError
)
var DefaultLogger = &Logger{level: LevelInfo}
type Logger struct {
level LogLevel
}
func init() {
switch strings.ToLower(os.Getenv("LOG_LEVEL")) {
case "debug":
DefaultLogger.SetLogLevel(LevelDebug)
case "info":
DefaultLogger.SetLogLevel(LevelInfo)
case "warn":
DefaultLogger.SetLogLevel(LevelWarn)
case "error":
DefaultLogger.SetLogLevel(LevelError)
default:
DefaultLogger.SetLogLevel(LevelInfo)
}
}
func (l *Logger) SetLogLevel(lvl LogLevel) {
l.level = lvl
}
func Debug(v ...interface{}) {
DefaultLogger.Debug(v...)
}
func (l *Logger) Debug(v ...interface{}) {
if l.level > LevelDebug {
return
}
fmt.Println(
aurora.Sprintf(
aurora.Faint("DEBUG %s"),
fmt.Sprint(v...),
),
)
}
func Debugf(format string, v ...interface{}) {
DefaultLogger.Debugf(format, v...)
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.level > LevelDebug {
return
}
fmt.Printf(
aurora.Sprintf(
aurora.Faint(fmt.Sprintf("DEBUG %s", format)),
v...,
),
)
}
func Info(v ...interface{}) {
DefaultLogger.Info(v...)
}
func (l *Logger) Info(v ...interface{}) {
if l.level > LevelInfo {
return
}
fmt.Print("INFO ")
fmt.Println(v...)
}
func Infof(format string, v ...interface{}) {
DefaultLogger.Infof(format, v...)
}
func (l *Logger) Infof(format string, v ...interface{}) {
if l.level > LevelInfo {
return
}
fmt.Print("INFO ")
fmt.Printf(format, v...)
}
func Warn(v ...interface{}) {
DefaultLogger.Warn(v...)
}
func (l *Logger) Warn(v ...interface{}) {
if l.level > LevelWarn {
return
}
fmt.Print(aurora.Yellow("WARN "))
fmt.Println(v...)
}
func Warnf(format string, v ...interface{}) {
DefaultLogger.Warnf(format, v...)
}
func (l *Logger) Warnf(format string, v ...interface{}) {
if l.level > LevelWarn {
return
}
fmt.Print(aurora.Yellow("WARN "))
fmt.Printf(format, v...)
}
func Error(v ...interface{}) {
DefaultLogger.Error(v...)
}
func (l *Logger) Error(v ...interface{}) {
if l.level > LevelError {
return
}
fmt.Print(aurora.Red("ERROR "))
fmt.Println(v...)
}
func Errorf(format string, v ...interface{}) {
DefaultLogger.Errorf(format, v...)
}
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.level > LevelError {
return
}
fmt.Print(aurora.Red("ERROR "))
fmt.Printf(format, v...)
}