-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathylog.go
178 lines (159 loc) · 3.07 KB
/
ylog.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
package ylog
import (
"fmt"
"sync"
"sync/atomic"
"time"
)
type (
Logger interface {
Info(msg string)
Debug(msg string)
Warn(msg string)
Error(msg string)
SetFormatter(f Formatter)
SetOuter(o Outer)
SetLevel(l Level)
With(fields Fields) Logger
}
// 日志记录
Record struct {
Time time.Time
Msg string // 原始消息
Fmsg string // 格式化后的消息内容
Level Level
Fields Fields // 自定义字段
}
Log struct {
f Formatter
o Outer
level Level
fields Fields // 公共字段
mu sync.RWMutex
}
)
var defaultLog Logger
func init() {
defaultLog = New()
defaultLog.SetLevel(InfoLevel)
}
func New() Logger {
return &Log{
f: NewLineFormatter(),
o: NewStdout(),
}
}
func DefaultLogger() Logger {
return defaultLog
}
func Info(msg string) {
defaultLog.Info(msg)
}
func Infof(format string, a ...interface{}) {
defaultLog.Info(fmt.Sprintf(format, a...))
}
func Debug(msg string) {
defaultLog.Debug(msg)
}
func Debugf(format string, a ...interface{}) {
defaultLog.Debug(fmt.Sprintf(format, a...))
}
func Warn(msg string) {
defaultLog.Warn(msg)
}
func Warnf(format string, a ...interface{}) {
defaultLog.Warn(fmt.Sprintf(format, a...))
}
func Error(msg string) {
defaultLog.Error(msg)
}
func Errorf(format string, a ...interface{}) {
defaultLog.Error(fmt.Sprintf(format, a...))
}
func SetOuter(o Outer) {
defaultLog.SetOuter(o)
}
func SetFormatter(f Formatter) {
defaultLog.SetFormatter(f)
}
func SetLevel(l Level) {
defaultLog.SetLevel(l)
}
func With(f Fields) Logger {
return defaultLog.With(f)
}
func (l *Log) Info(msg string) {
l.Write(msg, InfoLevel)
}
func (l *Log) Debug(msg string) {
l.Write(msg, DebugLevel)
}
func (l *Log) Warn(msg string) {
l.Write(msg, WarnLevel)
}
func (l *Log) Error(msg string) {
l.Write(msg, ErrorLevel)
}
func (l *Log) SetFormatter(f Formatter) {
l.mu.Lock()
defer l.mu.Unlock()
l.f = f
}
func (l *Log) GetFormatter() Formatter {
l.mu.RLock()
defer l.mu.RUnlock()
return l.f
}
func (l *Log) SetOuter(o Outer) {
l.mu.Lock()
defer l.mu.Unlock()
l.o = o
}
func (l *Log) GetOuter() Outer {
l.mu.RLock()
defer l.mu.RUnlock()
return l.o
}
func (l *Log) SetLevel(level Level) {
atomic.StoreUint32((*uint32)(&l.level), uint32(level))
}
func (l *Log) GetLevel() Level {
return Level(atomic.LoadUint32((*uint32)(&l.level)))
}
func (l *Log) With(fs Fields) Logger {
newdata := make(Fields, len(l.fields)+len(fs))
for k, v := range l.GetFields() {
newdata[k] = v
}
for k, v := range fs {
newdata[k] = v
}
return &Log{
f: l.GetFormatter(),
o: l.GetOuter(),
level: l.GetLevel(),
fields: newdata,
}
}
func (l *Log) GetFields() Fields {
return l.fields
}
func (l *Log) Write(msg string, level Level) error {
if level >= l.GetLevel() {
record := Record{
Time: time.Now(),
Msg: msg,
Level: level,
Fields: l.GetFields(),
}
// 格式化
fmsg, err := l.GetFormatter().Format(record)
if err != nil {
return fmt.Errorf("format record err: %w", err)
}
record.Fmsg = fmsg
// 输出
return l.GetOuter().Write(record)
}
return nil
}