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.2.0 10.0.1 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3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.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / Admin / Logging / LogHandlerFileV2.php
woocommerce / src / Internal / Admin / Logging Last commit date
FileV2 2 weeks ago LogHandlerFileV2.php 2 weeks ago PageController.php 1 month ago Settings.php 1 month ago
LogHandlerFileV2.php
366 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Internal\Admin\Logging;
4
5 use Automattic\Jetpack\Constants;
6 use Automattic\WooCommerce\Internal\Admin\Logging\FileV2\{ File, FileController };
7 use WC_Log_Handler;
8
9 /**
10 * LogHandlerFileV2 class.
11 */
12 class LogHandlerFileV2 extends WC_Log_Handler {
13 /**
14 * Maximum number of log files to delete in one loop iteration.
15 *
16 * @var int
17 */
18 private const DELETE_BATCH_SIZE = 100;
19
20 /**
21 * Instance of the FileController class.
22 *
23 * @var FileController
24 */
25 private $file_controller;
26
27 /**
28 * LogHandlerFileV2 class.
29 */
30 public function __construct() {
31 $this->file_controller = wc_get_container()->get( FileController::class );
32 }
33
34 /**
35 * Handle a log entry.
36 *
37 * @param int $timestamp Log timestamp.
38 * @param string $level emergency|alert|critical|error|warning|notice|info|debug.
39 * @param string $message Log message.
40 * @param array $context {
41 * Optional. Additional information for log handlers. Any data can be added here, but there are some array
42 * keys that have special behavior.
43 *
44 * @type string $source Determines which log file to write to. Must be at least 3 characters in length.
45 * @type bool $backtrace True to include a backtrace that shows where the logging function got called.
46 * }
47 *
48 * @return bool False if value was not handled and true if value was handled.
49 */
50 public function handle( $timestamp, $level, $message, $context ) {
51 $context = (array) $context;
52
53 if ( isset( $context['source'] ) && is_string( $context['source'] ) && strlen( $context['source'] ) >= 3 ) {
54 $source = sanitize_title( trim( $context['source'] ) );
55 } else {
56 $source = $this->determine_source();
57 }
58
59 $entry = static::format_entry( $timestamp, $level, $message, $context );
60
61 $written = $this->file_controller->write_to_file( $source, $entry, $timestamp );
62
63 if ( $written ) {
64 $this->file_controller->invalidate_cache();
65 }
66
67 return $written;
68 }
69
70 /**
71 * Builds a log entry text from level, timestamp, and message.
72 *
73 * @param int $timestamp Log timestamp.
74 * @param string $level emergency|alert|critical|error|warning|notice|info|debug.
75 * @param string $message Log message.
76 * @param array $context Additional information for log handlers.
77 *
78 * @return string Formatted log entry.
79 */
80 protected static function format_entry( $timestamp, $level, $message, $context ) {
81 $time_string = static::format_time( $timestamp );
82 $level_string = strtoupper( $level );
83
84 if ( isset( $context['backtrace'] ) && true === filter_var( $context['backtrace'], FILTER_VALIDATE_BOOLEAN ) ) {
85 $context['backtrace'] = static::get_backtrace();
86 }
87
88 $context_for_entry = $context;
89 unset( $context_for_entry['source'] );
90
91 if ( ! empty( $context_for_entry ) ) {
92 // Keep the JSON flags in sync with the context re-encoding in PageController::format_line().
93 $formatted_context = wp_json_encode( $context_for_entry, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
94 $message .= " CONTEXT: $formatted_context";
95 }
96
97 $entry = "$time_string $level_string $message";
98
99 // phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment
100 /** This filter is documented in includes/abstracts/abstract-wc-log-handler.php */
101 return apply_filters(
102 'woocommerce_format_log_entry',
103 $entry,
104 array(
105 'timestamp' => $timestamp,
106 'level' => $level,
107 'message' => $message,
108 'context' => $context,
109 )
110 );
111 // phpcs:enable WooCommerce.Commenting.CommentHooks.MissingSinceComment
112 }
113
114 /**
115 * Figures out a source string to use for a log entry based on where the log method was called from.
116 *
117 * @return string
118 */
119 protected function determine_source(): string {
120 $source_roots = array(
121 'mu-plugin' => trailingslashit( Constants::get_constant( 'WPMU_PLUGIN_DIR' ) ),
122 'plugin' => trailingslashit( Constants::get_constant( 'WP_PLUGIN_DIR' ) ),
123 'theme' => trailingslashit( get_theme_root() ),
124 );
125
126 $source = '';
127 $backtrace = static::get_backtrace();
128
129 foreach ( $backtrace as $frame ) {
130 if ( ! isset( $frame['file'] ) ) {
131 continue;
132 }
133
134 foreach ( $source_roots as $type => $path ) {
135 if ( 0 === strpos( $frame['file'], $path ) ) {
136 $relative_path = trim( substr( $frame['file'], strlen( $path ) ), DIRECTORY_SEPARATOR );
137
138 if ( 'mu-plugin' === $type ) {
139 $info = pathinfo( $relative_path );
140
141 if ( '.' === $info['dirname'] ) {
142 $source = "$type-" . $info['filename'];
143 } else {
144 $source = "$type-" . $info['dirname'];
145 }
146
147 break 2;
148 }
149
150 $segments = explode( DIRECTORY_SEPARATOR, $relative_path );
151 if ( is_array( $segments ) ) {
152 $source = "$type-" . reset( $segments );
153 }
154
155 break 2;
156 }
157 }
158 }
159
160 if ( ! $source ) {
161 $source = 'log';
162 }
163
164 return sanitize_title( $source );
165 }
166
167 /**
168 * Delete all logs from a specific source.
169 *
170 * @param string $source The source of the log entries.
171 * @param bool $quiet Whether to suppress the deletion message.
172 *
173 * @return int The number of files that were deleted.
174 */
175 public function clear( string $source, bool $quiet = false ): int {
176 $source = File::sanitize_source( $source );
177
178 // Bail on an empty source: an empty value would match every file and,
179 // combined with the batched deletion below, wipe out all log files.
180 if ( '' === $source ) {
181 return 0;
182 }
183
184 $deleted = 0;
185 $skipped = 0;
186
187 /*
188 * Fetch and delete in batches so that sources with more than the default
189 * per-page of log files don't leave files behind.
190 *
191 * Order by 'created' rather than the default 'modified'. Because paging
192 * advances $skipped past undeletable files, the offset is only reliable if
193 * get_files() returns a stable, strict total order across iterations. For a
194 * single source, created timestamps (plus rotation) are unique per file,
195 * whereas modified times can tie -- and on PHP < 8.0 usort() is not stable,
196 * so tied files could re-order between iterations and strand a deletable file.
197 */
198 do {
199 $files = $this->file_controller->get_files(
200 array(
201 'source' => $source,
202 'exact_source' => true,
203 'orderby' => 'created',
204 'per_page' => self::DELETE_BATCH_SIZE,
205 'offset' => $skipped,
206 )
207 );
208
209 if ( is_wp_error( $files ) || ! is_array( $files ) ) {
210 break;
211 }
212
213 $fetched_count = count( $files );
214 if ( $fetched_count < 1 ) {
215 break;
216 }
217
218 $file_ids = array_map(
219 fn( $file ) => $file->get_file_id(),
220 $files
221 );
222
223 $deleted_in_batch = $this->file_controller->delete_files( $file_ids );
224 $deleted += $deleted_in_batch;
225
226 // Deleted files disappear from the directory, so only files that could
227 // not be deleted need to be skipped. This avoids retrying a permanently
228 // undeletable batch forever.
229 $skipped += $fetched_count - $deleted_in_batch;
230 } while ( self::DELETE_BATCH_SIZE === $fetched_count );
231
232 if ( $deleted > 0 && ! $quiet ) {
233 $this->handle(
234 time(),
235 'info',
236 sprintf(
237 esc_html(
238 // translators: %1$s is a number of log files, %2$s is a slug-style name for a file.
239 _n(
240 '%1$s log file from source %2$s was deleted.',
241 '%1$s log files from source %2$s were deleted.',
242 $deleted,
243 'woocommerce'
244 )
245 ),
246 number_format_i18n( $deleted ),
247 sprintf(
248 '<code>%s</code>',
249 esc_html( $source )
250 )
251 ),
252 array(
253 'source' => 'wc_logger',
254 'backtrace' => true,
255 )
256 );
257 }
258
259 return $deleted;
260 }
261
262 /**
263 * Delete all logs older than a specified timestamp.
264 *
265 * @param int $timestamp All files created before this timestamp will be deleted.
266 *
267 * @return int The number of files that were deleted.
268 */
269 public function delete_logs_before_timestamp( int $timestamp = 0 ): int {
270 if ( ! $timestamp ) {
271 return 0;
272 }
273
274 $deleted = 0;
275 $skipped = 0;
276
277 /*
278 * Fetch and delete in batches so that sites with more than the default
279 * per-page of log files don't leave expired files behind.
280 *
281 * Order by 'created' so that paging past vetoed files stays reliable: the
282 * offset only lands correctly if get_files() returns a strict, stable total
283 * order across iterations, and (created, source, rotation) is unique per file
284 * whereas the default 'modified' order can tie -- which PHP < 8.0's unstable
285 * usort() could re-order between iterations, stranding an expired file.
286 */
287 do {
288 $files = $this->file_controller->get_files(
289 array(
290 'date_filter' => 'created',
291 'date_start' => 1,
292 'date_end' => $timestamp,
293 'orderby' => 'created',
294 'per_page' => self::DELETE_BATCH_SIZE,
295 'offset' => $skipped,
296 )
297 );
298
299 if ( is_wp_error( $files ) || ! is_array( $files ) ) {
300 break;
301 }
302
303 $fetched_count = count( $files );
304 $files = array_filter(
305 $files,
306 function ( $file ) use ( $timestamp ) {
307 /**
308 * Allows preventing an expired log file from being deleted.
309 *
310 * @param bool $delete True to delete the file.
311 * @param File $file The log file object.
312 * @param int $timestamp The expiration threshold.
313 *
314 * @since 8.7.0
315 */
316 $delete = apply_filters( 'woocommerce_logger_delete_expired_file', true, $file, $timestamp );
317
318 return boolval( $delete );
319 }
320 );
321
322 $file_count = count( $files );
323 $vetoed_count = $fetched_count - $file_count;
324 $deleted_in_batch = 0;
325 if ( $file_count > 0 ) {
326 $file_ids = array_map(
327 fn( $file ) => $file->get_file_id(),
328 $files
329 );
330
331 $deleted_in_batch = $this->file_controller->delete_files( $file_ids );
332 $deleted += $deleted_in_batch;
333 }
334
335 // Deleted files disappear from the directory, so only vetoed files
336 // need to be skipped. If no progress was made, skip the full page to
337 // avoid retrying a permanently vetoed or undeletable batch forever.
338 $skipped += $deleted_in_batch > 0 ? $vetoed_count : $fetched_count;
339 } while ( self::DELETE_BATCH_SIZE === $fetched_count );
340
341 if ( $deleted > 0 ) {
342 $this->handle(
343 time(),
344 'info',
345 sprintf(
346 esc_html(
347 // translators: %s is a number of log files.
348 _n(
349 '%s expired log file was deleted.',
350 '%s expired log files were deleted.',
351 $deleted,
352 'woocommerce'
353 )
354 ),
355 number_format_i18n( $deleted )
356 ),
357 array(
358 'source' => 'wc_logger',
359 )
360 );
361 }
362
363 return $deleted;
364 }
365 }
366