abstracts
8 years ago
admin
8 years ago
api
8 years ago
cli
8 years ago
customizer
8 years ago
data-stores
8 years ago
emails
8 years ago
export
8 years ago
gateways
8 years ago
import
8 years ago
interfaces
8 years ago
legacy
8 years ago
libraries
8 years ago
log-handlers
8 years ago
payment-tokens
9 years ago
shipping
8 years ago
shortcodes
8 years ago
theme-support
8 years ago
walkers
8 years ago
widgets
8 years ago
class-wc-ajax.php
8 years ago
class-wc-api.php
8 years ago
class-wc-auth.php
8 years ago
class-wc-autoloader.php
8 years ago
class-wc-background-emailer.php
8 years ago
class-wc-background-updater.php
8 years ago
class-wc-breadcrumb.php
8 years ago
class-wc-cache-helper.php
8 years ago
class-wc-cart-fees.php
8 years ago
class-wc-cart-session.php
8 years ago
class-wc-cart-totals.php
8 years ago
class-wc-cart.php
8 years ago
class-wc-checkout.php
8 years ago
class-wc-cli.php
9 years ago
class-wc-comments.php
8 years ago
class-wc-countries.php
8 years ago
class-wc-coupon.php
8 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
8 years ago
class-wc-customer.php
8 years ago
class-wc-data-exception.php
9 years ago
class-wc-data-store.php
8 years ago
class-wc-datetime.php
8 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
8 years ago
class-wc-discounts.php
8 years ago
class-wc-download-handler.php
8 years ago
class-wc-emails.php
8 years ago
class-wc-embed.php
9 years ago
class-wc-form-handler.php
8 years ago
class-wc-frontend-scripts.php
8 years ago
class-wc-geo-ip.php
9 years ago
class-wc-geolocation.php
8 years ago
class-wc-https.php
9 years ago
class-wc-install.php
8 years ago
class-wc-integrations.php
9 years ago
class-wc-log-levels.php
9 years ago
class-wc-logger.php
8 years ago
class-wc-meta-data.php
8 years ago
class-wc-order-factory.php
8 years ago
class-wc-order-item-coupon.php
8 years ago
class-wc-order-item-fee.php
8 years ago
class-wc-order-item-meta.php
8 years ago
class-wc-order-item-product.php
8 years ago
class-wc-order-item-shipping.php
8 years ago
class-wc-order-item-tax.php
8 years ago
class-wc-order-item.php
8 years ago
class-wc-order-query.php
8 years ago
class-wc-order-refund.php
9 years ago
class-wc-order.php
8 years ago
class-wc-payment-gateways.php
8 years ago
class-wc-payment-tokens.php
9 years ago
class-wc-post-data.php
8 years ago
class-wc-post-types.php
8 years ago
class-wc-product-attribute.php
8 years ago
class-wc-product-download.php
8 years ago
class-wc-product-external.php
8 years ago
class-wc-product-factory.php
9 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
8 years ago
class-wc-product-simple.php
8 years ago
class-wc-product-variable.php
8 years ago
class-wc-product-variation.php
8 years ago
class-wc-query.php
8 years ago
class-wc-regenerate-images-request.php
8 years ago
class-wc-regenerate-images.php
8 years ago
class-wc-register-wp-admin-settings.php
9 years ago
class-wc-session-handler.php
8 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
8 years ago
class-wc-shipping-zones.php
8 years ago
class-wc-shipping.php
8 years ago
class-wc-shortcodes.php
8 years ago
class-wc-structured-data.php
8 years ago
class-wc-tax.php
8 years ago
class-wc-template-loader.php
8 years ago
class-wc-tracker.php
8 years ago
class-wc-validation.php
8 years ago
class-wc-webhook.php
8 years ago
class-woocommerce.php
8 years ago
wc-account-functions.php
8 years ago
wc-attribute-functions.php
8 years ago
wc-cart-functions.php
8 years ago
wc-conditional-functions.php
8 years ago
wc-core-functions.php
8 years ago
wc-coupon-functions.php
9 years ago
wc-deprecated-functions.php
8 years ago
wc-formatting-functions.php
8 years ago
wc-notice-functions.php
8 years ago
wc-order-functions.php
8 years ago
wc-order-item-functions.php
9 years ago
wc-page-functions.php
8 years ago
wc-product-functions.php
8 years ago
wc-rest-functions.php
8 years ago
wc-stock-functions.php
8 years ago
wc-template-functions.php
8 years ago
wc-template-hooks.php
8 years ago
wc-term-functions.php
8 years ago
wc-update-functions.php
8 years ago
wc-user-functions.php
8 years ago
wc-webhook-functions.php
8 years ago
wc-widget-functions.php
8 years ago
class-wc-logger.php
277 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 | |
| 48 | if ( ! empty( $handlers ) && is_array( $handlers ) ) { |
| 49 | foreach ( $handlers as $handler ) { |
| 50 | $implements = class_implements( $handler ); |
| 51 | if ( is_object( $handler ) && is_array( $implements ) && in_array( 'WC_Log_Handler_Interface', $implements ) ) { |
| 52 | $register_handlers[] = $handler; |
| 53 | } else { |
| 54 | wc_doing_it_wrong( |
| 55 | __METHOD__, |
| 56 | sprintf( |
| 57 | /* translators: 1: class name 2: WC_Log_Handler_Interface */ |
| 58 | __( 'The provided handler %1$s does not implement %2$s.', 'woocommerce' ), |
| 59 | '<code>' . esc_html( is_object( $handler ) ? get_class( $handler ) : $handler ) . '</code>', |
| 60 | '<code>WC_Log_Handler_Interface</code>' |
| 61 | ), |
| 62 | '3.0' |
| 63 | ); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if ( null !== $threshold ) { |
| 69 | $threshold = WC_Log_Levels::get_level_severity( $threshold ); |
| 70 | } elseif ( defined( 'WC_LOG_THRESHOLD' ) && WC_Log_Levels::is_valid_level( WC_LOG_THRESHOLD ) ) { |
| 71 | $threshold = WC_Log_Levels::get_level_severity( WC_LOG_THRESHOLD ); |
| 72 | } else { |
| 73 | $threshold = null; |
| 74 | } |
| 75 | |
| 76 | $this->handlers = $register_handlers; |
| 77 | $this->threshold = $threshold; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Determine whether to handle or ignore log. |
| 82 | * |
| 83 | * @param string $level emergency|alert|critical|error|warning|notice|info|debug |
| 84 | * @return bool True if the log should be handled. |
| 85 | */ |
| 86 | protected function should_handle( $level ) { |
| 87 | if ( null === $this->threshold ) { |
| 88 | return true; |
| 89 | } |
| 90 | return $this->threshold <= WC_Log_Levels::get_level_severity( $level ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Add a log entry. |
| 95 | * |
| 96 | * This is not the preferred method for adding log messages. Please use log() or any one of |
| 97 | * the level methods (debug(), info(), etc.). This method may be deprecated in the future. |
| 98 | * |
| 99 | * @param string $handle |
| 100 | * @param string $message |
| 101 | * @param string $level |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | public function add( $handle, $message, $level = WC_Log_Levels::NOTICE ) { |
| 106 | $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle ); |
| 107 | $this->log( $level, $message, array( 'source' => $handle, '_legacy' => true ) ); |
| 108 | wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '3.0', 'This action has been deprecated with no alternative.' ); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Add a log entry. |
| 114 | * |
| 115 | * @param string $level One of the following: |
| 116 | * 'emergency': System is unusable. |
| 117 | * 'alert': Action must be taken immediately. |
| 118 | * 'critical': Critical conditions. |
| 119 | * 'error': Error conditions. |
| 120 | * 'warning': Warning conditions. |
| 121 | * 'notice': Normal but significant condition. |
| 122 | * 'info': Informational messages. |
| 123 | * 'debug': Debug-level messages. |
| 124 | * @param string $message Log message. |
| 125 | * @param array $context Optional. Additional information for log handlers. |
| 126 | */ |
| 127 | public function log( $level, $message, $context = array() ) { |
| 128 | if ( ! WC_Log_Levels::is_valid_level( $level ) ) { |
| 129 | /* translators: 1: WC_Logger::log 2: level */ |
| 130 | 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' ); |
| 131 | } |
| 132 | |
| 133 | if ( $this->should_handle( $level ) ) { |
| 134 | $timestamp = current_time( 'timestamp' ); |
| 135 | $message = apply_filters( 'woocommerce_logger_log_message', $message, $level, $context ); |
| 136 | |
| 137 | foreach ( $this->handlers as $handler ) { |
| 138 | $handler->handle( $timestamp, $level, $message, $context ); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Adds an emergency level message. |
| 145 | * |
| 146 | * System is unusable. |
| 147 | * |
| 148 | * @see WC_Logger::log |
| 149 | * |
| 150 | * @param string $message |
| 151 | * @param array $context |
| 152 | */ |
| 153 | public function emergency( $message, $context = array() ) { |
| 154 | $this->log( WC_Log_Levels::EMERGENCY, $message, $context ); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Adds an alert level message. |
| 159 | * |
| 160 | * Action must be taken immediately. |
| 161 | * Example: Entire website down, database unavailable, etc. |
| 162 | * |
| 163 | * @see WC_Logger::log |
| 164 | * |
| 165 | * @param string $message |
| 166 | * @param array $context |
| 167 | */ |
| 168 | public function alert( $message, $context = array() ) { |
| 169 | $this->log( WC_Log_Levels::ALERT, $message, $context ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Adds a critical level message. |
| 174 | * |
| 175 | * Critical conditions. |
| 176 | * Example: Application component unavailable, unexpected exception. |
| 177 | * |
| 178 | * @see WC_Logger::log |
| 179 | * |
| 180 | * @param string $message |
| 181 | * @param array $context |
| 182 | */ |
| 183 | public function critical( $message, $context = array() ) { |
| 184 | $this->log( WC_Log_Levels::CRITICAL, $message, $context ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Adds an error level message. |
| 189 | * |
| 190 | * Runtime errors that do not require immediate action but should typically be logged |
| 191 | * and monitored. |
| 192 | * |
| 193 | * @see WC_Logger::log |
| 194 | * |
| 195 | * @param string $message |
| 196 | * @param array $context |
| 197 | */ |
| 198 | public function error( $message, $context = array() ) { |
| 199 | $this->log( WC_Log_Levels::ERROR, $message, $context ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Adds a warning level message. |
| 204 | * |
| 205 | * Exceptional occurrences that are not errors. |
| 206 | * |
| 207 | * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not |
| 208 | * necessarily wrong. |
| 209 | * |
| 210 | * @see WC_Logger::log |
| 211 | * |
| 212 | * @param string $message |
| 213 | * @param array $context |
| 214 | */ |
| 215 | public function warning( $message, $context = array() ) { |
| 216 | $this->log( WC_Log_Levels::WARNING, $message, $context ); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Adds a notice level message. |
| 221 | * |
| 222 | * Normal but significant events. |
| 223 | * |
| 224 | * @see WC_Logger::log |
| 225 | * |
| 226 | * @param string $message |
| 227 | * @param array $context |
| 228 | */ |
| 229 | public function notice( $message, $context = array() ) { |
| 230 | $this->log( WC_Log_Levels::NOTICE, $message, $context ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Adds a info level message. |
| 235 | * |
| 236 | * Interesting events. |
| 237 | * Example: User logs in, SQL logs. |
| 238 | * |
| 239 | * @see WC_Logger::log |
| 240 | * |
| 241 | * @param string $message |
| 242 | * @param array $context |
| 243 | */ |
| 244 | public function info( $message, $context = array() ) { |
| 245 | $this->log( WC_Log_Levels::INFO, $message, $context ); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Adds a debug level message. |
| 250 | * |
| 251 | * Detailed debug information. |
| 252 | * |
| 253 | * @see WC_Logger::log |
| 254 | * |
| 255 | * @param string $message |
| 256 | * @param array $context |
| 257 | */ |
| 258 | public function debug( $message, $context = array() ) { |
| 259 | $this->log( WC_Log_Levels::DEBUG, $message, $context ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Clear entries from chosen file. |
| 264 | * |
| 265 | * @deprecated 3.0.0 |
| 266 | * |
| 267 | * @param string $handle |
| 268 | * |
| 269 | * @return bool |
| 270 | */ |
| 271 | public function clear( $handle ) { |
| 272 | wc_deprecated_function( 'WC_Logger::clear', '3.0', 'WC_Log_Handler_File::clear' ); |
| 273 | $handler = new WC_Log_Handler_File(); |
| 274 | return $handler->clear( $handle ); |
| 275 | } |
| 276 | } |
| 277 |