/
option.go
112 lines (96 loc) · 2.16 KB
/
option.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
package xzap
import (
"fmt"
"path/filepath"
)
type OptionFunc func(*Option)
type Option struct {
Tags []string //TODO:记录zap的fileds
Development bool
LogFileDir string
AppName string
MaxSize int
MaxBackups int
MaxAge int
Level string
EnableCompress bool
EnableWriteFile bool
EnableWriteConsole bool
}
// 初始化log的配置
func newZaplogOptions(optFunctions ...OptionFunc) *Option {
var (
curPath string
err error
)
option := &Option{
Development: true,
AppName: "test-apps",
MaxSize: 100,
MaxBackups: 60,
MaxAge: 30,
Level: "debug",
EnableCompress: false,
EnableWriteFile: false,
EnableWriteConsole: true,
}
if curPath, err = filepath.Abs(filepath.Dir(filepath.Join("."))); err != nil {
panic(err)
}
option.LogFileDir = fmt.Sprintf("%s/log/", curPath)
for _, o := range optFunctions {
//apply options
o(option)
}
return option
}
func SetDevelopment(development bool) OptionFunc {
return func(options *Option) {
options.Development = development
}
}
func SetLogFileDir(logFileDir string) OptionFunc {
return func(options *Option) {
options.LogFileDir = logFileDir
}
}
func SetAppName(appName string) OptionFunc {
return func(options *Option) {
options.AppName = appName
}
}
func SetMaxSize(maxSize int) OptionFunc {
return func(options *Option) {
options.MaxSize = maxSize
}
}
func SetMaxBackups(maxBackups int) OptionFunc {
return func(options *Option) {
options.MaxBackups = maxBackups
}
}
func SetMaxAge(maxAge int) OptionFunc {
return func(options *Option) {
options.MaxAge = maxAge
}
}
func SetLevel(level string) OptionFunc {
return func(options *Option) {
options.Level = level
}
}
func SetWriteFile(writeFile bool) OptionFunc {
return func(options *Option) {
options.EnableWriteFile = writeFile
}
}
func SetLogCompress(isOn bool) OptionFunc {
return func(options *Option) {
options.EnableCompress = isOn
}
}
func SetWriteConsole(writeConsole bool) OptionFunc {
return func(options *Option) {
options.EnableWriteConsole = writeConsole
}
}