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