Ver Fonte

Update 优化日志模块文件名自定义

YueYunyun há 2 anos atrás
pai
commit
fb97a7ebde

+ 7 - 4
SERVER/VberAdmin/core/tools/writer/file.go

@@ -38,6 +38,9 @@ func NewFileWriter(opts ...Option) (*FileWriter, error) {
 			log.Fatalf("[file] create dir error: %s", err.Error())
 		}
 	}
+	if p.opts.timeFormat == "" {
+		p.opts.timeFormat = timeFormat
+	}
 	var filename string
 	var err error
 	for {
@@ -84,7 +87,7 @@ func (p *FileWriter) write() {
 
 func (p *FileWriter) checkFile() {
 	info, _ := p.file.Stat()
-	if strings.Index(p.file.Name(), time.Now().Format(timeFormat)) < 0 ||
+	if strings.Index(p.file.Name(), time.Now().Format(p.opts.timeFormat)) < 0 ||
 		(p.opts.cap > 0 && uint(info.Size()) > p.opts.cap) {
 		//生成新文件
 		if uint(info.Size()) > p.opts.cap {
@@ -122,12 +125,12 @@ func (p *FileWriter) getFilename() string {
 	if p.opts.cap == 0 {
 		return filepath.Join(p.opts.path,
 			fmt.Sprintf("%s.%s",
-				time.Now().Format(timeFormat),
+				time.Now().Format(p.opts.timeFormat),
 				p.opts.suffix))
 	}
 	return filepath.Join(p.opts.path,
 		fmt.Sprintf("%s_%d.%s",
-			time.Now().Format(timeFormat),
-			p.num,
+			time.Now().Format(p.opts.timeFormat),
+			p.num+1,
 			p.opts.suffix))
 }

+ 10 - 4
SERVER/VberAdmin/core/tools/writer/options.go

@@ -2,14 +2,15 @@ package writer
 
 // Options 可配置参数
 type Options struct {
-	path   string
-	suffix string //文件扩展名
-	cap    uint
+	path       string
+	timeFormat string
+	suffix     string //文件扩展名
+	cap        uint
 }
 
 func setDefault() Options {
 	return Options{
-		path:   "/.logs/VberAdmin",
+		path:   "/.logs/IotAdmin",
 		suffix: "log",
 	}
 }
@@ -36,4 +37,9 @@ func WithCap(n uint) Option {
 	return func(o *Options) {
 		o.cap = n
 	}
+} // WithTimeFormat set timeFormat
+func WithTimeFormat(t string) Option {
+	return func(o *Options) {
+		o.timeFormat = t
+	}
 }