abstract-wc-address-provider.php
9 months ago
abstract-wc-data.php
4 months ago
abstract-wc-deprecated-hooks.php
6 years ago
abstract-wc-integration.php
5 years ago
abstract-wc-log-handler.php
2 years ago
abstract-wc-object-query.php
5 years ago
abstract-wc-order.php
1 month ago
abstract-wc-payment-gateway.php
1 month ago
abstract-wc-payment-token.php
5 years ago
abstract-wc-privacy.php
5 years ago
abstract-wc-product.php
1 month ago
abstract-wc-session.php
10 months ago
abstract-wc-settings-api.php
1 year ago
abstract-wc-shipping-method.php
4 months ago
abstract-wc-widget.php
5 years ago
class-wc-background-process.php
5 years ago
abstract-wc-log-handler.php
99 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Log handling functionality. |
| 4 | * |
| 5 | * @class WC_Log_Handler |
| 6 | * @package WooCommerce\Abstracts |
| 7 | */ |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly. |
| 11 | } |
| 12 | |
| 13 | /** |
| 14 | * Abstract WC Log Handler Class |
| 15 | * |
| 16 | * @version 1.0.0 |
| 17 | * @package WooCommerce\Abstracts |
| 18 | */ |
| 19 | abstract class WC_Log_Handler implements WC_Log_Handler_Interface { |
| 20 | |
| 21 | /** |
| 22 | * Formats a timestamp for use in log messages. |
| 23 | * |
| 24 | * @param int $timestamp Log timestamp. |
| 25 | * @return string Formatted time for use in log entry. |
| 26 | */ |
| 27 | protected static function format_time( $timestamp ) { |
| 28 | return gmdate( 'c', $timestamp ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Builds a log entry text from level, timestamp and message. |
| 33 | * |
| 34 | * @param int $timestamp Log timestamp. |
| 35 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 36 | * @param string $message Log message. |
| 37 | * @param array $context Additional information for log handlers. |
| 38 | * |
| 39 | * @return string Formatted log entry. |
| 40 | */ |
| 41 | protected static function format_entry( $timestamp, $level, $message, $context ) { |
| 42 | $time_string = self::format_time( $timestamp ); |
| 43 | $level_string = strtoupper( $level ); |
| 44 | $entry = "{$time_string} {$level_string} {$message}"; |
| 45 | |
| 46 | /** |
| 47 | * Filter the formatted log entry before it is written. |
| 48 | * |
| 49 | * @since 3.0.0 |
| 50 | * |
| 51 | * @param string $entry The formatted entry. |
| 52 | * @param array $args The raw data that gets assembled into a log entry. |
| 53 | */ |
| 54 | return apply_filters( |
| 55 | 'woocommerce_format_log_entry', |
| 56 | $entry, |
| 57 | array( |
| 58 | 'timestamp' => $timestamp, |
| 59 | 'level' => $level, |
| 60 | 'message' => $message, |
| 61 | 'context' => $context, |
| 62 | ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get a backtrace that shows where the logging function was called. |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | protected static function get_backtrace() { |
| 72 | // Get the filenames of the logging-related classes so we can ignore them. |
| 73 | $ignore_files = array_map( |
| 74 | function( $class ) { |
| 75 | try { |
| 76 | $reflector = new \ReflectionClass( $class ); |
| 77 | return $reflector->getFileName(); |
| 78 | } catch ( Exception $exception ) { |
| 79 | return null; |
| 80 | } |
| 81 | }, |
| 82 | array( wc_get_logger(), self::class, static::class ) |
| 83 | ); |
| 84 | |
| 85 | $backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace |
| 86 | |
| 87 | $filtered_backtrace = array_filter( |
| 88 | $backtrace, |
| 89 | function( $frame ) use ( $ignore_files ) { |
| 90 | $ignore = isset( $frame['file'] ) && in_array( $frame['file'], $ignore_files, true ); |
| 91 | |
| 92 | return ! $ignore; |
| 93 | } |
| 94 | ); |
| 95 | |
| 96 | return array_values( $filtered_backtrace ); |
| 97 | } |
| 98 | } |
| 99 |