woocommerce/includes/class-wc-logger.php

231 lines
6.1 KiB
PHP
Raw Normal View History

<?php
2015-11-06 09:22:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Provides logging capabilities for debugging purposes.
*
* @class WC_Logger
* @version 2.0.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
2012-01-27 16:38:39 +00:00
class WC_Logger {
2012-08-14 19:42:38 +00:00
/**
* Stores registered log handlers.
*
* @var array
2012-08-15 17:08:42 +00:00
*/
2016-12-18 20:27:47 +00:00
protected $handlers;
2012-08-14 19:42:38 +00:00
/**
* Minimum log level this handler will process.
*
* @var int Integer representation of minimum log level to handle.
*/
protected $threshold;
2012-08-14 19:42:38 +00:00
/**
2012-08-15 17:08:42 +00:00
* Constructor for the logger.
*
* @param array $handlers Optional. Array of log handlers. If $handlers is not provided,
* 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.
2012-08-14 19:42:38 +00:00
*/
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' );
}
$this->handlers = $handlers;
$this->threshold = WC_Log_Levels::get_level_severity( $threshold );
}
/**
2016-12-18 20:27:47 +00:00
* Determine whether to handle or ignore 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 <= WC_Log_Levels::get_level_severity( $level );
}
2012-08-14 19:42:38 +00:00
/**
* Add a log entry.
*
2016-12-21 19:35:25 +00:00
* This is not the preferred method for adding log messages. Please use log() or any one of
* the level methods (debug(), info(), etc.). This method may be deprecated in the future.
*
* @param string $handle
* @param string $message
* @return bool
2012-08-14 19:42:38 +00:00
*/
2016-12-21 21:34:13 +00:00
public function add( $handle, $message, $level = WC_Log_Levels::NOTICE ) {
$message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
2016-12-21 19:35:25 +00:00
$this->log( $level, $message, array( 'source' => $handle, '_legacy' => true ) );
wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '2.7', 'This action has been deprecated with no alternative.' );
return true;
}
2012-08-14 19:42:38 +00:00
/**
* Add a log entry.
2012-08-14 19:42:38 +00:00
*
2016-12-10 21:31:32 +00:00
* @param string $level One of the following:
* 'emergency': System is unusable.
* 'alert': Action must be taken immediately.
* 'critical': Critical conditions.
* 'error': Error conditions.
* 'warning': Warning conditions.
* 'notice': Normal but significant condition.
* 'informational': Informational messages.
* 'debug': Debug-level messages.
* @param string $message Log message.
* @param array $context Optional. Additional information for log handlers.
*/
public function log( $level, $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
if ( ! WC_Log_Levels::is_valid_level( $level ) ) {
$class = __CLASS__;
$method = __FUNCTION__;
wc_doing_it_wrong( "{$class}::{$method}", sprintf( __( 'WC_Logger::log was called with an invalid level "%s".', 'woocommerce' ), $level ), '2.7' );
}
if ( $this->should_handle( $level ) ) {
$timestamp = current_time( 'timestamp' );
2012-08-14 19:42:38 +00:00
foreach ( $this->handlers as $handler ) {
$handler->handle( $timestamp, $level, $message, $context );
}
2016-09-12 22:37:02 +00:00
}
}
2012-08-14 19:42:38 +00:00
2016-04-19 15:56:41 +00:00
/**
* Adds an emergency level message.
*
2016-12-10 21:31:32 +00:00
* System is unusable.
*
* @see WC_Logger::log
2016-04-19 15:56:41 +00:00
*/
public function emergency( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::EMERGENCY, $message, $context );
2016-04-19 15:56:41 +00:00
}
/**
* Adds an alert level message.
2012-08-14 19:42:38 +00:00
*
2016-12-10 21:31:32 +00:00
* Action must be taken immediately.
* Example: Entire website down, database unavailable, etc.
*
* @see WC_Logger::log
*/
public function alert( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::ALERT, $message, $context );
}
2012-08-14 19:42:38 +00:00
/**
* Adds a critical level message.
*
2016-12-10 21:31:32 +00:00
* Critical conditions.
* Example: Application component unavailable, unexpected exception.
*
* @see WC_Logger::log
*/
public function critical( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::CRITICAL, $message, $context );
}
2012-08-14 19:42:38 +00:00
/**
* Adds an error level message.
2012-08-14 19:42:38 +00:00
*
2016-12-10 21:31:32 +00:00
* Runtime errors that do not require immediate action but should typically be logged
* and monitored.
*
* @see WC_Logger::log
*/
public function error( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::ERROR, $message, $context );
}
2015-07-16 19:55:48 +00:00
/**
* Adds a warning level message.
*
2016-12-10 21:31:32 +00:00
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things that are not
* necessarily wrong.
*
* @see WC_Logger::log
*/
public function warning( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::WARNING, $message, $context );
}
/**
* Adds a notice level message.
*
2016-12-10 21:31:32 +00:00
* Normal but significant events.
*
* @see WC_Logger::log
*/
public function notice( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::NOTICE, $message, $context );
}
2012-11-27 16:22:47 +00:00
/**
* Adds a info level message.
*
2016-12-10 21:31:32 +00:00
* Interesting events.
* Example: User logs in, SQL logs.
*
* @see WC_Logger::log
*/
public function info( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::INFO, $message, $context );
}
/**
* Adds a debug level message.
*
2016-12-10 21:31:32 +00:00
* Detailed debug information.
*
* @see WC_Logger::log
*/
public function debug( $message, $context = array() ) {
2016-12-10 20:59:41 +00:00
$this->log( WC_Log_Levels::DEBUG, $message, $context );
}
/**
* Clear entries from chosen file.
*
* @deprecated 2.7.0
*
* @return bool
*/
2016-11-14 20:55:21 +00:00
public function clear() {
wc_deprecated_function( 'WC_Logger::clear', '2.7', 'WC_Log_Handler_File::clear' );
return false;
}
/**
* Remove/delete the chosen file.
*
* @deprecated 2.7.0
*
* @return bool
*/
2016-11-14 20:55:21 +00:00
public function remove() {
wc_deprecated_function( 'WC_Logger::remove', '2.7', 'WC_Log_Handler_File::remove' );
return false;
}
2016-08-02 13:42:27 +00:00
}