-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
246 lines (210 loc) · 4.7 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package golog
import (
"fmt"
"io"
"log"
"os"
"runtime"
"strconv"
"strings"
"time"
)
const (
ExpandFunctionName = "FUNCTION"
ExpandLineNumber = "LINE"
ExpandTime = "TIME"
ExpandDuration = "DURATION"
ExpandSubject = "SUBJECT"
ExpandMessageLevel = "LEVEL"
ExpandMessage = "MESSAGE"
)
type LogLevel uint8
const (
All LogLevel = iota
Trace
Debug
Info
Warning
Error
None
)
func (ll LogLevel) String() string {
switch ll {
case None:
return "NONE"
case Error:
return "ERR "
case Warning:
return "WARN"
case Info:
return "INFO"
case Debug:
return "DBG "
case Trace:
return "TRC "
case All:
return "ALL "
default:
panic(fmt.Sprintf("No such log level, %v", ll))
}
}
func ToLevel(s string) (LogLevel, error) {
switch strings.ToLower(s) {
case "none":
return None, nil
case "err":
return Error, nil
case "warning":
return Warning, nil
case "info":
return Info, nil
case "debug":
return Debug, nil
case "trace":
return Trace, nil
case "all":
return All, nil
default:
return None, fmt.Errorf("no such log level: %v", s)
}
}
type Instance struct {
Name string
Format string
output io.Writer
tags []string
log *log.Logger
needsRuntime bool
UpdateOutput func()
UpdateOutputTrigger time.Ticker
}
func newInstance(name, format string, output io.Writer, tags ...string) *Instance {
i := &Instance{
Name: name,
Format: format,
output: output,
tags: tags,
}
return i.checkForRuntime()
}
func (i *Instance) checkForRuntime() *Instance {
if strings.Contains(i.Format, ExpandFunctionName) || strings.Contains(i.Format, ExpandLineNumber) {
i.needsRuntime = true
}
return i
}
func (i *Instance) expand(format string, msg *Message) string {
for expand, value := range msg.values {
format = strings.Replace(format, expand, value, -1)
}
return format
}
func (i *Instance) initialize() *Instance {
//i.log = log.New(os.Stdout, "", log.LstdFlags)
i.log = log.New(i.output, "", 0)
return i
}
func AddLoggerInstance(name, format string, output io.Writer, tags ...string) {
RegisterLogger <- newInstance(name, format, output, tags...).initialize()
}
type Message struct {
Level LogLevel
Subject interface{}
Tags []string
values map[string]string
Format string
Elements []interface{}
}
var (
// Sink is the main channel for sending/recieving messages
Sink chan Message
// NewLevel is channel that can change the log level
NewLevel chan LogLevel
level LogLevel = Info
// By default there is one logging instance, and it logs to stdout.
gologgers []*Instance = []*Instance{newInstance("default", "[LEVEL] SUBJECT, MESSAGE", os.Stdout).initialize()}
RegisterLogger chan *Instance
DeRegisterLogger chan string
// Register the start time
start = time.Now()
)
func Log(level LogLevel, subject interface{}, format string, elements ...interface{}) {
pc, name, line, _ := runtime.Caller(1)
fun := runtime.FuncForPC(pc)
if fun != nil {
name = fun.Name()
}
// Context specific values
values := map[string]string{
ExpandLineNumber: strconv.Itoa(line),
ExpandFunctionName: name,
ExpandTime: time.Now().String(),
ExpandDuration: time.Now().Sub(start).String(),
}
Sink <- Message{level, subject, nil, values, format, elements}
}
func IsProduction(b bool) {
if b {
NewLevel <- Warning
}
}
func init() {
Sink = make(chan Message, 256)
RegisterLogger = make(chan *Instance, 1)
DeRegisterLogger = make(chan string, 1)
NewLevel = make(chan LogLevel)
go func() {
for {
select {
case logger := <-RegisterLogger:
gologgers = append(gologgers, logger)
case name := <-DeRegisterLogger:
i := 0
for _, l := range gologgers {
if l.Name == name {
continue
}
gologgers[i] = l
i++
}
gologgers = gologgers[:i]
case newLevel := <-NewLevel:
level = newLevel
case msg := <-Sink:
if msg.Level < level {
break
}
var loggers []*Instance
for _, l := range gologgers {
// Verify tags, no tags means pass on everything. Right now, though.
if len(l.tags) > 0 {
if !matchTags(msg.Tags, l.tags) {
continue
}
}
loggers = append(loggers, l)
}
if len(loggers) == 0 {
break
}
msg.values[ExpandMessage] = fmt.Sprintf(msg.Format, msg.Elements...)
msg.values[ExpandSubject] = fmt.Sprintf("%v", msg.Subject)
msg.values[ExpandMessageLevel] = msg.Level.String()
for _, l := range loggers {
format := l.expand(l.Format, &msg)
l.log.Printf(format)
}
}
}
}()
}
func matchTags(msgTags, logTags []string) bool {
for _, t := range msgTags {
for _, tt := range logTags {
if t == tt {
return true
}
}
}
return false
}