class-wc-logger.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * Provides logging capabilities for debugging purposes.
  4. *
  5. * @class WC_Logger
  6. * @version 2.0.0
  7. * @package WooCommerce/Classes
  8. */
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * WC_Logger class.
  12. */
  13. class WC_Logger implements WC_Logger_Interface {
  14. /**
  15. * Stores registered log handlers.
  16. *
  17. * @var array
  18. */
  19. protected $handlers;
  20. /**
  21. * Minimum log level this handler will process.
  22. *
  23. * @var int Integer representation of minimum log level to handle.
  24. */
  25. protected $threshold;
  26. /**
  27. * Constructor for the logger.
  28. *
  29. * @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.
  30. * @param string $threshold Optional. Define an explicit threshold. May be configured via WC_LOG_THRESHOLD. By default, all logs will be processed.
  31. */
  32. public function __construct( $handlers = null, $threshold = null ) {
  33. if ( null === $handlers ) {
  34. $handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
  35. }
  36. $register_handlers = array();
  37. if ( ! empty( $handlers ) && is_array( $handlers ) ) {
  38. foreach ( $handlers as $handler ) {
  39. $implements = class_implements( $handler );
  40. if ( is_object( $handler ) && is_array( $implements ) && in_array( 'WC_Log_Handler_Interface', $implements, true ) ) {
  41. $register_handlers[] = $handler;
  42. } else {
  43. wc_doing_it_wrong(
  44. __METHOD__,
  45. sprintf(
  46. /* translators: 1: class name 2: WC_Log_Handler_Interface */
  47. __( 'The provided handler %1$s does not implement %2$s.', 'woocommerce' ),
  48. '<code>' . esc_html( is_object( $handler ) ? get_class( $handler ) : $handler ) . '</code>',
  49. '<code>WC_Log_Handler_Interface</code>'
  50. ),
  51. '3.0'
  52. );
  53. }
  54. }
  55. }
  56. if ( null !== $threshold ) {
  57. $threshold = WC_Log_Levels::get_level_severity( $threshold );
  58. } elseif ( defined( 'WC_LOG_THRESHOLD' ) && WC_Log_Levels::is_valid_level( WC_LOG_THRESHOLD ) ) {
  59. $threshold = WC_Log_Levels::get_level_severity( WC_LOG_THRESHOLD );
  60. } else {
  61. $threshold = null;
  62. }
  63. $this->handlers = $register_handlers;
  64. $this->threshold = $threshold;
  65. }
  66. /**
  67. * Determine whether to handle or ignore log.
  68. *
  69. * @param string $level emergency|alert|critical|error|warning|notice|info|debug.
  70. * @return bool True if the log should be handled.
  71. */
  72. protected function should_handle( $level ) {
  73. if ( null === $this->threshold ) {
  74. return true;
  75. }
  76. return $this->threshold <= WC_Log_Levels::get_level_severity( $level );
  77. }
  78. /**
  79. * Add a log entry.
  80. *
  81. * This is not the preferred method for adding log messages. Please use log() or any one of
  82. * the level methods (debug(), info(), etc.). This method may be deprecated in the future.
  83. *
  84. * @param string $handle File handle.
  85. * @param string $message Message to log.
  86. * @param string $level Logging level.
  87. * @return bool
  88. */
  89. public function add( $handle, $message, $level = WC_Log_Levels::NOTICE ) {
  90. $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
  91. $this->log( $level, $message, array(
  92. 'source' => $handle,
  93. '_legacy' => true,
  94. ) );
  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. * 'info': 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. /* translators: 1: WC_Logger::log 2: level */
  116. wc_doing_it_wrong( __METHOD__, sprintf( __( '%1$s was called with an invalid level "%2$s".', 'woocommerce' ), '<code>WC_Logger::log</code>', $level ), '3.0' );
  117. }
  118. if ( $this->should_handle( $level ) ) {
  119. $timestamp = current_time( 'timestamp' );
  120. $message = apply_filters( 'woocommerce_logger_log_message', $message, $level, $context );
  121. foreach ( $this->handlers as $handler ) {
  122. $handler->handle( $timestamp, $level, $message, $context );
  123. }
  124. }
  125. }
  126. /**
  127. * Adds an emergency level message.
  128. *
  129. * System is unusable.
  130. *
  131. * @see WC_Logger::log
  132. *
  133. * @param string $message Message to log.
  134. * @param array $context Log context.
  135. */
  136. public function emergency( $message, $context = array() ) {
  137. $this->log( WC_Log_Levels::EMERGENCY, $message, $context );
  138. }
  139. /**
  140. * Adds an alert level message.
  141. *
  142. * Action must be taken immediately.
  143. * Example: Entire website down, database unavailable, etc.
  144. *
  145. * @see WC_Logger::log
  146. *
  147. * @param string $message Message to log.
  148. * @param array $context Log context.
  149. */
  150. public function alert( $message, $context = array() ) {
  151. $this->log( WC_Log_Levels::ALERT, $message, $context );
  152. }
  153. /**
  154. * Adds a critical level message.
  155. *
  156. * Critical conditions.
  157. * Example: Application component unavailable, unexpected exception.
  158. *
  159. * @see WC_Logger::log
  160. *
  161. * @param string $message Message to log.
  162. * @param array $context Log context.
  163. */
  164. public function critical( $message, $context = array() ) {
  165. $this->log( WC_Log_Levels::CRITICAL, $message, $context );
  166. }
  167. /**
  168. * Adds an error level message.
  169. *
  170. * Runtime errors that do not require immediate action but should typically be logged
  171. * and monitored.
  172. *
  173. * @see WC_Logger::log
  174. *
  175. * @param string $message Message to log.
  176. * @param array $context Log context.
  177. */
  178. public function error( $message, $context = array() ) {
  179. $this->log( WC_Log_Levels::ERROR, $message, $context );
  180. }
  181. /**
  182. * Adds a warning level message.
  183. *
  184. * Exceptional occurrences that are not errors.
  185. *
  186. * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not
  187. * necessarily wrong.
  188. *
  189. * @see WC_Logger::log
  190. *
  191. * @param string $message Message to log.
  192. * @param array $context Log context.
  193. */
  194. public function warning( $message, $context = array() ) {
  195. $this->log( WC_Log_Levels::WARNING, $message, $context );
  196. }
  197. /**
  198. * Adds a notice level message.
  199. *
  200. * Normal but significant events.
  201. *
  202. * @see WC_Logger::log
  203. *
  204. * @param string $message Message to log.
  205. * @param array $context Log context.
  206. */
  207. public function notice( $message, $context = array() ) {
  208. $this->log( WC_Log_Levels::NOTICE, $message, $context );
  209. }
  210. /**
  211. * Adds a info level message.
  212. *
  213. * Interesting events.
  214. * Example: User logs in, SQL logs.
  215. *
  216. * @see WC_Logger::log
  217. *
  218. * @param string $message Message to log.
  219. * @param array $context Log context.
  220. */
  221. public function info( $message, $context = array() ) {
  222. $this->log( WC_Log_Levels::INFO, $message, $context );
  223. }
  224. /**
  225. * Adds a debug level message.
  226. *
  227. * Detailed debug information.
  228. *
  229. * @see WC_Logger::log
  230. *
  231. * @param string $message Message to log.
  232. * @param array $context Log context.
  233. */
  234. public function debug( $message, $context = array() ) {
  235. $this->log( WC_Log_Levels::DEBUG, $message, $context );
  236. }
  237. /**
  238. * Clear entries for a chosen file/source.
  239. *
  240. * @param string $source Source/handle to clear.
  241. * @return bool
  242. */
  243. public function clear( $source = '' ) {
  244. if ( ! $source ) {
  245. return false;
  246. }
  247. foreach ( $this->handlers as $handler ) {
  248. if ( is_callable( array( $handler, 'clear' ) ) ) {
  249. $handler->clear( $source );
  250. }
  251. }
  252. return true;
  253. }
  254. /**
  255. * Clear all logs older than a defined number of days. Defaults to 30 days.
  256. *
  257. * @since 3.4.0
  258. */
  259. public function clear_expired_logs() {
  260. $days = absint( apply_filters( 'woocommerce_logger_days_to_retain_logs', 30 ) );
  261. $timestamp = strtotime( "-{$days} days" );
  262. foreach ( $this->handlers as $handler ) {
  263. if ( is_callable( array( $handler, 'delete_logs_before_timestamp' ) ) ) {
  264. $handler->delete_logs_before_timestamp( $timestamp );
  265. }
  266. }
  267. }
  268. }