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