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2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.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 5 days ago LogHandlerFileV2.php 5 days ago PageController.php 5 days ago Settings.php 5 days ago
LogHandlerFileV2.php
320 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 expired 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 $files = $this->file_controller->get_files(
179 array(
180 'source' => $source,
181 )
182 );
183
184 if ( is_wp_error( $files ) || ! is_array( $files ) || count( $files ) < 1 ) {
185 return 0;
186 }
187
188 $file_ids = array_map(
189 fn( $file ) => $file->get_file_id(),
190 $files
191 );
192
193 $deleted = $this->file_controller->delete_files( $file_ids );
194
195 if ( $deleted > 0 && ! $quiet ) {
196 $this->handle(
197 time(),
198 'info',
199 sprintf(
200 esc_html(
201 // translators: %1$s is a number of log files, %2$s is a slug-style name for a file.
202 _n(
203 '%1$s log file from source %2$s was deleted.',
204 '%1$s log files from source %2$s were deleted.',
205 $deleted,
206 'woocommerce'
207 )
208 ),
209 number_format_i18n( $deleted ),
210 sprintf(
211 '<code>%s</code>',
212 esc_html( $source )
213 )
214 ),
215 array(
216 'source' => 'wc_logger',
217 'backtrace' => true,
218 )
219 );
220 }
221
222 return $deleted;
223 }
224
225 /**
226 * Delete all logs older than a specified timestamp.
227 *
228 * @param int $timestamp All files created before this timestamp will be deleted.
229 *
230 * @return int The number of files that were deleted.
231 */
232 public function delete_logs_before_timestamp( int $timestamp = 0 ): int {
233 if ( ! $timestamp ) {
234 return 0;
235 }
236
237 $deleted = 0;
238 $skipped = 0;
239
240 // Fetch and delete in batches so that sites with more than the default
241 // per-page of log files don't leave expired files behind.
242 do {
243 $files = $this->file_controller->get_files(
244 array(
245 'date_filter' => 'created',
246 'date_start' => 1,
247 'date_end' => $timestamp,
248 'per_page' => self::DELETE_BATCH_SIZE,
249 'offset' => $skipped,
250 )
251 );
252
253 if ( is_wp_error( $files ) || ! is_array( $files ) ) {
254 break;
255 }
256
257 $fetched_count = count( $files );
258 $files = array_filter(
259 $files,
260 function ( $file ) use ( $timestamp ) {
261 /**
262 * Allows preventing an expired log file from being deleted.
263 *
264 * @param bool $delete True to delete the file.
265 * @param File $file The log file object.
266 * @param int $timestamp The expiration threshold.
267 *
268 * @since 8.7.0
269 */
270 $delete = apply_filters( 'woocommerce_logger_delete_expired_file', true, $file, $timestamp );
271
272 return boolval( $delete );
273 }
274 );
275
276 $file_count = count( $files );
277 $vetoed_count = $fetched_count - $file_count;
278 $deleted_in_batch = 0;
279 if ( $file_count > 0 ) {
280 $file_ids = array_map(
281 fn( $file ) => $file->get_file_id(),
282 $files
283 );
284
285 $deleted_in_batch = $this->file_controller->delete_files( $file_ids );
286 $deleted += $deleted_in_batch;
287 }
288
289 // Deleted files disappear from the directory, so only vetoed files
290 // need to be skipped. If no progress was made, skip the full page to
291 // avoid retrying a permanently vetoed or undeletable batch forever.
292 $skipped += $deleted_in_batch > 0 ? $vetoed_count : $fetched_count;
293 } while ( self::DELETE_BATCH_SIZE === $fetched_count );
294
295 if ( $deleted > 0 ) {
296 $this->handle(
297 time(),
298 'info',
299 sprintf(
300 esc_html(
301 // translators: %s is a number of log files.
302 _n(
303 '%s expired log file was deleted.',
304 '%s expired log files were deleted.',
305 $deleted,
306 'woocommerce'
307 )
308 ),
309 number_format_i18n( $deleted )
310 ),
311 array(
312 'source' => 'wc_logger',
313 )
314 );
315 }
316
317 return $deleted;
318 }
319 }
320