log.go
1.8 KB
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
package log
import (
"fmt"
"github.com/astaxie/beego/logs"
"github.com/tiptok/gocomm/config"
logx "github.com/tiptok/gocomm/pkg/log"
"log"
"strings"
)
type ConsoleLog struct {
//log *log.Logger
config config.Logger
depth int
level int
}
func NewConsoleLog(config config.Logger, prefix string, depth int) logx.Log {
if len(prefix) > 0 {
log.SetPrefix(prefix)
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
return &ConsoleLog{
config: config,
depth: depth,
level: beegoLogLevelAdapter(config.Level),
}
}
func (this *ConsoleLog) Debug(args ...interface{}) {
if this.level < logs.LevelDebug {
return
}
log.Output(this.depth, this.appendAhead(args, "[D]"))
}
func (this *ConsoleLog) Info(args ...interface{}) {
if this.level < logs.LevelInfo {
return
}
log.Output(this.depth, this.appendAhead(args, "[I]"))
}
func (this *ConsoleLog) Warn(args ...interface{}) {
if this.level < logs.LevelWarning {
return
}
log.Output(this.depth, this.appendAhead(args, "[W]"))
}
func (this *ConsoleLog) Error(args ...interface{}) {
if this.level < logs.LevelError {
return
}
log.Output(this.depth, this.appendAhead(args, "[E]"))
}
func (this *ConsoleLog) Panic(args ...interface{}) {
log.Output(this.depth, this.appendAhead(args, "[P]"))
}
func (this *ConsoleLog) Fatal(args ...interface{}) {
log.Output(this.depth, this.appendAhead(args, "[F]"))
}
func (this *ConsoleLog) appendAhead(args []interface{}, addArgs ...interface{}) string {
var rsp []interface{}
rsp = append(rsp, addArgs...)
rsp = append(rsp, args...)
return fmt.Sprint(rsp...)
}
func beegoLogLevelAdapter(logLevel string) int {
switch strings.ToUpper(logLevel) {
case "DEBUG":
return logs.LevelDebug
case "INFO":
return logs.LevelInformational
case "WARN":
return logs.LevelWarning
case "ERROR":
return logs.LevelError
}
return logs.LevelDebug
}