WC_Logger

Provides logging capabilities for debugging purposes.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-logger.php  
  1. class WC_Logger implements WC_Logger_Interface { 
  2.  
  3. /** 
  4. * Stores registered log handlers. 
  5. * @var array 
  6. */ 
  7. protected $handlers; 
  8.  
  9. /** 
  10. * Minimum log level this handler will process. 
  11. * @var int Integer representation of minimum log level to handle. 
  12. */ 
  13. protected $threshold; 
  14.  
  15. /** 
  16. * Constructor for the logger. 
  17. * @param array $handlers Optional. Array of log handlers. If $handlers is not provided,  
  18. * the filter 'woocommerce_register_log_handlers' will be used to define the handlers. 
  19. * If $handlers is provided, the filter will not be applied and the handlers will be 
  20. * used directly. 
  21. * @param string $threshold Optional. Define an explicit threshold. May be configured 
  22. * via WC_LOG_THRESHOLD. By default, all logs will be processed. 
  23. */ 
  24. public function __construct( $handlers = null, $threshold = null ) { 
  25. if ( null === $handlers ) { 
  26. $handlers = apply_filters( 'woocommerce_register_log_handlers', array() ); 
  27.  
  28. $register_handlers = array(); 
  29.  
  30. if ( ! empty( $handlers ) && is_array( $handlers ) ) { 
  31. foreach ( $handlers as $handler ) { 
  32. $implements = class_implements( $handler ); 
  33. if ( is_object( $handler ) && is_array( $implements ) && in_array( 'WC_Log_Handler_Interface', $implements ) ) { 
  34. $register_handlers[] = $handler; 
  35. } else { 
  36. wc_doing_it_wrong( 
  37. __METHOD__,  
  38. sprintf( 
  39. __( 'The provided handler <code>%s</code> does not implement WC_Log_Handler_Interface.', 'woocommerce' ),  
  40. esc_html( is_object( $handler ) ? get_class( $handler ) : $handler ) 
  41. ),  
  42. '3.0' 
  43. ); 
  44.  
  45. if ( null !== $threshold ) { 
  46. $threshold = WC_Log_Levels::get_level_severity( $threshold ); 
  47. } elseif ( defined( 'WC_LOG_THRESHOLD' ) && WC_Log_Levels::is_valid_level( WC_LOG_THRESHOLD ) ) { 
  48. $threshold = WC_Log_Levels::get_level_severity( WC_LOG_THRESHOLD ); 
  49. } else { 
  50. $threshold = null; 
  51.  
  52. $this->handlers = $register_handlers; 
  53. $this->threshold = $threshold; 
  54.  
  55. /** 
  56. * Determine whether to handle or ignore log. 
  57. * @param string $level emergency|alert|critical|error|warning|notice|info|debug 
  58. * @return bool True if the log should be handled. 
  59. */ 
  60. protected function should_handle( $level ) { 
  61. if ( null === $this->threshold ) { 
  62. return true; 
  63. return $this->threshold <= WC_Log_Levels::get_level_severity( $level ); 
  64.  
  65. /** 
  66. * Add a log entry. 
  67. * This is not the preferred method for adding log messages. Please use log() or any one of 
  68. * the level methods (debug(), info(), etc.). This method may be deprecated in the future. 
  69. * @param string $handle 
  70. * @param string $message 
  71. * @return bool 
  72. */ 
  73. public function add( $handle, $message, $level = WC_Log_Levels::NOTICE ) { 
  74. $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle ); 
  75. $this->log( $level, $message, array( 'source' => $handle, '_legacy' => true ) ); 
  76. wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '3.0', 'This action has been deprecated with no alternative.' ); 
  77. return true; 
  78.  
  79. /** 
  80. * Add a log entry. 
  81. * @param string $level One of the following: 
  82. * 'emergency': System is unusable. 
  83. * 'alert': Action must be taken immediately. 
  84. * 'critical': Critical conditions. 
  85. * 'error': Error conditions. 
  86. * 'warning': Warning conditions. 
  87. * 'notice': Normal but significant condition. 
  88. * 'informational': Informational messages. 
  89. * 'debug': Debug-level messages. 
  90. * @param string $message Log message. 
  91. * @param array $context Optional. Additional information for log handlers. 
  92. */ 
  93. public function log( $level, $message, $context = array() ) { 
  94. if ( ! WC_Log_Levels::is_valid_level( $level ) ) { 
  95. wc_doing_it_wrong( __METHOD__, sprintf( __( 'WC_Logger::log was called with an invalid level "%s".', 'woocommerce' ), $level ), '3.0' ); 
  96.  
  97. if ( $this->should_handle( $level ) ) { 
  98. $timestamp = current_time( 'timestamp' ); 
  99.  
  100. foreach ( $this->handlers as $handler ) { 
  101. $handler->handle( $timestamp, $level, $message, $context ); 
  102.  
  103. /** 
  104. * Adds an emergency level message. 
  105. * System is unusable. 
  106. * @see WC_Logger::log 
  107. */ 
  108. public function emergency( $message, $context = array() ) { 
  109. $this->log( WC_Log_Levels::EMERGENCY, $message, $context ); 
  110.  
  111. /** 
  112. * Adds an alert level message. 
  113. * Action must be taken immediately. 
  114. * Example: Entire website down, database unavailable, etc. 
  115. * @see WC_Logger::log 
  116. */ 
  117. public function alert( $message, $context = array() ) { 
  118. $this->log( WC_Log_Levels::ALERT, $message, $context ); 
  119.  
  120. /** 
  121. * Adds a critical level message. 
  122. * Critical conditions. 
  123. * Example: Application component unavailable, unexpected exception. 
  124. * @see WC_Logger::log 
  125. */ 
  126. public function critical( $message, $context = array() ) { 
  127. $this->log( WC_Log_Levels::CRITICAL, $message, $context ); 
  128.  
  129. /** 
  130. * Adds an error level message. 
  131. * Runtime errors that do not require immediate action but should typically be logged 
  132. * and monitored. 
  133. * @see WC_Logger::log 
  134. */ 
  135. public function error( $message, $context = array() ) { 
  136. $this->log( WC_Log_Levels::ERROR, $message, $context ); 
  137.  
  138. /** 
  139. * Adds a warning level message. 
  140. * Exceptional occurrences that are not errors. 
  141. * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not 
  142. * necessarily wrong. 
  143. * @see WC_Logger::log 
  144. */ 
  145. public function warning( $message, $context = array() ) { 
  146. $this->log( WC_Log_Levels::WARNING, $message, $context ); 
  147.  
  148. /** 
  149. * Adds a notice level message. 
  150. * Normal but significant events. 
  151. * @see WC_Logger::log 
  152. */ 
  153. public function notice( $message, $context = array() ) { 
  154. $this->log( WC_Log_Levels::NOTICE, $message, $context ); 
  155.  
  156. /** 
  157. * Adds a info level message. 
  158. * Interesting events. 
  159. * Example: User logs in, SQL logs. 
  160. * @see WC_Logger::log 
  161. */ 
  162. public function info( $message, $context = array() ) { 
  163. $this->log( WC_Log_Levels::INFO, $message, $context ); 
  164.  
  165. /** 
  166. * Adds a debug level message. 
  167. * Detailed debug information. 
  168. * @see WC_Logger::log 
  169. */ 
  170. public function debug( $message, $context = array() ) { 
  171. $this->log( WC_Log_Levels::DEBUG, $message, $context ); 
  172.  
  173. /** 
  174. * Clear entries from chosen file. 
  175. * @deprecated 3.0.0 
  176. * @param string $handle 
  177. * @return bool 
  178. */ 
  179. public function clear( $handle ) { 
  180. wc_deprecated_function( 'WC_Logger::clear', '3.0', 'WC_Log_Handler_File::clear' ); 
  181. $handler = new WC_Log_Handler_File(); 
  182. return $handler->clear( $handle );