/
zztlog.go
168 lines (142 loc) · 2.83 KB
/
zztlog.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
package zztlog
import (
"encoding/json"
"fmt"
"io/ioutil"
"sync"
)
var (
l logHandler
config BaseConfig
m sync.Mutex
)
type initLog struct{}
func InitConfigFile(path string) error {
r, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = json.Unmarshal(r, &config)
if err != nil {
return err
}
l.isInit = true
return nil
}
func InitConfig(b BaseConfig) {
handleBaseConfig(&b)
config = b
l.isInit = true
}
func handleBaseConfig(b *BaseConfig) {
if !b.LogConfig.CmdOutput {
b.LogConfig.CmdOutput = true
}
if b.LogConfig.TimeFormat == "" {
b.LogConfig.TimeFormat = "2006-01-02 15:04:05"
}
if b.LogConfig.MaxSizeM == 0 {
b.LogConfig.MaxSizeM = 100
}
}
func Default() *initLog { return &initLog{} }
func Debug(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.debug(fmt.Sprint(s...))
}
func Info(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.info(fmt.Sprint(s...))
}
func Fatal(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.fatal(fmt.Sprint(s...))
}
func Warn(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.warn(fmt.Sprint(s...))
}
func Error(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.error(fmt.Sprint(s...))
}
func DebugF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.debug(fmt.Sprintf(format, s...))
}
func InfoF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.info(fmt.Sprintf(format, s...))
}
func FatalF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.fatal(fmt.Sprintf(format, s...))
}
func WarnF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.warn(fmt.Sprintf(format, s...))
}
func ErrorF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.error(fmt.Sprintf(format, s...))
}
func (i *initLog) Debug(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.debug(fmt.Sprint(s...))
}
func (i *initLog) Info(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.info(fmt.Sprint(s...))
}
func (i *initLog) Fatal(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.fatal(fmt.Sprint(s...))
}
func (i *initLog) Warn(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.warn(fmt.Sprint(s...))
}
func (i *initLog) Error(s ...interface{}) {
m.Lock()
defer m.Unlock()
l.error(fmt.Sprint(s...))
}
func (i *initLog) DebugF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.debug(fmt.Sprintf(format, s...))
}
func (i *initLog) InfoF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.info(fmt.Sprintf(format, s...))
}
func (i *initLog) FatalF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.fatal(fmt.Sprintf(format, s...))
}
func (i *initLog) WarnF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.warn(fmt.Sprintf(format, s...))
}
func (i *initLog) ErrorF(format string, s ...interface{}) {
m.Lock()
defer m.Unlock()
l.error(fmt.Sprintf(format, s...))
}