@@ -17,6 +17,8 @@ class Rotation
1717
1818 private int $ _minSize = 0 ;
1919
20+ private bool $ _truncate = false ;
21+
2022 private $ thenCallback = null ;
2123
2224 public function __construct (array $ options = [])
@@ -25,6 +27,7 @@ public function __construct(array $options = [])
2527
2628 $ this ->methodsOptionables ([
2729 'compress ' ,
30+ 'truncate ' ,
2831 'minSize ' ,
2932 'files ' ,
3033 'then ' ,
@@ -60,6 +63,20 @@ public function compress(bool $compress = true): self
6063 return $ this ;
6164 }
6265
66+ /**
67+ * Truncate the original log file in place after creating a copy, instead of
68+ * moving the old log file.
69+ *
70+ * It can be used when some program cannot be told to close its logfile and
71+ * thus might continue writing (appending) to the previous log file forever.
72+ */
73+ public function truncate (bool $ truncate = true ): self
74+ {
75+ $ this ->_truncate = $ truncate ;
76+
77+ return $ this ;
78+ }
79+
6380 /**
6481 * Log files are rotated when they grow bigger than size bytes.
6582 */
@@ -95,18 +112,28 @@ public function rotate(string $filename): bool
95112 return false ;
96113 }
97114
98- $ filenameRotated = $ this ->runProcessor (
115+ $ fileTemporary = $ this ->_truncate
116+ ? $ this ->copyAndTruncate ($ filename )
117+ : $ this ->move ($ filename );
118+
119+ if (is_null ($ fileTemporary )) {
120+ return false ;
121+ }
122+
123+ $ fileTarget = $ this ->runProcessor (
99124 $ filename ,
100- $ this -> moveContentToTempFile ( $ filename )
125+ $ fileTemporary
101126 );
102127
103- $ filenameRotated = is_null ($ filenameRotated )
104- ? $ filenameRotated
105- : $ this -> runCompress ( $ filenameRotated );
128+ if ( is_null ($ fileTarget )) {
129+ return false ;
130+ }
106131
107- $ this ->sucessfull ( $ filename , $ filenameRotated );
132+ $ fileTarget = $ this ->runCompress ( $ fileTarget );
108133
109- return !empty ($ filenameRotated );
134+ $ this ->sucessfull ($ filename , $ fileTarget );
135+
136+ return true ;
110137 }
111138
112139 /**
@@ -186,9 +213,9 @@ private function fileIsValid(string $filename): bool
186213 }
187214
188215 /**
189- * move data to temp file and truncate.
216+ * copy data to temp file and truncate.
190217 */
191- private function moveContentToTempFile (string $ filename ): ?string
218+ private function copyAndTruncate (string $ filename ): ?string
192219 {
193220 clearstatcache ();
194221
@@ -247,4 +274,24 @@ private function moveContentToTempFile(string $filename): ?string
247274
248275 return $ filenameTarget ;
249276 }
277+
278+ private function move (string $ filename ): ?string
279+ {
280+ clearstatcache ();
281+
282+ $ filenameTarget = tempnam (dirname ($ filename ), 'LOG ' );
283+
284+ if (!rename ($ filename , $ filenameTarget )) {
285+ $ this ->exception (
286+ new Exception (
287+ sprintf ('the file %s not can move to temp file %s. ' , $ filename , $ filenameTarget ),
288+ 22
289+ )
290+ );
291+
292+ return null ;
293+ }
294+
295+ return $ filenameTarget ;
296+ }
250297}
0 commit comments