2011-11-15 13:08:56 +00:00
|
|
|
<?php
|
2015-11-06 09:22:19 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2016-11-12 18:38:25 +00:00
|
|
|
* Provides logging capabilities for debugging purposes.
|
2011-11-15 13:08:56 +00:00
|
|
|
*
|
2016-04-13 06:46:30 +00:00
|
|
|
* @class WC_Logger
|
2016-11-12 18:38:25 +00:00
|
|
|
* @version 2.0.0
|
2016-04-13 06:46:30 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @category Class
|
|
|
|
* @author WooThemes
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
2012-01-27 16:38:39 +00:00
|
|
|
class WC_Logger {
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-11-12 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* Stores registered log handlers.
|
2016-01-06 15:24:47 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2012-08-15 17:08:42 +00:00
|
|
|
*/
|
2016-12-18 20:27:47 +00:00
|
|
|
protected $handlers;
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-12-11 12:27:49 +00:00
|
|
|
/**
|
|
|
|
* Minimum log level this handler will process.
|
|
|
|
*
|
|
|
|
* @var int Integer representation of minimum log level to handle.
|
|
|
|
*/
|
|
|
|
protected $threshold;
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2012-08-15 17:08:42 +00:00
|
|
|
* Constructor for the logger.
|
2016-12-11 12:27:49 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2016-12-11 16:24:30 +00:00
|
|
|
* @param string $threshold Optional. Define an explicit threshold. Defaults to the global
|
|
|
|
* setting 'woocommerce_log_threshold' or 'notice' if the setting is not configured.
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-12-11 12:27:49 +00:00
|
|
|
public function __construct( $handlers = null, $threshold = null ) {
|
|
|
|
if ( null === $handlers ) {
|
|
|
|
$handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
|
|
|
|
}
|
|
|
|
|
2016-12-11 16:24:30 +00:00
|
|
|
if ( null === $threshold ) {
|
2016-12-11 12:27:49 +00:00
|
|
|
$threshold = get_option( 'woocommerce_log_threshold', 'notice' );
|
|
|
|
}
|
|
|
|
|
2016-11-16 19:19:20 +00:00
|
|
|
$this->handlers = $handlers;
|
2016-12-11 12:27:49 +00:00
|
|
|
$this->threshold = WC_Log_Levels::get_level_severity( $threshold );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-18 20:27:47 +00:00
|
|
|
* Determine whether to handle or ignore log.
|
2016-12-11 12:27:49 +00:00
|
|
|
*
|
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
|
|
|
* @return bool True if the log should be handled.
|
|
|
|
*/
|
|
|
|
public function should_handle( $level ) {
|
|
|
|
return $this->threshold <= WC_Log_Levels::get_level_severity( $level );
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 19:42:38 +00:00
|
|
|
/**
|
2016-11-12 18:38:25 +00:00
|
|
|
* Add a log entry.
|
|
|
|
*
|
2016-12-19 20:28:55 +00:00
|
|
|
* @deprecated 2.7.0
|
2016-11-12 18:38:25 +00:00
|
|
|
*
|
|
|
|
* @param string $handle
|
|
|
|
* @param string $message
|
|
|
|
* @return bool
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2016-11-12 18:38:25 +00:00
|
|
|
public function add( $handle, $message ) {
|
2016-12-19 20:28:55 +00:00
|
|
|
wc_deprecated_function( 'WC_Logger::add', '2.7', 'WC_Logger::log' );
|
2016-11-13 22:56:11 +00:00
|
|
|
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
|
2016-12-18 20:22:58 +00:00
|
|
|
$this->log( WC_Log_Levels::NOTICE, $message, array( 'tag' => $handle, '_legacy' => true ) );
|
2016-12-19 20:28:55 +00:00
|
|
|
wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '2.7', 'This action has been deprecated with no alternative.' );
|
2016-11-12 18:38:25 +00:00
|
|
|
return true;
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2016-11-12 18:38:25 +00:00
|
|
|
* Add a log entry.
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* @param string $level One of the following:
|
|
|
|
* 'emergency': System is unusable.
|
|
|
|
* 'alert': Action must be taken immediately.
|
|
|
|
* 'critical': Critical conditions.
|
|
|
|
* 'error': Error conditions.
|
|
|
|
* 'warning': Warning conditions.
|
|
|
|
* 'notice': Normal but significant condition.
|
|
|
|
* 'informational': Informational messages.
|
|
|
|
* 'debug': Debug-level messages.
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param string $message Log message.
|
|
|
|
* @param array $context Optional. Additional information for log handlers.
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function log( $level, $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
if ( ! WC_Log_Levels::is_valid_level( $level ) ) {
|
2016-11-13 17:45:45 +00:00
|
|
|
$class = __CLASS__;
|
|
|
|
$method = __FUNCTION__;
|
2016-12-19 20:28:55 +00:00
|
|
|
wc_doing_it_wrong( "{$class}::{$method}", sprintf( __( 'WC_Logger::log was called with an invalid level "%s".', 'woocommerce' ), $level ), '2.7' );
|
2016-11-13 12:46:06 +00:00
|
|
|
}
|
2016-11-12 19:46:58 +00:00
|
|
|
|
2016-12-11 12:27:49 +00:00
|
|
|
if ( $this->should_handle( $level ) ) {
|
|
|
|
$timestamp = current_time( 'timestamp' );
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-12-11 12:27:49 +00:00
|
|
|
foreach ( $this->handlers as $handler ) {
|
|
|
|
$handler->handle( $timestamp, $level, $message, $context );
|
|
|
|
}
|
2016-09-12 22:37:02 +00:00
|
|
|
}
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-04-19 15:56:41 +00:00
|
|
|
/**
|
2016-11-12 18:38:25 +00:00
|
|
|
* Adds an emergency level message.
|
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* System is unusable.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
2016-04-19 15:56:41 +00:00
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function emergency( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::EMERGENCY, $message, $context );
|
2016-04-19 15:56:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2016-11-12 18:38:25 +00:00
|
|
|
* Adds an alert level message.
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* Action must be taken immediately.
|
|
|
|
* Example: Entire website down, database unavailable, etc.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function alert( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::ALERT, $message, $context );
|
2016-11-12 18:38:25 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-11-12 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* Adds a critical level message.
|
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* Critical conditions.
|
|
|
|
* Example: Application component unavailable, unexpected exception.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function critical( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::CRITICAL, $message, $context );
|
2016-04-13 06:46:30 +00:00
|
|
|
}
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2011-11-15 13:08:56 +00:00
|
|
|
/**
|
2016-11-12 18:38:25 +00:00
|
|
|
* Adds an error level message.
|
2012-08-14 19:42:38 +00:00
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* Runtime errors that do not require immediate action but should typically be logged
|
|
|
|
* and monitored.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
2011-11-15 13:08:56 +00:00
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function error( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::ERROR, $message, $context );
|
2016-11-12 18:38:25 +00:00
|
|
|
}
|
2015-07-16 19:55:48 +00:00
|
|
|
|
2016-11-12 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* Adds a warning level message.
|
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* Exceptional occurrences that are not errors.
|
|
|
|
*
|
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things that are not
|
|
|
|
* necessarily wrong.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function warning( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::WARNING, $message, $context );
|
2016-11-12 18:38:25 +00:00
|
|
|
}
|
2016-04-13 06:46:30 +00:00
|
|
|
|
2016-11-12 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* Adds a notice level message.
|
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* Normal but significant events.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function notice( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::NOTICE, $message, $context );
|
2011-11-15 13:08:56 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-07-03 06:16:11 +00:00
|
|
|
/**
|
2016-11-12 18:38:25 +00:00
|
|
|
* Adds a info level message.
|
2016-07-03 06:16:11 +00:00
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* Interesting events.
|
|
|
|
* Example: User logs in, SQL logs.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
2016-07-03 06:16:11 +00:00
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function info( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::INFO, $message, $context );
|
2016-11-12 18:38:25 +00:00
|
|
|
}
|
2016-07-03 06:16:11 +00:00
|
|
|
|
2016-11-12 18:38:25 +00:00
|
|
|
/**
|
|
|
|
* Adds a debug level message.
|
|
|
|
*
|
2016-12-10 21:31:32 +00:00
|
|
|
* Detailed debug information.
|
|
|
|
*
|
2016-11-12 18:38:25 +00:00
|
|
|
* @see WC_Logger::log
|
|
|
|
*/
|
2016-11-12 19:46:58 +00:00
|
|
|
public function debug( $message, $context = array() ) {
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::DEBUG, $message, $context );
|
2016-11-12 18:38:25 +00:00
|
|
|
}
|
2016-07-03 06:16:11 +00:00
|
|
|
|
2016-11-12 18:38:25 +00:00
|
|
|
/**
|
2016-11-13 22:56:11 +00:00
|
|
|
* Clear entries from chosen file.
|
|
|
|
*
|
2016-12-19 20:28:55 +00:00
|
|
|
* @deprecated 2.7.0
|
2016-11-13 22:56:11 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
2016-11-12 18:38:25 +00:00
|
|
|
*/
|
2016-11-14 20:55:21 +00:00
|
|
|
public function clear() {
|
2016-12-19 20:28:55 +00:00
|
|
|
wc_deprecated_function( 'WC_Logger::clear', '2.7', 'WC_Log_Handler_File::clear' );
|
2016-11-13 22:56:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove/delete the chosen file.
|
|
|
|
*
|
2016-12-19 20:28:55 +00:00
|
|
|
* @deprecated 2.7.0
|
2016-11-13 22:56:11 +00:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-14 20:55:21 +00:00
|
|
|
public function remove() {
|
2016-12-19 20:28:55 +00:00
|
|
|
wc_deprecated_function( 'WC_Logger::remove', '2.7', 'WC_Log_Handler_File::remove' );
|
2016-11-13 22:56:11 +00:00
|
|
|
return false;
|
2016-07-03 06:16:11 +00:00
|
|
|
}
|
2016-08-02 13:42:27 +00:00
|
|
|
}
|