Use constants over settings to configure logger
Settings have been removed. WC_LOG_THRESHOLD and WC_LOG_HANDLER constants have been added.
This commit is contained in:
parent
e4681d268b
commit
4cf86ef346
|
@ -77,7 +77,7 @@ class WC_Admin_Status {
|
|||
* Show the logs page.
|
||||
*/
|
||||
public static function status_logs() {
|
||||
if ( 'db' === get_option( 'woocommerce_default_log_handler' ) ) {
|
||||
if ( defined( 'WC_LOG_HANDLER' ) && 'WC_Log_Handler_DB' === WC_LOG_HANDLER ) {
|
||||
self::status_logs_db();
|
||||
} else {
|
||||
self::status_logs_file();
|
||||
|
|
|
@ -133,38 +133,6 @@ class WC_Settings_General extends WC_Settings_Page {
|
|||
),
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => __( 'Default log handler', 'woocommerce' ),
|
||||
'id' => 'woocommerce_default_log_handler',
|
||||
'desc_tip' => __( 'This option defines the how log messages will be handled by default.', 'woocommerce' ),
|
||||
'default' => 'file',
|
||||
'type' => 'select',
|
||||
'class' => 'wc-enhanced-select',
|
||||
'options' => array(
|
||||
'file' => __( 'File handler', 'woocommerce' ),
|
||||
'db' => __( 'Database handler', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => __( 'Log level threshold', 'woocommerce' ),
|
||||
'id' => 'woocommerce_log_threshold',
|
||||
'desc_tip' => __( 'This option defines the minimum level that will be handled by the logger. Anything belew this level will be ignored. The recommended setting for live sites is "notice".', 'woocommerce' ),
|
||||
'default' => 'notice',
|
||||
'type' => 'select',
|
||||
'class' => 'wc-enhanced-select',
|
||||
'options' => array(
|
||||
WC_Log_Levels::EMERGENCY => __( 'Emergency: system is unusable', 'woocommerce' ),
|
||||
WC_Log_Levels::ALERT => __( 'Alert: action must be taken immediately', 'woocommerce' ),
|
||||
WC_Log_Levels::CRITICAL => __( 'Critical: critical conditions', 'woocommerce' ),
|
||||
WC_Log_Levels::ERROR => __( 'Error: error conditions', 'woocommerce' ),
|
||||
WC_Log_Levels::WARNING => __( 'Warning: warning conditions', 'woocommerce' ),
|
||||
WC_Log_Levels::NOTICE => __( 'Notice: normal but significant condition', 'woocommerce' ),
|
||||
WC_Log_Levels::INFO => __( 'Informational: informational messages', 'woocommerce' ),
|
||||
WC_Log_Levels::DEBUG => __( 'Debug: debug-level messages', 'woocommerce' ),
|
||||
),
|
||||
),
|
||||
|
||||
array(
|
||||
'title' => __( 'Enable taxes', 'woocommerce' ),
|
||||
'desc' => __( 'Enable taxes and tax calculations', 'woocommerce' ),
|
||||
|
|
|
@ -81,7 +81,7 @@ abstract class WC_Log_Levels {
|
|||
* @return bool True if $level is a valid level.
|
||||
*/
|
||||
public static function is_valid_level( $level ) {
|
||||
return array_key_exists( $level, self::$level_to_severity );
|
||||
return array_key_exists( strtolower( $level ), self::$level_to_severity );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,7 +92,7 @@ abstract class WC_Log_Levels {
|
|||
*/
|
||||
public static function get_level_severity( $level ) {
|
||||
if ( self::is_valid_level( $level ) ) {
|
||||
$severity = self::$level_to_severity[ $level ];
|
||||
$severity = self::$level_to_severity[ strtolower( $level ) ];
|
||||
} else {
|
||||
$severity = 0;
|
||||
}
|
||||
|
|
|
@ -35,20 +35,24 @@ class WC_Logger {
|
|||
* 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.
|
||||
* @param string $threshold Optional. Define an explicit threshold. Defaults to the global
|
||||
* setting 'woocommerce_log_threshold' or 'notice' if the setting is not configured.
|
||||
* @param string $threshold Optional. Define an explicit threshold. May be configured
|
||||
* via WC_LOG_THRESHOLD. By default, all logs will be processed.
|
||||
*/
|
||||
public function __construct( $handlers = null, $threshold = null ) {
|
||||
if ( null === $handlers ) {
|
||||
$handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
|
||||
}
|
||||
|
||||
if ( null === $threshold ) {
|
||||
$threshold = get_option( 'woocommerce_log_threshold', 'notice' );
|
||||
if ( null !== $threshold ) {
|
||||
$threshold = WC_Log_Levels::get_level_severity( $threshold );
|
||||
} elseif ( defined( 'WC_LOG_THRESHOLD' ) && WC_Log_Levels::is_valid_level( WC_LOG_THRESHOLD ) ) {
|
||||
$threshold = WC_Log_Levels::get_level_severity( WC_LOG_THRESHOLD );
|
||||
} else {
|
||||
$threshold = null;
|
||||
}
|
||||
|
||||
$this->handlers = $handlers;
|
||||
$this->threshold = WC_Log_Levels::get_level_severity( $threshold );
|
||||
$this->handlers = $handlers;
|
||||
$this->threshold = $threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,6 +62,9 @@ class WC_Logger {
|
|||
* @return bool True if the log should be handled.
|
||||
*/
|
||||
public function should_handle( $level ) {
|
||||
if ( null === $this->threshold ) {
|
||||
return true;
|
||||
}
|
||||
return $this->threshold <= WC_Log_Levels::get_level_severity( $level );
|
||||
}
|
||||
|
||||
|
|
|
@ -1459,15 +1459,19 @@ function wc_print_r( $expression, $return = false ) {
|
|||
* @return array
|
||||
*/
|
||||
function wc_register_default_log_handler( $handlers ) {
|
||||
if ( 'db' === get_option( 'woocommerce_default_log_handler' ) ) {
|
||||
array_push( $handlers, new WC_Log_Handler_DB() );
|
||||
|
||||
if ( defined( 'WC_LOG_HANDLER' ) && class_exists( WC_LOG_HANDLER ) ) {
|
||||
$handler_class = WC_LOG_HANDLER;
|
||||
$default_handler = new $handler_class();
|
||||
} else {
|
||||
array_push( $handlers, new WC_Log_Handler_File() );
|
||||
$default_handler = new WC_Log_Handler_File();
|
||||
}
|
||||
|
||||
array_push( $handlers, $default_handler );
|
||||
|
||||
return $handlers;
|
||||
}
|
||||
add_filter( 'woocommerce_register_log_handlers', 'wc_register_default_log_handler', 0 );
|
||||
add_filter( 'woocommerce_register_log_handlers', 'wc_register_default_log_handler' );
|
||||
|
||||
/**
|
||||
* Store user agents. Used for tracker.
|
||||
|
|
Loading…
Reference in New Issue