WC_Logger registers and delegates logging to handlers

This commit is contained in:
Jon Surrell 2016-11-12 19:38:25 +01:00
parent e922efb0f7
commit d9dcabf8f2
1 changed files with 117 additions and 104 deletions

View File

@ -5,10 +5,10 @@ if ( ! defined( 'ABSPATH' ) ) {
} }
/** /**
* Allows log files to be written to for debugging purposes * Provides logging capabilities for debugging purposes.
* *
* @class WC_Logger * @class WC_Logger
* @version 1.6.4 * @version 2.0.0
* @package WooCommerce/Classes * @package WooCommerce/Classes
* @category Class * @category Class
* @author WooThemes * @author WooThemes
@ -16,78 +16,39 @@ if ( ! defined( 'ABSPATH' ) ) {
class WC_Logger { class WC_Logger {
/** /**
* Stores open file _handles. * Log Levels
*
* @see @link {https://tools.ietf.org/html/rfc5424}
*/
const EMERGENCY = 'emergency';
const ALERT = 'alert';
const CRITICAL = 'critical';
const ERROR = 'error';
const WARNING = 'warning';
const NOTICE = 'notice';
const INFO = 'info';
const DEBUG = 'debug';
/**
* Stores registered log handlers.
* *
* @var array * @var array
* @access private * @access private
*/ */
private $_handles; private $_handlers;
/** /**
* Constructor for the logger. * Constructor for the logger.
*/ */
public function __construct() { public function __construct() {
$this->_handles = array(); $handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
$this->_handlers = $handlers;
} }
/** /**
* Destructor. * Add a log entry.
*/
public function __destruct() {
foreach ( $this->_handles as $handle ) {
if ( is_resource( $handle ) ) {
fclose( $handle );
}
}
}
/**
* Open log file for writing.
* *
* @param string $handle * @deprecated since 2.0.0
* @param string $mode
* @return bool success
*/
protected function open( $handle, $mode = 'a' ) {
if ( isset( $this->_handles[ $handle ] ) ) {
return true;
}
if ( ! file_exists( wc_get_log_file_path( $handle ) ) ) {
$temphandle = @fopen( wc_get_log_file_path( $handle ), 'w+' );
@fclose( $temphandle );
if ( defined( 'FS_CHMOD_FILE' ) ) {
@chmod( wc_get_log_file_path( $handle ), FS_CHMOD_FILE );
}
}
if ( $this->_handles[ $handle ] = @fopen( wc_get_log_file_path( $handle ), $mode ) ) {
return true;
}
return false;
}
/**
* Close a handle.
*
* @param string $handle
* @return bool success
*/
protected function close( $handle ) {
$result = false;
if ( is_resource( $this->_handles[ $handle ] ) ) {
$result = fclose( $this->_handles[ $handle ] );
unset( $this->_handles[ $handle ] );
}
return $result;
}
/**
* Add a log entry to chosen file.
* *
* @param string $handle * @param string $handle
* @param string $message * @param string $message
@ -95,67 +56,119 @@ class WC_Logger {
* @return bool * @return bool
*/ */
public function add( $handle, $message ) { public function add( $handle, $message ) {
$result = false; _deprecated_function( 'WC_Logger::add', '2.8', 'WC_Logger::log' );
$this->log( self::INFO, $message, array( 'tag' => $handle ) );
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) { wc_do_deprecated_action( 'woocommerce_log_add', $handle, $message, '2.8');
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle ); return true;
$time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
$result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
}
do_action( 'woocommerce_log_add', $handle, $message );
return false !== $result;
} }
/** /**
* Clear entries from chosen file. * Add a log entry.
* *
* @param string $handle * @param string $level emergency|alert|critical|error|warning|notice|info|debug
* @param string $message
* @param array $context {
* Optional. Additional information for log handlers.
* *
* @return bool * @type string $tag Optional. May be used by log handlers to sort messages.
* }
*/ */
public function clear( $handle ) { public function log( $level, $message, $context=array() ) {
$result = false;
// Close the file if it's already open. foreach ( $this->_handlers as $handler ) {
$this->close( $handle ); $continue = $handler->handle( $level, $message, $context );
/** if ( false === $continue ) {
* $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at the beginning of the file, break;
* and truncate the file to zero length. }
*/
if ( $this->open( $handle, 'w' ) && is_resource( $this->_handles[ $handle ] ) ) {
$result = true;
} }
do_action( 'woocommerce_log_clear', $handle );
return $result;
} }
/** /**
* Remove/delete the chosen file. * Adds an emergency level message.
* *
* @param string $handle * @see WC_Logger::log
* *
* @return bool
*/ */
public function remove( $handle ) { public function emergency( $message, $context=array() ) {
$removed = false; $this->log( self::EMERGENCY, $message, $context );
$file = wc_get_log_file_path( $handle ); }
if ( is_file( $file ) && is_writable( $file ) ) { /**
// Close first to be certain no processes keep it alive after it is unlinked. * Adds an alert level message.
$this->close( $handle ); *
$removed = unlink( $file ); * @see WC_Logger::log
} elseif ( is_file( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) && is_writable( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ) ) { *
$this->close( $handle ); */
$removed = unlink( trailingslashit( WC_LOG_DIR ) . $handle . '.log' ); public function alert( $message, $context=array() ) {
} $this->log( self::ALERT, $message, $context );
}
do_action( 'woocommerce_log_remove', $handle, $removed ); /**
* Adds a critical level message.
*
* @see WC_Logger::log
*
*/
public function critical( $message, $context=array() ) {
$this->log( self::CRITICAL, $message, $context );
}
return $removed; /**
* Adds an error level message.
*
* @see WC_Logger::log
*
*/
public function error( $message, $context=array() ) {
$this->log( self::ERROR, $message, $context );
}
/**
* Adds a warning level message.
*
* @see WC_Logger::log
*
*/
public function warning( $message, $context=array() ) {
$this->log( self::WARNING, $message, $context );
}
/**
* Adds a notice level message.
*
* @see WC_Logger::log
*
*/
public function notice( $message, $context=array() ) {
$this->log( self::NOTICE, $message, $context );
}
/**
* Adds a info level message.
*
* @see WC_Logger::log
*
*/
public function info( $message, $context=array() ) {
$this->log( self::INFO, $message, $context );
}
/**
* Adds a debug level message.
*
* @see WC_Logger::log
*
*/
public function debug( $message, $context=array() ) {
$this->log( self::DEBUG, $message, $context );
}
/**
* @deprecated since 2.0.0
*/
public function clear() {
_deprecated_function( 'WC_Logger::clear', '2.8' );
} }
} }