logger.go
2.2 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package log
import (
"encoding/json"
"github.com/astaxie/beego/logs"
)
type SetLoggerConfig interface {
MarshalString() string
Name() string
}
//LoggerConsole 日志输出到命令行
type LoggerConsole struct {
Level int `json:"level,omitempty"`
Color bool `json:"color,omitempty"`
}
//LoggerFile 日志输出到文件
type LoggerFile struct {
FileName string `json:"filename,omitempty"`
Maxlines int `json:"maxlines,omitempty"`
Maxsize int `json:"maxsize,omitempty"`
Daily bool `json:"daily,omitempty"`
MaxDays int `json:"maxdays,omitempty"`
Rotate bool `json:"rotate,omitempty"`
Level int `json:"level,omitempty"`
}
var (
_ SetLoggerConfig = &LoggerConsole{}
_ SetLoggerConfig = &LoggerFile{}
)
// log level
const (
LogLevelError int = logs.LevelError
LogLevelWarn int = logs.LevelWarning
LogLevelInfo int = logs.LevelInformational
LogLevelDebug int = logs.LevelDebug
)
func (config LoggerConsole) MarshalString() string {
bytedata, err := json.Marshal(config)
if err != nil {
return ""
}
return string(bytedata)
}
func (config LoggerConsole) Name() string {
return logs.AdapterConsole
}
func (config LoggerFile) MarshalString() string {
bytedata, err := json.Marshal(config)
if err != nil {
return ""
}
return string(bytedata)
}
func (config LoggerFile) Name() string {
return logs.AdapterFile
}
var logger *logs.BeeLogger
func ResetLog(config SetLoggerConfig, funcCall bool) {
logger = logs.GetBeeLogger()
out := config.Name()
logconf := config.MarshalString()
logger.EnableFuncCallDepth(funcCall)
logger.SetLogFuncCallDepth(3)
logger.SetLogger(out, logconf)
return
}
//LogLevel ...
func LogLevel(s string) (i int) {
switch s {
case "info":
i = LogLevelInfo
case "debug":
i = LogLevelDebug
case "warning":
i = LogLevelWarn
case "error":
i = LogLevelError
default:
i = LogLevelDebug
}
return
}
func Info(format string, v ...interface{}) {
logger.Info(format, v...)
}
func Debug(format string, v ...interface{}) {
logger.Debug(format, v...)
}
func Error(format string, v ...interface{}) {
logger.Error(format, v...)
}
func Warn(format string, v ...interface{}) {
logger.Warn(format, v...)
}