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
|
|
|
* @access private
|
|
|
|
*/
|
2016-11-16 19:19:20 +00:00
|
|
|
private $handlers;
|
2012-08-14 19:42:38 +00:00
|
|
|
|
|
|
|
/**
|
2012-08-15 17:08:42 +00:00
|
|
|
* Constructor for the logger.
|
2012-08-14 19:42:38 +00:00
|
|
|
*/
|
2012-12-14 21:44:43 +00:00
|
|
|
public function __construct() {
|
2016-11-12 18:38:25 +00:00
|
|
|
$handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
|
2016-11-16 19:19:20 +00:00
|
|
|
$this->handlers = $handlers;
|
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.
|
|
|
|
*
|
|
|
|
* @deprecated since 2.0.0
|
|
|
|
*
|
|
|
|
* @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 ) {
|
|
|
|
_deprecated_function( 'WC_Logger::add', '2.8', 'WC_Logger::log' );
|
2016-11-13 22:56:11 +00:00
|
|
|
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
|
2016-12-10 20:59:41 +00:00
|
|
|
$this->log( WC_Log_Levels::INFO, $message, array( 'tag' => $handle, '_legacy' => true ) );
|
2016-11-13 22:56:11 +00:00
|
|
|
wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '2.8', '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-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-12 18:38:25 +00:00
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
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__;
|
|
|
|
_doing_it_wrong( "{$class}::{$method}", sprintf( __( 'WC_Logger::log was called with an invalid level "%s".', 'woocommerce' ), $level ), '2.8' );
|
2016-11-13 12:46:06 +00:00
|
|
|
}
|
2016-11-12 19:46:58 +00:00
|
|
|
|
|
|
|
$timestamp = current_time( 'timestamp' );
|
2012-08-14 19:42:38 +00:00
|
|
|
|
2016-11-16 19:19:20 +00:00
|
|
|
foreach ( $this->handlers as $handler ) {
|
2016-11-20 13:32:46 +00:00
|
|
|
$continue = $handler->handle( $timestamp, $level, $message, $context );
|
2016-09-13 21:38:16 +00:00
|
|
|
|
2016-11-12 18:38:25 +00:00
|
|
|
if ( false === $continue ) {
|
|
|
|
break;
|
2016-09-13 21:38:16 +00:00
|
|
|
}
|
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.
|
|
|
|
*
|
|
|
|
* @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-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.
|
|
|
|
*
|
|
|
|
* @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-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.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @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-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.
|
|
|
|
*
|
|
|
|
* @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-11-12 18:38:25 +00:00
|
|
|
* @deprecated since 2.0.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-11-12 18:38:25 +00:00
|
|
|
_deprecated_function( 'WC_Logger::clear', '2.8' );
|
2016-11-13 22:56:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove/delete the chosen file.
|
|
|
|
*
|
|
|
|
* @deprecated since 2.0.0
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-14 20:55:21 +00:00
|
|
|
public function remove() {
|
2016-11-13 22:56:11 +00:00
|
|
|
_deprecated_function( 'WC_Logger::remove', '2.8' );
|
|
|
|
return false;
|
2016-07-03 06:16:11 +00:00
|
|
|
}
|
2016-08-02 13:42:27 +00:00
|
|
|
}
|