forked from milanbella/gocosem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
97 lines (85 loc) · 1.91 KB
/
logger.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
package gocosem
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
)
var (
Log = log.New(os.Stderr, "[gocosem] ", log.Lmicroseconds)
logLevel int
)
const (
LOG_LEVEL_OFF = iota
LOG_LEVEL_FATAL
LOG_LEVEL_ERROR
LOG_LEVEL_WARN
LOG_LEVEL_INFO
LOG_LEVEL_DEBUG
LOG_LEVEL_TRACE
LOG_LEVEL_ALL
)
func SetLogLevel(lvl string) {
switch lvl {
case "ALL":
logLevel = LOG_LEVEL_ALL
case "TRACE":
logLevel = LOG_LEVEL_TRACE
case "DEBUG":
logLevel = LOG_LEVEL_DEBUG
case "INFO":
logLevel = LOG_LEVEL_INFO
case "WARN":
logLevel = LOG_LEVEL_WARN
case "ERROR":
logLevel = LOG_LEVEL_ERROR
case "FATAL":
logLevel = LOG_LEVEL_FATAL
case "OFF":
logLevel = LOG_LEVEL_OFF
default:
panic("invalid log level: " + lvl)
}
}
func fatalLog(f string, a ...interface{}) {
if logLevel >= LOG_LEVEL_FATAL {
Log.Printf("FATAL: %s: %s", funcInfo(), fmt.Sprintf(f, a...))
}
}
func errorLog(f string, a ...interface{}) {
if logLevel >= LOG_LEVEL_ERROR {
Log.Printf("ERROR: %s: %s", funcInfo(), fmt.Sprintf(f, a...))
}
}
func warnLog(f string, a ...interface{}) {
if logLevel >= LOG_LEVEL_WARN {
Log.Printf("WARN: %s: %s", funcInfo(), fmt.Sprintf(f, a...))
}
}
func infoLog(f string, a ...interface{}) {
if logLevel >= LOG_LEVEL_INFO {
Log.Printf("INFO: %s: %s", funcInfo(), fmt.Sprintf(f, a...))
}
}
func debugLog(f string, a ...interface{}) {
if logLevel >= LOG_LEVEL_DEBUG {
Log.Printf("DEBUG: %s: %s", funcInfo(), fmt.Sprintf(f, a...))
}
}
func traceLog(f string, a ...interface{}) {
if logLevel >= LOG_LEVEL_TRACE {
Log.Printf("TRACE: %s: %s", funcInfo(), fmt.Sprintf(f, a...))
}
}
var stripFnPreamble = regexp.MustCompile(`^.*\.(.*)$`)
func funcInfo() string {
name := "<unknown>"
pc, file, line, ok := runtime.Caller(2)
if ok {
name = stripFnPreamble.ReplaceAllString(runtime.FuncForPC(pc).Name(), "$1")
}
file = filepath.Base(file)
return fmt.Sprintf("%s:%d: %s()", file, line, name)
}