@@ -12,32 +12,24 @@ import (
1212)
1313
1414type options struct {
15- // Dir 日志输出目录
16- Dir string `mapStructure:"dir"`
17- // Name 日志文件名称
18- Name string `mapStructure:"name"`
19- // Level 日志初始等级
20- Level string `mapStructure:"level"`
21- // 日志初始化字段
22- Fields []zap.Field `mapStructure:"fields"`
23- // 是否添加调用者信息
24- AddCaller bool `mapStructure:"add_caller"`
25- // 日志前缀
26- Prefix string `mapStructure:"prefix"`
27- // 日志输出文件最大长度,超过改值则截断
28- MaxSize int `mapStructure:"max_size"`
29- MaxAge int `mapStructure:"max_age"`
30- MaxBackup int `mapStructure:"max_backup"`
31- // 日志磁盘刷盘间隔
32- Interval time.Duration `mapStructure:"interval"`
33- CallerSkip int `mapStructure:"caller_skip"`
34- Async bool `mapStructure:"async"`
35- Queue bool `mapStructure:"queue"`
36- QueueSleep time.Duration `mapStructure:"queue_sleep"`
37- Core zapcore.Core `mapStructure:"core"`
38- Debug bool `mapStructure:"debug"`
39- EncoderConfig * zapcore.EncoderConfig `mapStructure:"encoder_config"`
40- ConfigKey string `mapStructure:"config_key"`
15+ Dir string `mapStructure:"dir"` //Dir 日志输出目录
16+ Name string `mapStructure:"name"` //Name 日志文件名称
17+ Level string `mapStructure:"level"` //Level 日志初始等级
18+ AddCaller bool `mapStructure:"add_caller"` //是否添加调用者信息
19+ MaxSize int `mapStructure:"max_size"` //日志输出文件最大长度,超过改值则截断,默认500M
20+ MaxAge int `mapStructure:"max_age"` //日志存储最大时间,默认最大保存天数为7天
21+ MaxBackup int `mapStructure:"max_backup"` //日志存储最大数量,默认最大保存文件个数为10个
22+ Interval time.Duration `mapStructure:"interval"` //日志轮转时间,默认1天
23+ CallerSkip int `mapStructure:"caller_skip"` //调用堆栈
24+ Async bool `mapStructure:"async"` //是否异步,默认异步
25+ ConfigKey string `mapStructure:"config_key"` //config key
26+ Debug bool `mapStructure:"debug"` //debug
27+ FlushBufferSize int `mapStructure:"flush_buffer_size"` //缓冲大小,默认256 * 1024B
28+ FlushBufferInterval time.Duration `mapStructure:"flush_buffer_interval"` //缓冲时间,默认30秒
29+
30+ fields []zap.Field `mapStructure:"fields"` //日志初始化字段
31+ encoderConfig * zapcore.EncoderConfig `mapStructure:"encoder_config"`
32+ core zapcore.Core `mapStructure:"core"`
4133}
4234
4335// Filename ...
@@ -70,32 +62,39 @@ func StdConfig(name ...string) *options {
7062// DefaultConfig ...
7163func defaultConfig () * options {
7264 return & options {
73- Name : "log.log" ,
74- Dir : "." ,
75- Level : "" ,
76- MaxSize : 500 , // 500M
77- MaxAge : 1 , // 1 day
78- MaxBackup : 10 , // 10 backup
79- Interval : 24 * time .Hour ,
80- CallerSkip : 2 ,
81- AddCaller : true ,
82- Debug : true ,
83- Async : false ,
84- Queue : false ,
85- QueueSleep : 100 * time .Millisecond ,
86- EncoderConfig : DefaultZapConfig (),
65+ Dir : "." ,
66+ Name : "log.log" ,
67+ Level : "info" ,
68+ AddCaller : true ,
69+ MaxSize : 500 , // 500M
70+ MaxAge : 1 , // 1 day
71+ MaxBackup : 10 , // 10 backup
72+ Interval : 24 * time .Hour ,
73+ CallerSkip : 2 ,
74+ Async : false ,
75+ ConfigKey : "xlog" ,
76+ Debug : true ,
77+ FlushBufferSize : defaultBufferSize ,
78+ FlushBufferInterval : defaultFlushInterval ,
79+ fields : nil ,
80+ encoderConfig : DefaultZapConfig (),
81+ core : nil ,
8782 }
8883}
8984
9085// Build ...
91- func (o options ) Build () * Logger {
92- if o .EncoderConfig == nil {
93- o .EncoderConfig = DefaultZapConfig ()
86+ func (o * options ) Build (options ... Option ) * Logger {
87+ for _ , option := range options {
88+ option (o )
89+ }
90+ if o .encoderConfig == nil {
91+ o .encoderConfig = DefaultZapConfig ()
9492 }
9593 if o .Debug {
96- o .EncoderConfig .EncodeLevel = DebugEncodeLevel
94+ o .encoderConfig .EncodeLevel = DebugEncodeLevel
95+ o .encoderConfig .EncodeTime = timeDebugEncoder
9796 }
98- logger := newLogger (& o )
97+ logger := newLogger (o )
9998 if o .ConfigKey != "" {
10099 logger .AutoLevel (o .ConfigKey + ".level" )
101100 }
@@ -137,3 +136,51 @@ func DebugEncodeLevel(lv zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
137136func timeEncoder (t time.Time , enc zapcore.PrimitiveArrayEncoder ) {
138137 enc .AppendInt64 (t .Unix ())
139138}
139+
140+ func timeDebugEncoder (t time.Time , enc zapcore.PrimitiveArrayEncoder ) {
141+ enc .AppendString (t .Format ("2006-01-02 15:04:05" ))
142+ }
143+
144+ // Option 可选项
145+ type Option func (c * options )
146+
147+ // WithFileName 设置文件名
148+ func WithFileName (name string ) Option {
149+ return func (c * options ) {
150+ c .Name = name
151+ }
152+ }
153+
154+ // WithDebug 设置在命令行显示
155+ func WithDebug (debug bool ) Option {
156+ return func (c * options ) {
157+ c .Debug = debug
158+ }
159+ }
160+
161+ // WithLevel 设置级别
162+ func WithLevel (level string ) Option {
163+ return func (c * options ) {
164+ c .Level = level
165+ }
166+ }
167+
168+ // WithEnableAsync 是否异步执行,默认异步
169+ func WithEnableAsync (enableAsync bool ) Option {
170+ return func (c * options ) {
171+ c .Async = enableAsync
172+ }
173+ }
174+
175+ // WithEnableAddCaller 是否添加行号,默认不添加行号
176+ func WithEnableAddCaller (enableAddCaller bool ) Option {
177+ return func (c * options ) {
178+ c .AddCaller = enableAddCaller
179+ }
180+ }
181+
182+ func WithFields (f []Field ) Option {
183+ return func (c * options ) {
184+ c .fields = f
185+ }
186+ }
0 commit comments