| 1 |
<?php |
| 2 |
/** |
| 3 |
* Provides logging capabilities for debugging purposes. |
| 4 |
* |
| 5 |
* @class Cartflows_WC_Logger |
| 6 |
* @version 2.0.0 |
| 7 |
* @package WooCommerce/Classes |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Cartflows_WC_Logger class. |
| 14 |
*/ |
| 15 |
class Cartflows_WC_Logger implements Cartflows_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, 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. |
| 35 |
* @param string $threshold Optional. Define an explicit threshold. May be configured via WC_LOG_THRESHOLD. By default, all logs will be processed. |
| 36 |
*/ |
| 37 |
public function __construct( $handlers = null, $threshold = null ) { |
| 38 |
if ( null === $handlers ) { |
| 39 |
$handlers = apply_filters( 'cartflows_register_log_handlers', array() ); |
| 40 |
|
| 41 |
$default_handler = new Cartflows_Log_Handler_File(); |
| 42 |
|
| 43 |
array_push( $handlers, $default_handler ); |
| 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( 'Cartflows_Log_Handler_Interface', $implements, true ) ) { |
| 52 |
$register_handlers[] = $handler; |
| 53 |
} else { |
| 54 |
wc_doing_it_wrong( |
| 55 |
__METHOD__, |
| 56 |
sprintf( |
| 57 |
/* translators: 1: class name 2: Cartflows_Log_Handler_Interface */ |
| 58 |
__( 'The provided handler %1$s does not implement %2$s.', 'cartflows' ), |
| 59 |
'<code>' . esc_html( is_object( $handler ) ? get_class( $handler ) : $handler ) . '</code>', |
| 60 |
'<code>Cartflows_Log_Handler_Interface</code>' |
| 61 |
), |
| 62 |
'3.0' |
| 63 |
); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
if ( null !== $threshold ) { |
| 69 |
$threshold = Cartflows_Log_Levels::get_level_severity( $threshold ); |
| 70 |
} elseif ( defined( 'WC_LOG_THRESHOLD' ) && Cartflows_Log_Levels::is_valid_level( WC_LOG_THRESHOLD ) ) { |
| 71 |
$threshold = Cartflows_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 <= Cartflows_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 File handle. |
| 100 |
* @param string $message Message to log. |
| 101 |
* @param string $level Logging level. |
| 102 |
* @return bool |
| 103 |
*/ |
| 104 |
public function add( $handle, $message, $level = Cartflows_Log_Levels::NOTICE ) { |
| 105 |
$message = apply_filters( 'cartflows_logger_add_message', $message, $handle ); |
| 106 |
$this->log( |
| 107 |
$level, |
| 108 |
$message, |
| 109 |
array( |
| 110 |
'source' => $handle, |
| 111 |
'_legacy' => true, |
| 112 |
) |
| 113 |
); |
| 114 |
wc_do_deprecated_action( 'cartflows_log_add', array( $handle, $message ), '3.0', 'This action has been deprecated with no alternative.' ); |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Add a log entry. |
| 120 |
* |
| 121 |
* @param string $level One of the following: |
| 122 |
* 'emergency': System is unusable. |
| 123 |
* 'alert': Action must be taken immediately. |
| 124 |
* 'critical': Critical conditions. |
| 125 |
* 'error': Error conditions. |
| 126 |
* 'warning': Warning conditions. |
| 127 |
* 'notice': Normal but significant condition. |
| 128 |
* 'info': Informational messages. |
| 129 |
* 'debug': Debug-level messages. |
| 130 |
* @param string $message Log message. |
| 131 |
* @param array $context Optional. Additional information for log handlers. |
| 132 |
*/ |
| 133 |
public function log( $level, $message, $context = array() ) { |
| 134 |
if ( ! Cartflows_Log_Levels::is_valid_level( $level ) ) { |
| 135 |
/* translators: 1: Cartflows_WC_Logger::log 2: level */ |
| 136 |
wc_doing_it_wrong( __METHOD__, sprintf( __( '%1$s was called with an invalid level "%2$s".', 'cartflows' ), '<code>Cartflows_WC_Logger::log</code>', $level ), '3.0' ); |
| 137 |
} |
| 138 |
|
| 139 |
if ( $this->should_handle( $level ) ) { |
| 140 |
$timestamp = time(); |
| 141 |
$message = apply_filters( 'cartflows_logger_log_message', $message, $level, $context ); |
| 142 |
|
| 143 |
foreach ( $this->handlers as $handler ) { |
| 144 |
$handler->handle( $timestamp, $level, $message, $context ); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Adds an emergency level message. |
| 151 |
* |
| 152 |
* System is unusable. |
| 153 |
* |
| 154 |
* @see Cartflows_WC_Logger::log |
| 155 |
* |
| 156 |
* @param string $message Message to log. |
| 157 |
* @param array $context Log context. |
| 158 |
*/ |
| 159 |
public function emergency( $message, $context = array() ) { |
| 160 |
$this->log( Cartflows_Log_Levels::EMERGENCY, $message, $context ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Adds an alert level message. |
| 165 |
* |
| 166 |
* Action must be taken immediately. |
| 167 |
* Example: Entire website down, database unavailable, etc. |
| 168 |
* |
| 169 |
* @see Cartflows_WC_Logger::log |
| 170 |
* |
| 171 |
* @param string $message Message to log. |
| 172 |
* @param array $context Log context. |
| 173 |
*/ |
| 174 |
public function alert( $message, $context = array() ) { |
| 175 |
$this->log( Cartflows_Log_Levels::ALERT, $message, $context ); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Adds a critical level message. |
| 180 |
* |
| 181 |
* Critical conditions. |
| 182 |
* Example: Application component unavailable, unexpected exception. |
| 183 |
* |
| 184 |
* @see Cartflows_WC_Logger::log |
| 185 |
* |
| 186 |
* @param string $message Message to log. |
| 187 |
* @param array $context Log context. |
| 188 |
*/ |
| 189 |
public function critical( $message, $context = array() ) { |
| 190 |
$this->log( Cartflows_Log_Levels::CRITICAL, $message, $context ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Adds an error level message. |
| 195 |
* |
| 196 |
* Runtime errors that do not require immediate action but should typically be logged |
| 197 |
* and monitored. |
| 198 |
* |
| 199 |
* @see Cartflows_WC_Logger::log |
| 200 |
* |
| 201 |
* @param string $message Message to log. |
| 202 |
* @param array $context Log context. |
| 203 |
*/ |
| 204 |
public function error( $message, $context = array() ) { |
| 205 |
$this->log( Cartflows_Log_Levels::ERROR, $message, $context ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Adds a warning level message. |
| 210 |
* |
| 211 |
* Exceptional occurrences that are not errors. |
| 212 |
* |
| 213 |
* Example: Use of deprecated APIs, poor use of an API, undesirable things that are not |
| 214 |
* necessarily wrong. |
| 215 |
* |
| 216 |
* @see Cartflows_WC_Logger::log |
| 217 |
* |
| 218 |
* @param string $message Message to log. |
| 219 |
* @param array $context Log context. |
| 220 |
*/ |
| 221 |
public function warning( $message, $context = array() ) { |
| 222 |
$this->log( Cartflows_Log_Levels::WARNING, $message, $context ); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* Adds a notice level message. |
| 227 |
* |
| 228 |
* Normal but significant events. |
| 229 |
* |
| 230 |
* @see Cartflows_WC_Logger::log |
| 231 |
* |
| 232 |
* @param string $message Message to log. |
| 233 |
* @param array $context Log context. |
| 234 |
*/ |
| 235 |
public function notice( $message, $context = array() ) { |
| 236 |
$this->log( Cartflows_Log_Levels::NOTICE, $message, $context ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Adds a info level message. |
| 241 |
* |
| 242 |
* Interesting events. |
| 243 |
* Example: User logs in, SQL logs. |
| 244 |
* |
| 245 |
* @see Cartflows_WC_Logger::log |
| 246 |
* |
| 247 |
* @param string $message Message to log. |
| 248 |
* @param array $context Log context. |
| 249 |
*/ |
| 250 |
public function info( $message, $context = array() ) { |
| 251 |
$this->log( Cartflows_Log_Levels::INFO, $message, $context ); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Adds a debug level message. |
| 256 |
* |
| 257 |
* Detailed debug information. |
| 258 |
* |
| 259 |
* @see Cartflows_WC_Logger::log |
| 260 |
* |
| 261 |
* @param string $message Message to log. |
| 262 |
* @param array $context Log context. |
| 263 |
*/ |
| 264 |
public function debug( $message, $context = array() ) { |
| 265 |
$this->log( Cartflows_Log_Levels::DEBUG, $message, $context ); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Clear entries for a chosen file/source. |
| 270 |
* |
| 271 |
* @param string $source Source/handle to clear. |
| 272 |
* @return bool |
| 273 |
*/ |
| 274 |
public function clear( $source = '' ) { |
| 275 |
if ( ! $source ) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
foreach ( $this->handlers as $handler ) { |
| 279 |
if ( is_callable( array( $handler, 'clear' ) ) ) { |
| 280 |
$handler->clear( $source ); |
| 281 |
} |
| 282 |
} |
| 283 |
return true; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Clear all logs older than a defined number of days. Defaults to 30 days. |
| 288 |
* |
| 289 |
* @since 3.4.0 |
| 290 |
*/ |
| 291 |
public function clear_expired_logs() { |
| 292 |
$days = absint( apply_filters( 'cartflows_logger_days_to_retain_logs', 30 ) ); |
| 293 |
$timestamp = strtotime( "-{$days} days" ); |
| 294 |
|
| 295 |
foreach ( $this->handlers as $handler ) { |
| 296 |
if ( is_callable( array( $handler, 'delete_logs_before_timestamp' ) ) ) { |
| 297 |
$handler->delete_logs_before_timestamp( $timestamp ); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|