abstracts
5 years ago
admin
5 years ago
cli
5 years ago
customizer
5 years ago
data-stores
4 years ago
emails
5 years ago
export
5 years ago
gateways
5 years ago
import
5 years ago
integrations
5 years ago
interfaces
5 years ago
legacy
5 years ago
libraries
5 years ago
log-handlers
5 years ago
payment-tokens
5 years ago
queue
5 years ago
shipping
5 years ago
shortcodes
5 years ago
theme-support
5 years ago
tracks
5 years ago
traits
5 years ago
walkers
5 years ago
wccom-site
5 years ago
widgets
5 years ago
class-wc-ajax.php
5 years ago
class-wc-api.php
5 years ago
class-wc-auth.php
5 years ago
class-wc-autoloader.php
5 years ago
class-wc-background-emailer.php
5 years ago
class-wc-background-updater.php
5 years ago
class-wc-breadcrumb.php
5 years ago
class-wc-cache-helper.php
5 years ago
class-wc-cart-fees.php
5 years ago
class-wc-cart-session.php
5 years ago
class-wc-cart-totals.php
5 years ago
class-wc-cart.php
5 years ago
class-wc-checkout.php
5 years ago
class-wc-cli.php
5 years ago
class-wc-comments.php
5 years ago
class-wc-countries.php
5 years ago
class-wc-coupon.php
5 years ago
class-wc-customer-download-log.php
5 years ago
class-wc-customer-download.php
5 years ago
class-wc-customer.php
5 years ago
class-wc-data-exception.php
5 years ago
class-wc-data-store.php
5 years ago
class-wc-datetime.php
5 years ago
class-wc-deprecated-action-hooks.php
5 years ago
class-wc-deprecated-filter-hooks.php
5 years ago
class-wc-discounts.php
5 years ago
class-wc-download-handler.php
5 years ago
class-wc-emails.php
5 years ago
class-wc-embed.php
5 years ago
class-wc-form-handler.php
5 years ago
class-wc-frontend-scripts.php
5 years ago
class-wc-geo-ip.php
5 years ago
class-wc-geolite-integration.php
5 years ago
class-wc-geolocation.php
5 years ago
class-wc-https.php
5 years ago
class-wc-install.php
4 years ago
class-wc-integrations.php
5 years ago
class-wc-log-levels.php
5 years ago
class-wc-logger.php
5 years ago
class-wc-meta-data.php
5 years ago
class-wc-order-factory.php
5 years ago
class-wc-order-item-coupon.php
5 years ago
class-wc-order-item-fee.php
5 years ago
class-wc-order-item-meta.php
5 years ago
class-wc-order-item-product.php
5 years ago
class-wc-order-item-shipping.php
5 years ago
class-wc-order-item-tax.php
5 years ago
class-wc-order-item.php
5 years ago
class-wc-order-query.php
5 years ago
class-wc-order-refund.php
5 years ago
class-wc-order.php
5 years ago
class-wc-payment-gateways.php
5 years ago
class-wc-payment-tokens.php
5 years ago
class-wc-post-data.php
5 years ago
class-wc-post-types.php
5 years ago
class-wc-privacy-background-process.php
5 years ago
class-wc-privacy-erasers.php
5 years ago
class-wc-privacy-exporters.php
5 years ago
class-wc-privacy.php
5 years ago
class-wc-product-attribute.php
5 years ago
class-wc-product-download.php
5 years ago
class-wc-product-external.php
5 years ago
class-wc-product-factory.php
5 years ago
class-wc-product-grouped.php
5 years ago
class-wc-product-query.php
5 years ago
class-wc-product-simple.php
5 years ago
class-wc-product-variable.php
5 years ago
class-wc-product-variation.php
5 years ago
class-wc-query.php
5 years ago
class-wc-rate-limiter.php
5 years ago
class-wc-regenerate-images-request.php
5 years ago
class-wc-regenerate-images.php
5 years ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
5 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 years ago
class-wc-shipping-rate.php
5 years ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
5 years ago
class-wc-shipping.php
5 years ago
class-wc-shortcodes.php
5 years ago
class-wc-structured-data.php
5 years ago
class-wc-tax.php
5 years ago
class-wc-template-loader.php
5 years ago
class-wc-tracker.php
5 years ago
class-wc-validation.php
5 years ago
class-wc-webhook.php
5 years ago
class-woocommerce.php
4 years ago
wc-account-functions.php
5 years ago
wc-attribute-functions.php
5 years ago
wc-cart-functions.php
5 years ago
wc-conditional-functions.php
5 years ago
wc-core-functions.php
5 years ago
wc-coupon-functions.php
5 years ago
wc-deprecated-functions.php
5 years ago
wc-formatting-functions.php
5 years ago
wc-notice-functions.php
5 years ago
wc-order-functions.php
5 years ago
wc-order-item-functions.php
5 years ago
wc-page-functions.php
5 years ago
wc-product-functions.php
5 years ago
wc-rest-functions.php
5 years ago
wc-stock-functions.php
5 years ago
wc-template-functions.php
5 years ago
wc-template-hooks.php
5 years ago
wc-term-functions.php
5 years ago
wc-update-functions.php
4 years ago
wc-user-functions.php
5 years ago
wc-webhook-functions.php
5 years ago
wc-widget-functions.php
5 years ago
class-wc-logger.php
304 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Provides logging capabilities for debugging purposes. |
| 4 | * |
| 5 | * @class WC_Logger |
| 6 | * @version 2.0.0 |
| 7 | * @package WooCommerce/Classes |
| 8 | */ |
| 9 | |
| 10 | use Automattic\Jetpack\Constants; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * WC_Logger class. |
| 16 | */ |
| 17 | class WC_Logger implements WC_Logger_Interface { |
| 18 | |
| 19 | /** |
| 20 | * Stores registered log handlers. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | protected $handlers; |
| 25 | |
| 26 | /** |
| 27 | * Minimum log level this handler will process. |
| 28 | * |
| 29 | * @var int Integer representation of minimum log level to handle. |
| 30 | */ |
| 31 | protected $threshold; |
| 32 | |
| 33 | /** |
| 34 | * Constructor for the logger. |
| 35 | * |
| 36 | * @param array $handlers Optional. Array of log handlers. If $handlers is not provided, the filter 'woocommerce_register_log_handlers' will be used to define the handlers. If $handlers is provided, the filter will not be applied and the handlers will be used directly. |
| 37 | * @param string $threshold Optional. Define an explicit threshold. May be configured via WC_LOG_THRESHOLD. By default, all logs will be processed. |
| 38 | */ |
| 39 | public function __construct( $handlers = null, $threshold = null ) { |
| 40 | if ( null === $handlers ) { |
| 41 | $handlers = apply_filters( 'woocommerce_register_log_handlers', array() ); |
| 42 | } |
| 43 | |
| 44 | $register_handlers = array(); |
| 45 | |
| 46 | if ( ! empty( $handlers ) && is_array( $handlers ) ) { |
| 47 | foreach ( $handlers as $handler ) { |
| 48 | $implements = class_implements( $handler ); |
| 49 | if ( is_object( $handler ) && is_array( $implements ) && in_array( 'WC_Log_Handler_Interface', $implements, true ) ) { |
| 50 | $register_handlers[] = $handler; |
| 51 | } else { |
| 52 | wc_doing_it_wrong( |
| 53 | __METHOD__, |
| 54 | sprintf( |
| 55 | /* translators: 1: class name 2: WC_Log_Handler_Interface */ |
| 56 | __( 'The provided handler %1$s does not implement %2$s.', 'woocommerce' ), |
| 57 | '<code>' . esc_html( is_object( $handler ) ? get_class( $handler ) : $handler ) . '</code>', |
| 58 | '<code>WC_Log_Handler_Interface</code>' |
| 59 | ), |
| 60 | '3.0' |
| 61 | ); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Support the constant as long as a valid log level has been set for it. |
| 67 | if ( null === $threshold ) { |
| 68 | $threshold = Constants::get_constant( 'WC_LOG_THRESHOLD' ); |
| 69 | if ( null !== $threshold && ! WC_Log_Levels::is_valid_level( $threshold ) ) { |
| 70 | $threshold = null; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if ( null !== $threshold ) { |
| 75 | $threshold = WC_Log_Levels::get_level_severity( $threshold ); |
| 76 | } |
| 77 | |
| 78 | $this->handlers = $register_handlers; |
| 79 | $this->threshold = $threshold; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Determine whether to handle or ignore log. |
| 84 | * |
| 85 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug. |
| 86 | * @return bool True if the log should be handled. |
| 87 | */ |
| 88 | protected function should_handle( $level ) { |
| 89 | if ( null === $this->threshold ) { |
| 90 | return true; |
| 91 | } |
| 92 | return $this->threshold <= WC_Log_Levels::get_level_severity( $level ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Add a log entry. |
| 97 | * |
| 98 | * This is not the preferred method for adding log messages. Please use log() or any one of |
| 99 | * the level methods (debug(), info(), etc.). This method may be deprecated in the future. |
| 100 | * |
| 101 | * @param string $handle File handle. |
| 102 | * @param string $message Message to log. |
| 103 | * @param string $level Logging level. |
| 104 | * @return bool |
| 105 | */ |
| 106 | public function add( $handle, $message, $level = WC_Log_Levels::NOTICE ) { |
| 107 | $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle ); |
| 108 | $this->log( |
| 109 | $level, |
| 110 | $message, |
| 111 | array( |
| 112 | 'source' => $handle, |
| 113 | '_legacy' => true, |
| 114 | ) |
| 115 | ); |
| 116 | wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '3.0', 'This action has been deprecated with no alternative.' ); |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Add a log entry. |
| 122 | * |
| 123 | * @param string $level One of the following: |
| 124 | * 'emergency': System is unusable. |
| 125 | * 'alert': Action must be taken immediately. |
| 126 | * 'critical': Critical conditions. |
| 127 | * 'error': Error conditions. |
| 128 | * 'warning': Warning conditions. |
| 129 | * 'notice': Normal but significant condition. |
| 130 | * 'info': Informational messages. |
| 131 | * 'debug': Debug-level messages. |
| 132 | * @param string $message Log message. |
| 133 | * @param array $context Optional. Additional information for log handlers. |
| 134 | */ |
| 135 | public function log( $level, $message, $context = array() ) { |
| 136 | if ( ! WC_Log_Levels::is_valid_level( $level ) ) { |
| 137 | /* translators: 1: WC_Logger::log 2: level */ |
| 138 | wc_doing_it_wrong( __METHOD__, sprintf( __( '%1$s was called with an invalid level "%2$s".', 'woocommerce' ), '<code>WC_Logger::log</code>', $level ), '3.0' ); |
| 139 | } |
| 140 | |
| 141 | if ( $this->should_handle( $level ) ) { |
| 142 | $timestamp = current_time( 'timestamp', 1 ); |
| 143 | $message = apply_filters( 'woocommerce_logger_log_message', $message, $level, $context ); |
| 144 | |
| 145 | foreach ( $this->handlers as $handler ) { |
| 146 | $handler->handle( $timestamp, $level, $message, $context ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Adds an emergency level message. |
| 153 | * |
| 154 | * System is unusable. |
| 155 | * |
| 156 | * @see WC_Logger::log |
| 157 | * |
| 158 | * @param string $message Message to log. |
| 159 | * @param array $context Log context. |
| 160 | */ |
| 161 | public function emergency( $message, $context = array() ) { |
| 162 | $this->log( WC_Log_Levels::EMERGENCY, $message, $context ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Adds an alert level message. |
| 167 | * |
| 168 | * Action must be taken immediately. |
| 169 | * Example: Entire website down, database unavailable, etc. |
| 170 | * |
| 171 | * @see WC_Logger::log |
| 172 | * |
| 173 | * @param string $message Message to log. |
| 174 | * @param array $context Log context. |
| 175 | */ |
| 176 | public function alert( $message, $context = array() ) { |
| 177 | $this->log( WC_Log_Levels::ALERT, $message, $context ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Adds a critical level message. |
| 182 | * |
| 183 | * Critical conditions. |
| 184 | * Example: Application component unavailable, unexpected exception. |
| 185 | * |
| 186 | * @see WC_Logger::log |
| 187 | * |
| 188 | * @param string $message Message to log. |
| 189 | * @param array $context Log context. |
| 190 | */ |
| 191 | public function critical( $message, $context = array() ) { |
| 192 | $this->log( WC_Log_Levels::CRITICAL, $message, $context ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Adds an error level message. |
| 197 | * |
| 198 | * Runtime errors that do not require immediate action but should typically be logged |
| 199 | * and monitored. |
| 200 | * |
| 201 | * @see WC_Logger::log |
| 202 | * |
| 203 | * @param string $message Message to log. |
| 204 | * @param array $context Log context. |
| 205 | */ |
| 206 | public function error( $message, $context = array() ) { |
| 207 | $this->log( WC_Log_Levels::ERROR, $message, $context ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Adds a warning level message. |
| 212 | * |
| 213 | * Exceptional occurrences that are not errors. |
| 214 | * |
| 215 | * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not |
| 216 | * necessarily wrong. |
| 217 | * |
| 218 | * @see WC_Logger::log |
| 219 | * |
| 220 | * @param string $message Message to log. |
| 221 | * @param array $context Log context. |
| 222 | */ |
| 223 | public function warning( $message, $context = array() ) { |
| 224 | $this->log( WC_Log_Levels::WARNING, $message, $context ); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Adds a notice level message. |
| 229 | * |
| 230 | * Normal but significant events. |
| 231 | * |
| 232 | * @see WC_Logger::log |
| 233 | * |
| 234 | * @param string $message Message to log. |
| 235 | * @param array $context Log context. |
| 236 | */ |
| 237 | public function notice( $message, $context = array() ) { |
| 238 | $this->log( WC_Log_Levels::NOTICE, $message, $context ); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Adds a info level message. |
| 243 | * |
| 244 | * Interesting events. |
| 245 | * Example: User logs in, SQL logs. |
| 246 | * |
| 247 | * @see WC_Logger::log |
| 248 | * |
| 249 | * @param string $message Message to log. |
| 250 | * @param array $context Log context. |
| 251 | */ |
| 252 | public function info( $message, $context = array() ) { |
| 253 | $this->log( WC_Log_Levels::INFO, $message, $context ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Adds a debug level message. |
| 258 | * |
| 259 | * Detailed debug information. |
| 260 | * |
| 261 | * @see WC_Logger::log |
| 262 | * |
| 263 | * @param string $message Message to log. |
| 264 | * @param array $context Log context. |
| 265 | */ |
| 266 | public function debug( $message, $context = array() ) { |
| 267 | $this->log( WC_Log_Levels::DEBUG, $message, $context ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Clear entries for a chosen file/source. |
| 272 | * |
| 273 | * @param string $source Source/handle to clear. |
| 274 | * @return bool |
| 275 | */ |
| 276 | public function clear( $source = '' ) { |
| 277 | if ( ! $source ) { |
| 278 | return false; |
| 279 | } |
| 280 | foreach ( $this->handlers as $handler ) { |
| 281 | if ( is_callable( array( $handler, 'clear' ) ) ) { |
| 282 | $handler->clear( $source ); |
| 283 | } |
| 284 | } |
| 285 | return true; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Clear all logs older than a defined number of days. Defaults to 30 days. |
| 290 | * |
| 291 | * @since 3.4.0 |
| 292 | */ |
| 293 | public function clear_expired_logs() { |
| 294 | $days = absint( apply_filters( 'woocommerce_logger_days_to_retain_logs', 30 ) ); |
| 295 | $timestamp = strtotime( "-{$days} days" ); |
| 296 | |
| 297 | foreach ( $this->handlers as $handler ) { |
| 298 | if ( is_callable( array( $handler, 'delete_logs_before_timestamp' ) ) ) { |
| 299 | $handler->delete_logs_before_timestamp( $timestamp ); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 |