-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_log.go
209 lines (173 loc) · 4.55 KB
/
io_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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package io
import (
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/injoyai/logs"
"os"
"time"
)
const (
LevelAll Level = iota
LevelWrite
LevelRead
LevelInfo
LevelError
LevelNone Level = 999
)
var (
nullLogger = NewLoggerWithWriter(&null{})
stdoutLogger = NewLoggerWithWriter(os.Stdout)
)
type Level int
func (this Level) Name() string {
switch this {
case LevelWrite:
return "发送"
case LevelRead:
return "接收"
case LevelInfo:
return "信息"
case LevelError:
return "错误"
}
return ""
}
func defaultLogger() *logger {
return NewLogger(stdoutLogger)
}
// NewLoggerWithNull 无输出的日志
func NewLoggerWithNull() *logger {
return NewLogger(nullLogger)
}
func NewLogger(l Logger) *logger {
if l2, ok := l.(*logger); ok {
return l2
}
if l == nil {
l = nullLogger
}
return &logger{
Logger: l,
level: LevelAll,
debug: true,
coding: nil,
}
}
type logger struct {
Logger
debug bool //是否打印调试
level Level //日志等级
coding func(p []byte) string //编码
}
func (this *logger) Debug(b ...bool) {
this.debug = len(b) == 0 || b[0]
}
func (this *logger) SetLevel(level Level) {
this.level = level
}
func (this *logger) SetLevelAll() {
this.SetLevel(LevelAll)
}
func (this *logger) SetLevelError() {
this.SetLevel(LevelError)
}
func (this *logger) SetLevelInfo() {
this.SetLevel(LevelInfo)
}
// SetPrintCoding 设置字节编码方式
func (this *logger) SetPrintCoding(coding func(p []byte) string) {
this.coding = coding
}
// SetPrintWithHEX 设置字节编码方式hex
func (this *logger) SetPrintWithHEX() {
this.SetPrintCoding(func(p []byte) string { return hex.EncodeToString(p) })
}
// SetPrintWithUTF8 设置字节编码方式utf-8
func (this *logger) SetPrintWithUTF8() {
this.SetPrintCoding(func(p []byte) string { return string(p) })
}
func (this *logger) SetPrintWithBase64() {
this.SetPrintCoding(func(p []byte) string { return base64.StdEncoding.EncodeToString(p) })
}
// Readln 打印读取到的数据
func (this *logger) Readln(prefix string, p []byte) {
if this.debug && LevelRead >= this.level {
if this.coding == nil {
this.SetPrintWithUTF8()
}
this.Logger.Readf("%s%s\n", prefix, this.coding(p))
}
}
// Writeln 打印写入的数据
func (this *logger) Writeln(prefix string, p []byte) {
if this.debug && LevelWrite >= this.level {
if this.coding == nil {
this.SetPrintWithUTF8()
}
this.Logger.Writef("%s%s\n", prefix, this.coding(p))
}
}
// Infof 打印信息
func (this *logger) Infof(format string, v ...interface{}) {
if this.debug && LevelInfo >= this.level {
this.Logger.Infof(format, v...)
}
}
// Errorf 打印错误
func (this *logger) Errorf(format string, v ...interface{}) {
if this.debug && LevelError >= this.level {
this.Logger.Errorf(format, v...)
}
}
// Printf 自定义打印
func (this *logger) Printf(format string, v ...interface{}) {
if this.debug {
this.Logger.Printf(format, v...)
}
}
/*
*/
// NewLoggerWithWriter 新建输出到writer的日志
func NewLoggerWithWriter(w Writer) Logger {
if w == nil {
w = &null{}
}
return &logWriter{w}
}
// NewLoggerWithChan 新建输出到指定通道的日志
func NewLoggerWithChan(c chan []byte) Logger {
return NewLoggerWithWriter(TryChan(c))
}
// NewLoggerChan 新建输出到通道的日志
func NewLoggerChan() (Logger, chan []byte) {
c := make(chan []byte, 10)
return NewLoggerWithChan(c), c
}
type Logger interface {
Readf(format string, v ...interface{})
Writef(format string, v ...interface{})
Infof(format string, v ...interface{})
Errorf(format string, v ...interface{})
Printf(format string, v ...interface{})
}
//==========================================logWriter==========================================
type logWriter struct{ Writer }
func (p logWriter) Readf(format string, v ...interface{}) { p.printf(LevelRead, format, v...) }
func (p logWriter) Writef(format string, v ...interface{}) { p.printf(LevelWrite, format, v...) }
func (p logWriter) Infof(format string, v ...interface{}) { p.printf(LevelInfo, format, v...) }
func (p logWriter) Errorf(format string, v ...interface{}) { p.printf(LevelError, format, v...) }
func (p logWriter) Printf(format string, v ...interface{}) {
if p.Writer == nil {
return
}
_, _ = p.Writer.Write([]byte(fmt.Sprintf(format, v...)))
}
func (p logWriter) printf(level Level, format string, v ...interface{}) {
if p.Writer == nil {
return
}
timeStr := time.Now().Format("2006-01-02 15:04:05 ")
_, err := p.Writer.Write([]byte(fmt.Sprintf(timeStr+"["+level.Name()+"] "+format, v...)))
logs.PrintErr(err)
}