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