-
Notifications
You must be signed in to change notification settings - Fork 126
/
log.go
77 lines (65 loc) · 1.07 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
package logger
import (
"fmt"
"log"
"regexp"
)
const (
ERROR = 1
INFO = 2
VERBOSE = 3
DEBUG = 7
)
// FIXME GLOBAL VARIABLES
var (
level int
filter *regexp.Regexp
)
func SetLevel(l int) {
level = l
}
func SetFilter(pattern string) error {
if pattern == "" {
return nil
}
// https://github.com/google/re2/wiki/Syntax
reg, err := regexp.Compile(pattern)
if err != nil {
return err
}
filter = reg
return nil
}
func Println(v ...any) {
if level >= INFO {
log.Println(v...)
}
}
func Printf(format string, v ...any) {
if level >= INFO {
log.Printf(format, v...)
}
}
func Verbosef(format string, v ...any) {
printfAtLevel(VERBOSE, format, v...)
}
func Debugf(format string, v ...any) {
printfAtLevel(DEBUG, format, v...)
}
func printfAtLevel(l int, format string, v ...any) {
if level < l {
return
}
out := filterOutput(format, v...)
if out == "" {
return
}
log.Print(out)
}
func filterOutput(format string, v ...any) string {
out := fmt.Sprintf(format, v...)
if filter == nil || filter.MatchString(out) {
return out
}
return ""
}