From b1c3c21c819f4926a80e517ff4d8faecd3bc9bd7 Mon Sep 17 00:00:00 2001 From: edieruby <469960757@qq.com> Date: Fri, 25 Nov 2022 23:48:32 +0800 Subject: [PATCH] fix: log currentSize should not be 0 when file exists and size is not 0 (#2639) --- core/logx/rotatelogger.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/logx/rotatelogger.go b/core/logx/rotatelogger.go index 0b8f0b14..926bb020 100644 --- a/core/logx/rotatelogger.go +++ b/core/logx/rotatelogger.go @@ -284,7 +284,7 @@ func (l *RotateLogger) getBackupFilename() string { func (l *RotateLogger) init() error { l.backup = l.rule.BackupFileName() - if _, err := os.Stat(l.filename); err != nil { + if fileInfo, err := os.Stat(l.filename); err != nil { basePath := path.Dir(l.filename) if _, err = os.Stat(basePath); err != nil { if err = os.MkdirAll(basePath, defaultDirMode); err != nil { @@ -295,8 +295,11 @@ func (l *RotateLogger) init() error { if l.fp, err = os.Create(l.filename); err != nil { return err } - } else if l.fp, err = os.OpenFile(l.filename, os.O_APPEND|os.O_WRONLY, defaultFileMode); err != nil { - return err + } else { + if l.fp, err = os.OpenFile(l.filename, os.O_APPEND|os.O_WRONLY, defaultFileMode); err != nil { + return err + } + l.currentSize = fileInfo.Size() } fs.CloseOnExec(l.fp)