Use Copyfile() to back up the log, so as not to continue writing the log to the old file because the old fd is not closed
@@ -100,9 +100,13 @@ func (l *Logger) doRotateFile(ctx context.Context, filePath string) error {
}
}
intlog.Printf(ctx, "rotating file by size from %s to %s", filePath, newFilePath)
- if err := gfile.Rename(filePath, newFilePath); err != nil {
+
+ if err := gfile.CopyFile(filePath, newFilePath); err != nil {
+ intlog.Error(ctx, "copy file to: ", newFilePath, err)
return err
}
+ gfile.Truncate(filePath, 0)
+
return nil
}
