2016-11-12 19:46:58 +00:00
|
|
|
<?php
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:46:06 +00:00
|
|
|
if ( ! class_exists( 'WC_Logger' ) ) {
|
|
|
|
include_once( dirname( dirname( __FILE__ ) ) . '/class-wc-logger.php' );
|
|
|
|
}
|
|
|
|
|
2016-11-12 19:46:58 +00:00
|
|
|
/**
|
|
|
|
* Abstract WC Log Handler Class
|
|
|
|
*
|
|
|
|
* @version 2.8.0
|
|
|
|
* @package WooCommerce/Abstracts
|
|
|
|
* @category Abstract Class
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
abstract class WC_Log_Handler {
|
|
|
|
|
2016-11-13 12:46:06 +00:00
|
|
|
/**
|
|
|
|
* Minimum log level this handler will process.
|
|
|
|
*
|
|
|
|
* @var int 0-8 minimum level severity for handling log entry.
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-11-16 19:19:20 +00:00
|
|
|
protected $threshold;
|
2016-11-13 12:46:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log levels by severity.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access private
|
|
|
|
*/
|
2016-11-16 19:19:20 +00:00
|
|
|
protected static $log_levels = array(
|
2016-11-13 12:46:06 +00:00
|
|
|
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.
|
|
|
|
*
|
2016-11-14 20:40:54 +00:00
|
|
|
* @param array $args {
|
2016-11-13 21:14:11 +00:00
|
|
|
* @type string $threshold Optional. Default 'emergency'. Sets the log severity threshold.
|
2016-11-13 12:46:06 +00:00
|
|
|
* emergency|alert|critical|error|warning|notice|info|debug
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
public function __construct( $args = array() ) {
|
2016-11-13 21:14:11 +00:00
|
|
|
|
|
|
|
$args = wp_parse_args( $args, array(
|
2016-11-14 20:56:00 +00:00
|
|
|
'threshold' => WC_Logger::DEBUG,
|
2016-11-13 21:14:11 +00:00
|
|
|
) );
|
|
|
|
|
|
|
|
$this->set_threshold( $args['threshold'] );
|
2016-11-13 12:46:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 19:46:58 +00:00
|
|
|
/**
|
|
|
|
* Handle a log entry.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-12 19:46:58 +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 Additional information for log handlers.
|
2016-11-12 19:46:58 +00:00
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @return bool True if log entry should bubble to further loggers.
|
2016-11-12 19:46:58 +00:00
|
|
|
*/
|
2016-11-20 13:32:46 +00:00
|
|
|
abstract public function handle( $timestamp, $level, $message, $context );
|
2016-11-12 19:46:58 +00:00
|
|
|
|
2016-11-13 12:46:06 +00:00
|
|
|
/**
|
|
|
|
* 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__ );
|
2016-11-16 19:19:20 +00:00
|
|
|
$this->threshold = $this->get_level_severity( $level );
|
2016-11-13 12:46:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ) {
|
2016-11-16 19:19:20 +00:00
|
|
|
if ( array_key_exists( $level, self::$log_levels ) ) {
|
|
|
|
$severity = self::$log_levels[ $level ];
|
2016-11-13 12:46:06 +00:00
|
|
|
} else {
|
|
|
|
$severity = -1;
|
|
|
|
}
|
|
|
|
return $severity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-20 13:32:46 +00:00
|
|
|
* Determine whether handler should handle log.
|
2016-11-13 12:46:06 +00:00
|
|
|
*
|
|
|
|
* @param string $level emergency|alert|critical|error|warning|notice|info|debug
|
2016-11-20 13:32:46 +00:00
|
|
|
* @return bool True if the log should be handled.
|
2016-11-13 12:46:06 +00:00
|
|
|
*/
|
|
|
|
public function should_handle( $level ) {
|
2016-11-16 19:19:20 +00:00
|
|
|
return $this->threshold <= $this->get_level_severity( $level );
|
2016-11-13 12:46:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 19:46:58 +00:00
|
|
|
/**
|
|
|
|
* Formats a timestamp for use in log messages.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
|
|
|
* @return string Formatted time for use in log entry.
|
2016-11-12 19:46:58 +00:00
|
|
|
*/
|
|
|
|
public static function format_time( $timestamp ) {
|
|
|
|
return date( 'c', $timestamp );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a log entry text from level, timestamp and message.
|
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @param int $timestamp Log timestamp.
|
2016-11-12 19:46:58 +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 Additional information for log handlers.
|
2016-11-12 19:46:58 +00:00
|
|
|
*
|
2016-11-20 13:32:46 +00:00
|
|
|
* @return string Formatted log entry.
|
2016-11-12 19:46:58 +00:00
|
|
|
*/
|
2016-11-20 13:32:46 +00:00
|
|
|
public function format_entry( $timestamp, $level, $message, $context ) {
|
2016-11-12 19:46:58 +00:00
|
|
|
$time_string = $this->format_time( $timestamp );
|
2016-11-13 17:45:45 +00:00
|
|
|
$level_string = strtoupper( $level );
|
2016-11-19 16:38:05 +00:00
|
|
|
return "{$time_string} {$level_string} {$message}";
|
2016-11-12 19:46:58 +00:00
|
|
|
}
|
|
|
|
}
|