@@ -17,6 +17,8 @@ class Rotation
17
17
18
18
private int $ _minSize = 0 ;
19
19
20
+ private bool $ _truncate = false ;
21
+
20
22
private $ thenCallback = null ;
21
23
22
24
public function __construct (array $ options = [])
@@ -25,6 +27,7 @@ public function __construct(array $options = [])
25
27
26
28
$ this ->methodsOptionables ([
27
29
'compress ' ,
30
+ 'truncate ' ,
28
31
'minSize ' ,
29
32
'files ' ,
30
33
'then ' ,
@@ -60,6 +63,20 @@ public function compress(bool $compress = true): self
60
63
return $ this ;
61
64
}
62
65
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
+
63
80
/**
64
81
* Log files are rotated when they grow bigger than size bytes.
65
82
*/
@@ -95,18 +112,28 @@ public function rotate(string $filename): bool
95
112
return false ;
96
113
}
97
114
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 (
99
124
$ filename ,
100
- $ this -> moveContentToTempFile ( $ filename )
125
+ $ fileTemporary
101
126
);
102
127
103
- $ filenameRotated = is_null ($ filenameRotated )
104
- ? $ filenameRotated
105
- : $ this -> runCompress ( $ filenameRotated );
128
+ if ( is_null ($ fileTarget )) {
129
+ return false ;
130
+ }
106
131
107
- $ this ->sucessfull ( $ filename , $ filenameRotated );
132
+ $ fileTarget = $ this ->runCompress ( $ fileTarget );
108
133
109
- return !empty ($ filenameRotated );
134
+ $ this ->sucessfull ($ filename , $ fileTarget );
135
+
136
+ return true ;
110
137
}
111
138
112
139
/**
@@ -186,9 +213,9 @@ private function fileIsValid(string $filename): bool
186
213
}
187
214
188
215
/**
189
- * move data to temp file and truncate.
216
+ * copy data to temp file and truncate.
190
217
*/
191
- private function moveContentToTempFile (string $ filename ): ?string
218
+ private function copyAndTruncate (string $ filename ): ?string
192
219
{
193
220
clearstatcache ();
194
221
@@ -247,4 +274,24 @@ private function moveContentToTempFile(string $filename): ?string
247
274
248
275
return $ filenameTarget ;
249
276
}
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
+ }
250
297
}
0 commit comments