Keine Beschreibung

class-wc-logger.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. use Automattic\Jetpack\Constants;
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * WC_Logger class.
  13. */
  14. class WC_Logger implements WC_Logger_Interface {
  15. /**
  16. * Stores registered log handlers.
  17. *
  18. * @var array
  19. */
  20. protected $handlers;
  21. /**
  22. * Minimum log level this handler will process.
  23. *
  24. * @var int Integer representation of minimum log level to handle.
  25. */
  26. protected $threshold;
  27. /**
  28. * Constructor for the logger.
  29. *
  30. * @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.
  31. * @param string $threshold Optional. Define an explicit threshold. May be configured via WC_LOG_THRESHOLD. By default, all logs will be processed.
  32. */
  33. public function __construct( $handlers = null, $threshold = null ) {
  34. if ( null === $handlers ) {
  35. $handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
  36. }
  37. $register_handlers = array();
  38. if ( ! empty( $handlers ) && is_array( $handlers ) ) {
  39. foreach ( $handlers as $handler ) {
  40. $implements = class_implements( $handler );
  41. if ( is_object( $handler ) && is_array( $implements ) && in_array( 'WC_Log_Handler_Interface', $implements, true ) ) {
  42. $register_handlers[] = $handler;
  43. } else {
  44. wc_doing_it_wrong(
  45. __METHOD__,
  46. sprintf(
  47. /* translators: 1: class name 2: WC_Log_Handler_Interface */
  48. __( 'The provided handler %1$s does not implement %2$s.', 'woocommerce' ),
  49. '<code>' . esc_html( is_object( $handler ) ? get_class( $handler ) : $handler ) . '</code>',
  50. '<code>WC_Log_Handler_Interface</code>'
  51. ),
  52. '3.0'
  53. );
  54. }
  55. }
  56. }
  57. // Support the constant as long as a valid log level has been set for it.
  58. if ( null === $threshold ) {
  59. $threshold = Constants::get_constant( 'WC_LOG_THRESHOLD' );
  60. if ( null !== $threshold && ! WC_Log_Levels::is_valid_level( $threshold ) ) {
  61. $threshold = null;
  62. }
  63. }
  64. if ( null !== $threshold ) {
  65. $threshold = WC_Log_Levels::get_level_severity( $threshold );
  66. }
  67. $this->handlers = $register_handlers;
  68. $this->threshold = $threshold;
  69. }
  70. /**
  71. * Determine whether to handle or ignore log.
  72. *
  73. * @param string $level emergency|alert|critical|error|warning|notice|info|debug.
  74. * @return bool True if the log should be handled.
  75. */
  76. protected function should_handle( $level ) {
  77. if ( null === $this->threshold ) {
  78. return true;
  79. }
  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 File handle.
  89. * @param string $message Message to log.
  90. * @param string $level Logging level.
  91. * @return bool
  92. */
  93. public function add( $handle, $message, $level = WC_Log_Levels::NOTICE ) {
  94. $message = apply_filters( 'woocommerce_logger_add_message', $message, $handle );
  95. $this->log(
  96. $level,
  97. $message,
  98. array(
  99. 'source' => $handle,
  100. '_legacy' => true,
  101. )
  102. );
  103. wc_do_deprecated_action( 'woocommerce_log_add', array( $handle, $message ), '3.0', 'This action has been deprecated with no alternative.' );
  104. return true;
  105. }
  106. /**
  107. * Add a log entry.
  108. *
  109. * @param string $level One of the following:
  110. * 'emergency': System is unusable.
  111. * 'alert': Action must be taken immediately.
  112. * 'critical': Critical conditions.
  113. * 'error': Error conditions.
  114. * 'warning': Warning conditions.
  115. * 'notice': Normal but significant condition.
  116. * 'info': Informational messages.
  117. * 'debug': Debug-level messages.
  118. * @param string $message Log message.
  119. * @param array $context Optional. Additional information for log handlers.
  120. */
  121. public function log( $level, $message, $context = array() ) {
  122. if ( ! WC_Log_Levels::is_valid_level( $level ) ) {
  123. /* translators: 1: WC_Logger::log 2: level */
  124. 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' );
  125. }
  126. if ( $this->should_handle( $level ) ) {
  127. $timestamp = time();
  128. foreach ( $this->handlers as $handler ) {
  129. /**
  130. * Filter the logging message. Returning null will prevent logging from occuring since 5.3.
  131. *
  132. * @since 3.1
  133. * @param string $message Log message.
  134. * @param string $level One of: emergency, alert, critical, error, warning, notice, info, or debug.
  135. * @param array $context Additional information for log handlers.
  136. * @param object $handler The handler object, such as WC_Log_Handler_File. Available since 5.3.
  137. */
  138. $message = apply_filters( 'woocommerce_logger_log_message', $message, $level, $context, $handler );
  139. if ( null !== $message ) {
  140. $handler->handle( $timestamp, $level, $message, $context );
  141. }
  142. }
  143. }
  144. }
  145. /**
  146. * Adds an emergency level message.
  147. *
  148. * System is unusable.
  149. *
  150. * @see WC_Logger::log
  151. *
  152. * @param string $message Message to log.
  153. * @param array $context Log context.
  154. */
  155. public function emergency( $message, $context = array() ) {
  156. $this->log( WC_Log_Levels::EMERGENCY, $message, $context );
  157. }
  158. /**
  159. * Adds an alert level message.
  160. *
  161. * Action must be taken immediately.
  162. * Example: Entire website down, database unavailable, etc.
  163. *
  164. * @see WC_Logger::log
  165. *
  166. * @param string $message Message to log.
  167. * @param array $context Log context.
  168. */
  169. public function alert( $message, $context = array() ) {
  170. $this->log( WC_Log_Levels::ALERT, $message, $context );
  171. }
  172. /**
  173. * Adds a critical level message.
  174. *
  175. * Critical conditions.
  176. * Example: Application component unavailable, unexpected exception.
  177. *
  178. * @see WC_Logger::log
  179. *
  180. * @param string $message Message to log.
  181. * @param array $context Log context.
  182. */
  183. public function critical( $message, $context = array() ) {
  184. $this->log( WC_Log_Levels::CRITICAL, $message, $context );
  185. }
  186. /**
  187. * Adds an error level message.
  188. *
  189. * Runtime errors that do not require immediate action but should typically be logged
  190. * and monitored.
  191. *
  192. * @see WC_Logger::log
  193. *
  194. * @param string $message Message to log.
  195. * @param array $context Log context.
  196. */
  197. public function error( $message, $context = array() ) {
  198. $this->log( WC_Log_Levels::ERROR, $message, $context );
  199. }
  200. /**
  201. * Adds a warning level message.
  202. *
  203. * Exceptional occurrences that are not errors.
  204. *
  205. * Example: Use of deprecated APIs, poor use of an API, undesirable things that are not
  206. * necessarily wrong.
  207. *
  208. * @see WC_Logger::log
  209. *
  210. * @param string $message Message to log.
  211. * @param array $context Log context.
  212. */
  213. public function warning( $message, $context = array() ) {
  214. $this->log( WC_Log_Levels::WARNING, $message, $context );
  215. }
  216. /**
  217. * Adds a notice level message.
  218. *
  219. * Normal but significant events.
  220. *
  221. * @see WC_Logger::log
  222. *
  223. * @param string $message Message to log.
  224. * @param array $context Log context.
  225. */
  226. public function notice( $message, $context = array() ) {
  227. $this->log( WC_Log_Levels::NOTICE, $message, $context );
  228. }
  229. /**
  230. * Adds a info level message.
  231. *
  232. * Interesting events.
  233. * Example: User logs in, SQL logs.
  234. *
  235. * @see WC_Logger::log
  236. *
  237. * @param string $message Message to log.
  238. * @param array $context Log context.
  239. */
  240. public function info( $message, $context = array() ) {
  241. $this->log( WC_Log_Levels::INFO, $message, $context );
  242. }
  243. /**
  244. * Adds a debug level message.
  245. *
  246. * Detailed debug information.
  247. *
  248. * @see WC_Logger::log
  249. *
  250. * @param string $message Message to log.
  251. * @param array $context Log context.
  252. */
  253. public function debug( $message, $context = array() ) {
  254. $this->log( WC_Log_Levels::DEBUG, $message, $context );
  255. }
  256. /**
  257. * Clear entries for a chosen file/source.
  258. *
  259. * @param string $source Source/handle to clear.
  260. * @return bool
  261. */
  262. public function clear( $source = '' ) {
  263. if ( ! $source ) {
  264. return false;
  265. }
  266. foreach ( $this->handlers as $handler ) {
  267. if ( is_callable( array( $handler, 'clear' ) ) ) {
  268. $handler->clear( $source );
  269. }
  270. }
  271. return true;
  272. }
  273. /**
  274. * Clear all logs older than a defined number of days. Defaults to 30 days.
  275. *
  276. * @since 3.4.0
  277. */
  278. public function clear_expired_logs() {
  279. $days = absint( apply_filters( 'woocommerce_logger_days_to_retain_logs', 30 ) );
  280. $timestamp = strtotime( "-{$days} days" );
  281. foreach ( $this->handlers as $handler ) {
  282. if ( is_callable( array( $handler, 'delete_logs_before_timestamp' ) ) ) {
  283. $handler->delete_logs_before_timestamp( $timestamp );
  284. }
  285. }
  286. }
  287. }