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