/includes/class-wc-logger.php

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