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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 / includes / log-handlers / class-wc-log-handler-file.php
woocommerce / includes / log-handlers Last commit date
class-wc-log-handler-db.php 7 years ago class-wc-log-handler-email.php 8 years ago class-wc-log-handler-file.php 7 years ago
class-wc-log-handler-file.php
443 lines
1 <?php
2 /**
3 * Class WC_Log_Handler_File file.
4 *
5 * @package WooCommerce\Log Handlers
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Handles log entries by writing to a file.
14 *
15 * @class WC_Log_Handler_File
16 * @version 1.0.0
17 * @package WooCommerce/Classes/Log_Handlers
18 */
19 class WC_Log_Handler_File extends WC_Log_Handler {
20
21 /**
22 * Stores open file handles.
23 *
24 * @var array
25 */
26 protected $handles = array();
27
28 /**
29 * File size limit for log files in bytes.
30 *
31 * @var int
32 */
33 protected $log_size_limit;
34
35 /**
36 * Cache logs that could not be written.
37 *
38 * If a log is written too early in the request, pluggable functions may be unavailable. These
39 * logs will be cached and written on 'plugins_loaded' action.
40 *
41 * @var array
42 */
43 protected $cached_logs = array();
44
45 /**
46 * Constructor for the logger.
47 *
48 * @param int $log_size_limit Optional. Size limit for log files. Default 5mb.
49 */
50 public function __construct( $log_size_limit = null ) {
51 if ( null === $log_size_limit ) {
52 $log_size_limit = 5 * 1024 * 1024;
53 }
54
55 $this->log_size_limit = apply_filters( 'woocommerce_log_file_size_limit', $log_size_limit );
56
57 add_action( 'plugins_loaded', array( $this, 'write_cached_logs' ) );
58 }
59
60 /**
61 * Destructor.
62 *
63 * Cleans up open file handles.
64 */
65 public function __destruct() {
66 foreach ( $this->handles as $handle ) {
67 if ( is_resource( $handle ) ) {
68 fclose( $handle ); // @codingStandardsIgnoreLine.
69 }
70 }
71 }
72
73 /**
74 * Handle a log entry.
75 *
76 * @param int $timestamp Log timestamp.
77 * @param string $level emergency|alert|critical|error|warning|notice|info|debug.
78 * @param string $message Log message.
79 * @param array $context {
80 * Additional information for log handlers.
81 *
82 * @type string $source Optional. Determines log file to write to. Default 'log'.
83 * @type bool $_legacy Optional. Default false. True to use outdated log format
84 * originally used in deprecated WC_Logger::add calls.
85 * }
86 *
87 * @return bool False if value was not handled and true if value was handled.
88 */
89 public function handle( $timestamp, $level, $message, $context ) {
90
91 if ( isset( $context['source'] ) && $context['source'] ) {
92 $handle = $context['source'];
93 } else {
94 $handle = 'log';
95 }
96
97 $entry = self::format_entry( $timestamp, $level, $message, $context );
98
99 return $this->add( $entry, $handle );
100 }
101
102 /**
103 * Builds a log entry text from timestamp, level and message.
104 *
105 * @param int $timestamp Log timestamp.
106 * @param string $level emergency|alert|critical|error|warning|notice|info|debug.
107 * @param string $message Log message.
108 * @param array $context Additional information for log handlers.
109 *
110 * @return string Formatted log entry.
111 */
112 protected static function format_entry( $timestamp, $level, $message, $context ) {
113
114 if ( isset( $context['_legacy'] ) && true === $context['_legacy'] ) {
115 if ( isset( $context['source'] ) && $context['source'] ) {
116 $handle = $context['source'];
117 } else {
118 $handle = 'log';
119 }
120 $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
121 $time = date_i18n( 'm-d-Y @ H:i:s' );
122 $entry = "{$time} - {$message}";
123 } else {
124 $entry = parent::format_entry( $timestamp, $level, $message, $context );
125 }
126
127 return $entry;
128 }
129
130 /**
131 * Open log file for writing.
132 *
133 * @param string $handle Log handle.
134 * @param string $mode Optional. File mode. Default 'a'.
135 * @return bool Success.
136 */
137 protected function open( $handle, $mode = 'a' ) {
138 if ( $this->is_open( $handle ) ) {
139 return true;
140 }
141
142 $file = self::get_log_file_path( $handle );
143
144 if ( $file ) {
145 if ( ! file_exists( $file ) ) {
146 $temphandle = @fopen( $file, 'w+' ); // @codingStandardsIgnoreLine.
147 @fclose( $temphandle ); // @codingStandardsIgnoreLine.
148
149 if ( defined( 'FS_CHMOD_FILE' ) ) {
150 @chmod( $file, FS_CHMOD_FILE ); // @codingStandardsIgnoreLine.
151 }
152 }
153
154 $resource = @fopen( $file, $mode ); // @codingStandardsIgnoreLine.
155
156 if ( $resource ) {
157 $this->handles[ $handle ] = $resource;
158 return true;
159 }
160 }
161
162 return false;
163 }
164
165 /**
166 * Check if a handle is open.
167 *
168 * @param string $handle Log handle.
169 * @return bool True if $handle is open.
170 */
171 protected function is_open( $handle ) {
172 return array_key_exists( $handle, $this->handles ) && is_resource( $this->handles[ $handle ] );
173 }
174
175 /**
176 * Close a handle.
177 *
178 * @param string $handle Log handle.
179 * @return bool success
180 */
181 protected function close( $handle ) {
182 $result = false;
183
184 if ( $this->is_open( $handle ) ) {
185 $result = fclose( $this->handles[ $handle ] ); // @codingStandardsIgnoreLine.
186 unset( $this->handles[ $handle ] );
187 }
188
189 return $result;
190 }
191
192 /**
193 * Add a log entry to chosen file.
194 *
195 * @param string $entry Log entry text.
196 * @param string $handle Log entry handle.
197 *
198 * @return bool True if write was successful.
199 */
200 protected function add( $entry, $handle ) {
201 $result = false;
202
203 if ( $this->should_rotate( $handle ) ) {
204 $this->log_rotate( $handle );
205 }
206
207 if ( $this->open( $handle ) && is_resource( $this->handles[ $handle ] ) ) {
208 $result = fwrite( $this->handles[ $handle ], $entry . PHP_EOL ); // @codingStandardsIgnoreLine.
209 } else {
210 $this->cache_log( $entry, $handle );
211 }
212
213 return false !== $result;
214 }
215
216 /**
217 * Clear entries from chosen file.
218 *
219 * @param string $handle Log handle.
220 *
221 * @return bool
222 */
223 public function clear( $handle ) {
224 $result = false;
225
226 // Close the file if it's already open.
227 $this->close( $handle );
228
229 /**
230 * $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at
231 * the beginning of the file, and truncate the file to zero length.
232 */
233 if ( $this->open( $handle, 'w' ) && is_resource( $this->handles[ $handle ] ) ) {
234 $result = true;
235 }
236
237 do_action( 'woocommerce_log_clear', $handle );
238
239 return $result;
240 }
241
242 /**
243 * Remove/delete the chosen file.
244 *
245 * @param string $handle Log handle.
246 *
247 * @return bool
248 */
249 public function remove( $handle ) {
250 $removed = false;
251 $logs = $this->get_log_files();
252 $handle = sanitize_title( $handle );
253
254 if ( isset( $logs[ $handle ] ) && $logs[ $handle ] ) {
255 $file = realpath( trailingslashit( WC_LOG_DIR ) . $logs[ $handle ] );
256 if ( 0 === stripos( $file, realpath( trailingslashit( WC_LOG_DIR ) ) ) && is_file( $file ) && is_writable( $file ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
257 $this->close( $file ); // Close first to be certain no processes keep it alive after it is unlinked.
258 $removed = unlink( $file ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_unlink
259 }
260 do_action( 'woocommerce_log_remove', $handle, $removed );
261 }
262 return $removed;
263 }
264
265 /**
266 * Check if log file should be rotated.
267 *
268 * Compares the size of the log file to determine whether it is over the size limit.
269 *
270 * @param string $handle Log handle.
271 * @return bool True if if should be rotated.
272 */
273 protected function should_rotate( $handle ) {
274 $file = self::get_log_file_path( $handle );
275 if ( $file ) {
276 if ( $this->is_open( $handle ) ) {
277 $file_stat = fstat( $this->handles[ $handle ] );
278 return $file_stat['size'] > $this->log_size_limit;
279 } elseif ( file_exists( $file ) ) {
280 return filesize( $file ) > $this->log_size_limit;
281 } else {
282 return false;
283 }
284 } else {
285 return false;
286 }
287 }
288
289 /**
290 * Rotate log files.
291 *
292 * Logs are rotated by prepending '.x' to the '.log' suffix.
293 * The current log plus 10 historical logs are maintained.
294 * For example:
295 * base.9.log -> [ REMOVED ]
296 * base.8.log -> base.9.log
297 * ...
298 * base.0.log -> base.1.log
299 * base.log -> base.0.log
300 *
301 * @param string $handle Log handle.
302 */
303 protected function log_rotate( $handle ) {
304 for ( $i = 8; $i >= 0; $i-- ) {
305 $this->increment_log_infix( $handle, $i );
306 }
307 $this->increment_log_infix( $handle );
308 }
309
310 /**
311 * Increment a log file suffix.
312 *
313 * @param string $handle Log handle.
314 * @param null|int $number Optional. Default null. Log suffix number to be incremented.
315 * @return bool True if increment was successful, otherwise false.
316 */
317 protected function increment_log_infix( $handle, $number = null ) {
318 if ( null === $number ) {
319 $suffix = '';
320 $next_suffix = '.0';
321 } else {
322 $suffix = '.' . $number;
323 $next_suffix = '.' . ( $number + 1 );
324 }
325
326 $rename_from = self::get_log_file_path( "{$handle}{$suffix}" );
327 $rename_to = self::get_log_file_path( "{$handle}{$next_suffix}" );
328
329 if ( $this->is_open( $rename_from ) ) {
330 $this->close( $rename_from );
331 }
332
333 if ( is_writable( $rename_from ) ) { // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
334 return rename( $rename_from, $rename_to ); // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_rename
335 } else {
336 return false;
337 }
338
339 }
340
341 /**
342 * Get a log file path.
343 *
344 * @param string $handle Log name.
345 * @return bool|string The log file path or false if path cannot be determined.
346 */
347 public static function get_log_file_path( $handle ) {
348 if ( function_exists( 'wp_hash' ) ) {
349 return trailingslashit( WC_LOG_DIR ) . self::get_log_file_name( $handle );
350 } else {
351 wc_doing_it_wrong( __METHOD__, __( 'This method should not be called before plugins_loaded.', 'woocommerce' ), '3.0' );
352 return false;
353 }
354 }
355
356 /**
357 * Get a log file name.
358 *
359 * File names consist of the handle, followed by the date, followed by a hash, .log.
360 *
361 * @since 3.3
362 * @param string $handle Log name.
363 * @return bool|string The log file name or false if cannot be determined.
364 */
365 public static function get_log_file_name( $handle ) {
366 if ( function_exists( 'wp_hash' ) ) {
367 $date_suffix = date( 'Y-m-d', current_time( 'timestamp', true ) );
368 $hash_suffix = wp_hash( $handle );
369 return sanitize_file_name( implode( '-', array( $handle, $date_suffix, $hash_suffix ) ) . '.log' );
370 } else {
371 wc_doing_it_wrong( __METHOD__, __( 'This method should not be called before plugins_loaded.', 'woocommerce' ), '3.3' );
372 return false;
373 }
374 }
375
376 /**
377 * Cache log to write later.
378 *
379 * @param string $entry Log entry text.
380 * @param string $handle Log entry handle.
381 */
382 protected function cache_log( $entry, $handle ) {
383 $this->cached_logs[] = array(
384 'entry' => $entry,
385 'handle' => $handle,
386 );
387 }
388
389 /**
390 * Write cached logs.
391 */
392 public function write_cached_logs() {
393 foreach ( $this->cached_logs as $log ) {
394 $this->add( $log['entry'], $log['handle'] );
395 }
396 }
397
398 /**
399 * Delete all logs older than a defined timestamp.
400 *
401 * @since 3.4.0
402 * @param integer $timestamp Timestamp to delete logs before.
403 */
404 public static function delete_logs_before_timestamp( $timestamp = 0 ) {
405 if ( ! $timestamp ) {
406 return;
407 }
408
409 $log_files = self::get_log_files();
410
411 foreach ( $log_files as $log_file ) {
412 $last_modified = filemtime( trailingslashit( WC_LOG_DIR ) . $log_file );
413
414 if ( $last_modified < $timestamp ) {
415 @unlink( trailingslashit( WC_LOG_DIR ) . $log_file ); // @codingStandardsIgnoreLine.
416 }
417 }
418 }
419
420 /**
421 * Get all log files in the log directory.
422 *
423 * @since 3.4.0
424 * @return array
425 */
426 public static function get_log_files() {
427 $files = @scandir( WC_LOG_DIR ); // @codingStandardsIgnoreLine.
428 $result = array();
429
430 if ( ! empty( $files ) ) {
431 foreach ( $files as $key => $value ) {
432 if ( ! in_array( $value, array( '.', '..' ), true ) ) {
433 if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
434 $result[ sanitize_title( $value ) ] = $value;
435 }
436 }
437 }
438 }
439
440 return $result;
441 }
442 }
443