This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
smallog.go
137 lines (117 loc) · 2.6 KB
/
smallog.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
//go:build !windows && !plan9
package smallog
import (
"errors"
"log/syslog"
)
type Logger interface {
Emerg(string, ...interface{})
Alert(string, ...interface{})
Crit(string, ...interface{})
Err(string, ...interface{})
Warning(string, ...interface{})
Notice(string, ...interface{})
Info(string, ...interface{})
Debug(string, ...interface{})
}
var logLevel = syslog.LOG_INFO
var logger Logger = nil
func init() {
sysLogger, _ := syslog.New(syslog.LOG_DEBUG, "")
if err != nil {
panic("failed to new syslog ")
}
logger = newDefaultLogger(sysLogger)
SetLogLevel(syslog.LOG_INFO)
}
func SetCustomLogger(newLogger Logger) (err error) {
if newLogger == nil {
err = errors.New("new logger is nil")
}
logger = newLogger
SetLogLevel(logLevel)
return
}
func GetLogLevel() (level syslog.Priority) {
level = logLevel
return
}
func SetLogLevel(level syslog.Priority) {
{
debug = nothingToDo
info = nothingToDo
notice = nothingToDo
warning = nothingToDo
err = nothingToDo
crit = nothingToDo
alert = nothingToDo
emerg = nothingToDo
}
resetDefault:
switch level {
case syslog.LOG_DEBUG:
debug = logger.Debug
fallthrough
case syslog.LOG_INFO:
info = logger.Info
fallthrough
case syslog.LOG_NOTICE:
notice = logger.Notice
fallthrough
case syslog.LOG_WARNING:
warning = logger.Warning
fallthrough
case syslog.LOG_ERR:
err = logger.Err
fallthrough
case syslog.LOG_CRIT:
crit = logger.Crit
fallthrough
case syslog.LOG_ALERT:
alert = logger.Alert
fallthrough
case syslog.LOG_EMERG:
emerg = logger.Emerg
logLevel = level
return
default:
logger.Err("failed to set smallog level, fallback to default level [LOG_INFO]")
level = syslog.LOG_INFO
goto resetDefault
}
}
func nothingToDo(string, ...interface{}) {}
var (
debug func(string, ...interface{})
info func(string, ...interface{})
notice func(string, ...interface{})
warning func(string, ...interface{})
err func(string, ...interface{})
crit func(string, ...interface{})
alert func(string, ...interface{})
emerg func(string, ...interface{})
)
func Debug(format string, a ...interface{}) {
debug(format, a...)
}
func Info(format string, a ...interface{}) {
info(format, a...)
}
func Notice(format string, a ...interface{}) {
notice(format, a...)
}
func Warning(format string, a ...interface{}) {
warning(format, a...)
}
func Err(format string, a ...interface{}) {
err(format, a...)
}
func Crit(format string, a ...interface{}) {
crit(format, a...)
}
func Alert(format string, a ...interface{}) {
alert(format, a...)
}
func Emerg(format string, a ...interface{}) {
emerg(format, a...)
}