-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslog.go
195 lines (159 loc) · 5.04 KB
/
slog.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package slog
import (
"io"
"os"
"strings"
)
// TODO: Syslog Output
// FieldRepresentationType specifies which log instance fields formatting should be used
type FieldRepresentationType int
const (
// NoFields disables the representation of the log instance fields
NoFields FieldRepresentationType = iota
// JSONFields enables the representation of log instance fields and formats them as a json string
JSONFields
// KeyValueFields enables the representation of log instance fields and formats them as a comma separated key=value fields
KeyValueFields
)
// Format specifies the logging format (could be pipe separated, JSON, ...)
type Format string
const (
// JSON specifies to log in JSON format
JSON Format = "json"
// PIPE specifies to log in Pipe Delimited Text format
PIPE Format = "pipe"
)
// ToFormat converts a string to its corresponding Format type
func ToFormat(s string) Format {
switch strings.ToLower(s) {
case string(JSON):
return JSON
case string(PIPE):
return PIPE
default:
return PIPE
}
}
// region Global
var enabledLevels = map[LogLevel]bool{
DEBUG: true,
WARN: true,
ERROR: true,
INFO: true,
FATAL: true,
}
var fieldRepresentation = JSONFields
var logFormat = PIPE
var defaultOut io.Writer = os.Stdout
var showLines = false
var glog *slogInstance
func init() {
glog = Scope("Global").(*slogInstance)
glog.stackOffset += 1 // This will be called from global context, so the stack has one more level
}
// LogNoFormat prints a log string without any ANSI formatting
func LogNoFormat(str interface{}, v ...interface{}) Instance {
return glog.LogNoFormat(str, v...)
}
// Log is equivalent of calling Info. It logs out a message in INFO level
func Log(str interface{}, v ...interface{}) Instance {
return glog.Log(str, v...)
}
// Info logs out a message in INFO level
func Info(str interface{}, v ...interface{}) Instance {
return glog.Info(str, v...)
}
// Debug logs out a message in DEBUG level
func Debug(str interface{}, v ...interface{}) Instance {
return glog.Debug(str, v...)
}
// Warn logs out a message in WARN level
func Warn(str interface{}, v ...interface{}) Instance {
return glog.Warn(str, v...)
}
// Error logs out a message in ERROR level
func Error(str interface{}, v ...interface{}) Instance {
return glog.Error(str, v...)
}
// Fatal logs out a message in ERROR level and closes the program
func Fatal(str interface{}, v ...interface{}) {
glog.Fatal(str, v)
}
// Scope creates a new slog Instance with the specified root scope
func Scope(scope string) Instance {
return &slogInstance{
scope: []string{scope},
customOut: defaultOut,
stackOffset: 5,
tag: "NONE",
op: MSG,
}
}
// SetDefaultOutput sets the Global Default Output I/O and for every new instance created by Scope function
func SetDefaultOutput(o io.Writer) {
defaultOut = o
glog.customOut = o
}
// SetDebug globally sets if the DEBUG level messages will be shown. Affects all instances
func SetDebug(enabled bool) {
enabledLevels[DEBUG] = enabled
}
// SetWarning globally sets if the WARN level messages will be shown. Affects all instances
func SetWarning(enabled bool) {
enabledLevels[WARN] = enabled
}
// SetInfo globally sets if the INFO level messages will be shown. Affects all instances
func SetInfo(enabled bool) {
enabledLevels[INFO] = enabled
}
// SetError globally sets if the ERROR level messages will be shown. Affects all instances
func SetError(enabled bool) {
enabledLevels[ERROR] = enabled
}
// SetShowLines globally sets if the filename and line of the caller function will be shown. Affects all instances
func SetShowLines(enabled bool) {
showLines = enabled
}
// SetFieldRepresentation globally sets if the representation of log fields. Affects all instances
func SetFieldRepresentation(representationType FieldRepresentationType) {
fieldRepresentation = representationType
}
// SetLogFormat globally sets the logging format. Affects all instances
func SetLogFormat(f Format) {
logFormat = f
}
// SetTestMode sets the SLog Instances to test mode a.k.a. all logs disabled. Equivalent to set all levels visibility to false
func SetTestMode() {
SetDebug(false)
SetWarning(false)
SetInfo(false)
SetError(false)
}
// UnsetTestMode sets the SLog Instances to default mode a.k.a. all logs enabled. Equivalent to set all levels visibility to true
func UnsetTestMode() {
SetDebug(true)
SetWarning(true)
SetInfo(true)
SetError(true)
}
// DebugEnabled returns if the DEBUG level messages are currently enabled
func DebugEnabled() bool {
return enabledLevels[DEBUG]
}
// WarningEnabled returns if the WARN level messages are currently enabled
func WarningEnabled() bool {
return enabledLevels[WARN]
}
// InfoEnabled returns if the INFO level messages are currently enabled
func InfoEnabled() bool {
return enabledLevels[INFO]
}
// ErrorEnabled returns if the ERROR level messages are currently enabled
func ErrorEnabled() bool {
return enabledLevels[ERROR]
}
// ShowLinesEnabled returns if the show filename and line from called function is currently enabled
func ShowLinesEnabled() bool {
return showLines
}
// endregion