Add logic for log level filtering to `WC_Log_Handler`.
This commit is contained in:
parent
9a0efcfe55
commit
0e0433195d
|
@ -3,6 +3,10 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
exit; // Exit if accessed directly
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'WC_Logger' ) ) {
|
||||
include_once( dirname( dirname( __FILE__ ) ) . '/class-wc-logger.php' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract WC Log Handler Class
|
||||
*
|
||||
|
@ -13,6 +17,47 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
*/
|
||||
abstract class WC_Log_Handler {
|
||||
|
||||
/**
|
||||
* Minimum log level this handler will process.
|
||||
*
|
||||
* @var int 0-8 minimum level severity for handling log entry.
|
||||
* @access private
|
||||
*/
|
||||
protected $_threshold;
|
||||
|
||||
/**
|
||||
* Log levels by severity.
|
||||
*
|
||||
* @var array
|
||||
* @access private
|
||||
*/
|
||||
protected static $_log_levels = array(
|
||||
WC_Logger::DEBUG => 0,
|
||||
WC_Logger::INFO => 1,
|
||||
WC_Logger::NOTICE => 2,
|
||||
WC_Logger::WARNING => 3,
|
||||
WC_Logger::ERROR => 4,
|
||||
WC_Logger::CRITICAL => 6,
|
||||
WC_Logger::ALERT => 7,
|
||||
WC_Logger::EMERGENCY => 8,
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor for log handler.
|
||||
*
|
||||
* @param arr $args {
|
||||
* @type string $threshold Optional. Sets the log severity threshold.
|
||||
* emergency|alert|critical|error|warning|notice|info|debug
|
||||
* }
|
||||
*/
|
||||
public function __construct( $args = array() ) {
|
||||
if ( isset( $args['threshold'] ) ) {
|
||||
$this->set_threshold( $args['threshold'] );
|
||||
} else {
|
||||
$this->set_threshold( WC_Logger::EMERGENCY );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a log entry.
|
||||
*
|
||||
|
@ -27,6 +72,41 @@ abstract class WC_Log_Handler {
|
|||
*/
|
||||
public abstract function handle( $level, $timestamp, $message, $context );
|
||||
|
||||
/**
|
||||
* Set handler severity threshold.
|
||||
*
|
||||
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
||||
*/
|
||||
public function set_threshold( $level ) {
|
||||
$level = apply_filters( 'woocommerce_log_handler_set_threshold', $level, __CLASS__ );
|
||||
$this->_threshold = $this->get_level_severity( $level );
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode level string into integer.
|
||||
*
|
||||
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
||||
* @return int 0 (debug) - 8 (emergency) or -1 if level is not valid
|
||||
*/
|
||||
public static function get_level_severity( $level ) {
|
||||
if ( array_key_exists( $level, self::$_log_levels ) ) {
|
||||
$severity = self::$_log_levels[ $level ];
|
||||
} else {
|
||||
$severity = -1;
|
||||
}
|
||||
return $severity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should this handler process this log?
|
||||
*
|
||||
* @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 <= $this->get_level_severity( $level );
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a timestamp for use in log messages.
|
||||
*
|
||||
|
|
|
@ -20,14 +20,14 @@ class WC_Logger {
|
|||
*
|
||||
* @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';
|
||||
const INFO = 'info';
|
||||
const NOTICE = 'notice';
|
||||
const WARNING = 'warning';
|
||||
const ERROR = 'error';
|
||||
const CRITICAL = 'critical';
|
||||
const ALERT = 'alert';
|
||||
const EMERGENCY = 'emergency';
|
||||
|
||||
/**
|
||||
* Stores registered log handlers.
|
||||
|
@ -37,6 +37,17 @@ class WC_Logger {
|
|||
*/
|
||||
private $_handlers;
|
||||
|
||||
private static $_valid_levels = array(
|
||||
self::DEBUG,
|
||||
self::INFO,
|
||||
self::NOTICE,
|
||||
self::WARNING,
|
||||
self::ERROR,
|
||||
self::CRITICAL,
|
||||
self::ALERT,
|
||||
self::EMERGENCY,
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor for the logger.
|
||||
*/
|
||||
|
@ -74,6 +85,9 @@ class WC_Logger {
|
|||
* }
|
||||
*/
|
||||
public function log( $level, $message, $context = array() ) {
|
||||
if ( ! in_array( $level, self::$_valid_levels ) ) {
|
||||
_doing_it_wrong( __FUNCTION__, sprintf( __( 'WC_Logger::log was called with an invalid level "%s"', 'woocommerce' ), $level ), '2.8' );
|
||||
}
|
||||
|
||||
$timestamp = current_time( 'timestamp' );
|
||||
|
||||
|
|
|
@ -28,8 +28,11 @@ class WC_Log_Handler_File extends WC_Log_Handler {
|
|||
|
||||
/**
|
||||
* Constructor for the logger.
|
||||
*
|
||||
* @param $args additional args. @see WC_Log_Handler::__construct
|
||||
*/
|
||||
public function __construct() {
|
||||
public function __construct( $args ) {
|
||||
parent::__construct( $args );
|
||||
$this->_handles = array();
|
||||
}
|
||||
|
||||
|
@ -60,6 +63,10 @@ class WC_Log_Handler_File extends WC_Log_Handler {
|
|||
*/
|
||||
public function handle( $level, $timestamp, $message, $context ) {
|
||||
|
||||
if ( ! $this->should_handle( $level ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( isset( $context['tag'] ) && $context['tag'] ) {
|
||||
$handle = $context['tag'];
|
||||
} else {
|
||||
|
@ -70,13 +77,12 @@ class WC_Log_Handler_File extends WC_Log_Handler {
|
|||
|
||||
// Bubble if add is NOT successful
|
||||
return ! $this->add( $entry, $handle );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a log entry text from level, timestamp and message.
|
||||
*
|
||||
* Attempt to ensure backwords compatibility for legacy WC_Logger::add calls
|
||||
* Attempt to ensure backwards compatibility for legacy WC_Logger::add calls
|
||||
*
|
||||
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
||||
* @param int $timestamp
|
||||
|
|
|
@ -1428,14 +1428,15 @@ function wc_get_logger() {
|
|||
*
|
||||
* @since 2.8
|
||||
* @param array $handlers
|
||||
* @param string $threshold Optional. Threshold for log handler. Defaults to 'info'.
|
||||
* @return array
|
||||
*/
|
||||
function wc_register_file_log_handler( $handlers ) {
|
||||
function wc_register_file_log_handler( $handlers, $threshold = 'info' ) {
|
||||
if ( ! class_exists( 'WC_Log_Handler_File' ) ) {
|
||||
include_once( dirname( __FILE__ ) . '/log-handlers/class-wc-log-handler-file.php' );
|
||||
}
|
||||
|
||||
array_push( $handlers, new WC_Log_Handler_File() );
|
||||
array_push( $handlers, new WC_Log_Handler_File( array( 'threshold' => $threshold ) ) );
|
||||
|
||||
return $handlers;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue