This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
90 lines (79 loc) · 1.7 KB
/
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
package gsimplelog
import (
"errors"
"time"
)
const (
LogModeOff = "off"
LogModeConsole = "console"
LogModeFile = "file"
)
type ILogConfig interface {
GetLogLevel() string
}
func InitLogger(logLevel int, logMode, logPath, logPrefix string, rotateSize int) (err error) {
var l ILogger
switch logMode {
case LogModeOff:
l, err = NewSkipLogger()
if err != nil {
return errors.New("init logger failed:" + err.Error())
}
case LogModeConsole:
l, err = NewStdLogger(logLevel)
if err != nil {
return errors.New("init logger failed:" + err.Error())
}
case LogModeFile:
//multiSize: 50MB
l, err = NewFileLogger(logPrefix, logPath, logLevel, rotateSize)
if err != nil {
return errors.New("init logger failed:" + err.Error())
}
default:
return errors.New("not support LogMode:" + logMode)
}
Logger = l
return
}
func ApplyConfig(logConfig ILogConfig) error {
levelFlag, ok := LevelMap[logConfig.GetLogLevel()]
if !ok {
return errors.New("not support LogLevel:" + logConfig.GetLogLevel())
}
Logger.SetLevel(levelFlag)
return nil
}
var Logger ILogger = &StdLogger{Level: LogDebug}
func SetLogger(logger ILogger) {
Logger = logger
}
const (
LogTrace = 0
LogDebug = 1
LogInfo = 2
LogError = 3
LogOff = 4
)
var LevelMap = map[string]int{
"trace": LogTrace,
"debug": LogDebug,
"info": LogInfo,
"error": LogError,
"off": LogOff,
}
func Now() string {
return time.Now().Format("2006-01-02 15:04:05")
}
type ILogger interface {
SetLevel(int)
Trace(...interface{})
Debug(...interface{})
Info(...interface{})
Error(...interface{})
Tracef(string, ...interface{})
Debugf(string, ...interface{})
Infof(string, ...interface{})
Errorf(string, ...interface{})
Close() error
}