-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzlog_unix.go
78 lines (68 loc) · 2.67 KB
/
zlog_unix.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
//go:build !windows
// +build !windows
package zlog
import (
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap/zapcore"
"log"
"os"
"strings"
"time"
)
func getWriteSyncerInfo(fileName string) (zapcore.WriteSyncer, error) {
fileWriter, err := rotatelogs.New(
// %Y-%m-%d %H:%M:%S
strings.Replace(fileName, ".log", "", -1)+"%Y-%m-%d.log", // 没有使用go风格反人类的format格式
rotatelogs.WithLinkName(fileName),
rotatelogs.WithMaxAge(time.Duration(g_config.WithMaxAge)*time.Hour), // 保存最大的时间
rotatelogs.WithRotationTime(time.Duration(g_config.WithRotationTime)*time.Hour), // 切割时间
)
if getConfig().Env == "pro" {
// 只写入文件
return zapcore.AddSync(fileWriter), err
} else {
// 测试环境,则终端和文件都写入
return zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
}
}
func getWriteSyncerErr(fileName string) (zapcore.WriteSyncer, error) {
fileWriter, err := rotatelogs.New(
// %Y-%m-%d %H:%M:%S
strings.Replace(fileName, ".log", "", -1)+"%Y-%m-%d.log", // 没有使用go风格反人类的format格式
rotatelogs.WithLinkName(fileName),
// gxz 后面开启
rotatelogs.WithMaxAge(time.Duration(g_config.WithMaxAge)*time.Hour), // 保存最大的时间
rotatelogs.WithRotationTime(time.Duration(g_config.WithRotationTime)*time.Hour), // 切割时间
//
//rotatelogs.WithMaxAge(time.Duration(getConfig().WithMaxAge)*time.Hour),
//rotatelogs.WithRotationCount(7), // 做多保存多少分
//rotatelogs.WithRotationSize(1024*1024), // 10MB切割 , WithRotationSize 和 WithRotationTime 互斥
//rotatelogs.WithRotationTime(time.Duration(getConfig().WithRotationTime)*time.Hour),
)
return zapcore.AddSync(fileWriter), err
}
// 系统日志log 通过zap写入文件
func SetZapOut(fileName string) error {
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
fileWriter, err := rotatelogs.New(
// %Y-%m-%d %H:%M:%S
strings.Replace(fileName, ".log", "", -1)+"%Y-%m-%d.log", // 没有使用go风格反人类的format格式
rotatelogs.WithLinkName(fileName),
rotatelogs.WithRotationCount(7), // 做多保存多少分
rotatelogs.WithRotationSize(1024*1024*10), // 10MB切割 , WithRotationSize 和 WithRotationTime 互斥
)
// 测试环境,则终端和文件都写入
var w zapcore.WriteSyncer
if getConfig().Env == "pro" {
// 只写入文件
w = zapcore.AddSync(fileWriter)
} else {
// 测试环境,则终端和文件都写入
w, err = zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), zapcore.AddSync(fileWriter)), err
if err != nil {
return err
}
}
log.SetOutput(w)
return nil
}