-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
93 lines (78 loc) · 1.74 KB
/
logger.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
package gbase
import (
"fmt"
"os"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
//Logger 按天记录日志
type dayLogger struct {
*log.Logger
}
//LogPath 日志路径
var LogPath = os.Getenv("LOG_PATH")
var (
_logger *dayLogger
loggerOnce sync.Once
)
func init() {
checkDir(LogPath)
}
//DayLogger 日志
func DayLogger() *dayLogger {
loggerOnce.Do(func() {
_logger = &dayLogger{log.New()}
_logger.Formatter = &log.JSONFormatter{}
})
return _logger
}
func checkDir(path string) bool {
if len(path) == 0 {
return false
}
_, err := os.Stat(path) //os.Stat获取文件信息
if err == nil {
return true
}
if os.IsExist(err) {
return true
}
if err := os.MkdirAll(path, os.ModePerm); err != nil {
log.WithFields(log.Fields{"err": err, "path": path}).Error("dir fail")
return false
}
return true
}
func (me *dayLogger) setFileOutput(filename string) bool {
logFile := fmt.Sprintf("%s/%s-%s.log", LogPath, filename, time.Now().Format("2006-01-02"))
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.WithFields(log.Fields{"err": err, "logfile": logFile}).Error("open fail")
return false
}
me.Out = file
return true
}
func (me *dayLogger) AddRoomLog(action string, data log.Fields, err error) {
if !me.setFileOutput("liveroom") {
return
}
ctxLogger := me.WithFields(data)
if err != nil {
ctxLogger.Error(err)
} else {
ctxLogger.Info(action)
}
}
func (me *dayLogger) AddLog(clientIP, logType string, data interface{}, lerr error) {
if !me.setFileOutput(logType) {
return
}
ctxLogger := me.WithFields(log.Fields{"ip": clientIP, "type": logType, "data": data})
if lerr != nil {
ctxLogger.Error(lerr)
} else {
ctxLogger.Info(logType + " success")
}
}