/
option.go
111 lines (94 loc) · 3.07 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
package options
import (
"encoding/json"
"unsafe"
"github.com/spf13/pflag"
)
type Option func(*Options)
type Options struct {
LogLevel Level `json:"level" mapstructure:"level"` //日志记录级别 ok
LogFormat string `json:"format" mapstructure:"format"` //日志类型,普通 或 json
Filename string `json:"filename" mapstructure:"filename"` //日志保存路径 ok
MaxSize int `json:"max-size" mapstructure:"max-size"` //日志分割的尺寸 MB //ok
MaxAge int `json:"max-age" mapstructure:"max-age"` //分割日志保存的时间 day
Stacktrace Level `json:"stacktrace" mapstructure:"stacktrace"` //记录堆栈的级别
IsStdOut bool `json:"is-stdout" mapstructure:"is-stdout"` //是否标准输出console输出
ProjectName string `json:"project-name" mapstructure:"project-name"` //项目名称
Color bool `json:"enable-color" mapstructure:"enable-color"`
}
func NewOptions() *Options {
return &Options{
LogLevel: LogLevel,
LogFormat: LogFormat,
Filename: Filename,
MaxSize: MaxSize,
MaxAge: MaxAge,
Stacktrace: Stacktrace,
IsStdOut: IsStdOut,
ProjectName: ProjectName,
Color: Color,
}
}
func (o *Options) Validate() []error {
return nil
}
func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.IntVar((*int)(unsafe.Pointer(&o.LogLevel)), "log.level", int(o.LogLevel), "Minimum log output `LEVEL`")
fs.StringVar(&o.LogFormat, "log.format", o.LogFormat, "Log output `FORMAT`")
fs.StringVar(&o.Filename, "log.filename", o.Filename, "Log output filename")
fs.IntVar(&o.MaxSize, "log.max-size", o.MaxSize, "Divided size of the log (MB)")
fs.IntVar(&o.MaxAge, "log.max-age", o.MaxAge, "Split log save time (day)")
fs.IntVar((*int)(unsafe.Pointer(&o.Stacktrace)), "log.stacktrace", int(o.Stacktrace), "Split log save time (day)")
fs.BoolVar(&o.IsStdOut, "log.is-stdout", o.IsStdOut, "Also log to console")
fs.StringVar(&o.ProjectName, "log.project-name", o.ProjectName, "Split log save time (day)")
fs.BoolVar(&o.Color, "log.enable-color", o.Color, "Whether to output colored log")
}
func WithLogLevel(loglevel Level) Option {
return func(o *Options) {
o.LogLevel = loglevel
}
}
func WithLogFormat(logformat string) Option {
return func(o *Options) {
o.LogFormat = logformat
}
}
func WithFilename(logpath string) Option {
return func(o *Options) {
o.Filename = logpath
}
}
func WithMaxSize(maxsize int) Option {
return func(o *Options) {
o.MaxSize = maxsize
}
}
func WithMaxAge(maxage int) Option {
return func(o *Options) {
o.MaxAge = maxage
}
}
func WithStacktrace(stacktrace Level) Option {
return func(o *Options) {
o.Stacktrace = stacktrace
}
}
func WithIsStdOut(isstdout bool) Option {
return func(o *Options) {
o.IsStdOut = isstdout
}
}
func WithProjectName(projectname string) Option {
return func(o *Options) {
o.ProjectName = projectname
}
}
func WithColor(color bool) Option {
return func(o *Options) {
o.Color = color
}
}
func (s *Options) String() string {
data, _ := json.Marshal(s)
return string(data)
}