-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_transport.go
70 lines (63 loc) · 1.25 KB
/
console_transport.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
package remote_log_go
import (
"fmt"
"os"
"strings"
)
type ConsoleTransport struct {
allowLevel []Level // 允许使用此方式的日志级别
}
/**
* @description: 创建ConsoleTransport类
* @param {...Level} allowLevel
* @return {*}
*/
func NewConsoleTransport(allowLevel ...Level) *ConsoleTransport {
return &ConsoleTransport{
allowLevel: allowLevel,
}
}
/**
* @description: 是否允许此方式记录日志
* @param {Level} level
* @return {*}
*/
func (c *ConsoleTransport) shouldLog(level Level) bool {
for _, v := range c.allowLevel {
if v == level {
return true
}
}
return false
}
/**
* @description: 记录日志
* @param {*LogInfo} log
* @return {*}
*/
func (c *ConsoleTransport) log(log *LogInfo) {
logStr := formatConsole(log)
if log.Level == string(Error) {
fmt.Fprintln(os.Stderr, logStr)
} else {
fmt.Println(logStr)
}
}
/**
* @description: 日志格式化
* @param {*logger.LogInfo} log
* @return {*}
*/
func formatConsole(log *LogInfo) string {
var s strings.Builder
s.WriteString(log.LogTime)
s.WriteString(" ")
s.WriteString(log.Level)
s.WriteString(" ")
s.WriteString(log.ServiceName)
s.WriteString(" ")
s.WriteString(log.AppName)
s.WriteString(" ")
s.WriteString(log.Message)
return s.String()
}