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 array $args { * @type string $threshold Optional. Default 'emergency'. Sets the log severity threshold. * emergency|alert|critical|error|warning|notice|info|debug * } */ public function __construct( $args = array() ) { $args = wp_parse_args( $args, array( 'threshold' => WC_Logger::DEBUG, ) ); $this->set_threshold( $args['threshold'] ); } /** * Handle a log entry. * * @param string $level emergency|alert|critical|error|warning|notice|info|debug * @param int $timestamp * @param string $message * @param array $context { * Optional. Array with additional information. * } * * @return bool log entry should bubble to further loggers. */ abstract public 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. * * @param int $timestamp * @return string formatted time */ public static function format_time( $timestamp ) { return date( 'c', $timestamp ); } /** * Builds a log entry text from level, timestamp and message. * * @param string $level emergency|alert|critical|error|warning|notice|info|debug * @param int $timestamp * @param string $message * * @return string Formatted log entry */ public function format_entry( $level, $timestamp, $message, $context ) { $time_string = $this->format_time( $timestamp ); $level_string = strtoupper( $level ); return "{$time_string} {$level_string} {$message}"; } }