-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.go
143 lines (127 loc) · 3.24 KB
/
logs.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package logs
import (
"github.com/lestrrat/go-file-rotatelogs"
"github.com/pkg/errors"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
"os"
"path"
"runtime"
"strconv"
"strings"
"time"
)
/*
@Description: log分割
@Author : weixiaonan
@Time : 2019/8/12 21:05
*/
var (
logs = make(map[string]*log.Logger)
maxAge time.Duration
rotationTime time.Duration
logPath string
)
func InitLogs(path string) {
log.SetLevel(log.InfoLevel)
log.SetOutput(os.Stdout)
logPath = path
maxAge = 7*24*time.Hour
rotationTime = 24*time.Hour
}
func addNewLogFile(logName string) {
l:=log.New()
l.SetOutput(os.Stdout)
l.AddHook(newRotateHook(logPath, logName, maxAge, rotationTime))
logs[logName]=l
}
func newRotateHook(logPath string, logFileName string, maxAge time.Duration, rotationTime time.Duration) *lfshook.LfsHook {
baseLogPath := path.Join(logPath, "%Y-%m-%d."+logFileName+".log")
writer, err := rotatelogs.New(
baseLogPath,
rotatelogs.WithLinkName(path.Join(logPath,logFileName+".log")), // 生成软链,指向最新日志文
rotatelogs.WithMaxAge(maxAge), // 文件最大保存时间
rotatelogs.WithRotationTime(rotationTime), // 日志切割时间间隔
)
if err != nil {
log.Errorf("config local file system logger error. %+v", errors.WithStack(err))
}
return lfshook.NewHook(lfshook.WriterMap{
log.DebugLevel: writer, // 为不同级别设置不同的输出目的
log.InfoLevel: writer,
log.WarnLevel: writer,
log.ErrorLevel: writer,
log.FatalLevel: writer,
log.PanicLevel: writer,
}, &log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05"})
}
func checkLog(logName string,args ...interface{}) bool {
if logs[logName] == nil {
addNewLogFile(logName)
}
if len(args) <= 0 {
return false
}
return true
}
func Info(logName string,args ...interface{}) {
if !checkLog(logName,args){
return
}
logs[logName].Info(args, printCallerName())
}
func Debug(logName string,args ...interface{}) {
if !checkLog(logName,args){
return
}
logs[logName].Debug(args, printCallerName())
}
func Warn(logName string,args ...interface{}) {
if !checkLog(logName,args){
return
}
logs[logName].Warn(args, printCallerName())
}
func Error(logName string,args ...interface{}) {
if !checkLog(logName,args){
return
}
logs[logName].Error(args, printCallerName())
}
func Fatal(logName string,args ...interface{}) {
if !checkLog(logName,args){
return
}
logs[logName].Fatal(args, printCallerName())
}
func Panic(logName string,args ...interface{}) {
if !checkLog(logName,args){
return
}
logs[logName].Panic(args, printCallerName())
}
func printCallerName() string {
pc, file, line, _ := runtime.Caller(2)
lineNum := strconv.Itoa(line)
files := strings.Split(file, "/")
pcs := strings.Split(runtime.FuncForPC(pc).Name(), "/")
return " -->path: [func:" + pcs[len(pcs)-1] + "(" + files[len(files)-1] + ":" + lineNum + ")]"
}
func LogLevel(level string) error {
if level == "debug" {
for _, logger := range logs {
logger.SetLevel(log.DebugLevel)
}
} else if level == "info" {
for _, logger := range logs {
logger.SetLevel(log.InfoLevel)
}
}else if level == "error" {
for _, logger := range logs {
logger.SetLevel(log.ErrorLevel)
}
}else {
return errors.New("level error")
}
return nil
}