abstract-wc-log-handler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Log handling functionality.
  4. *
  5. * @class WC_Log_Handler
  6. * @package WooCommerce/Abstracts
  7. */
  8. if ( ! defined( 'ABSPATH' ) ) {
  9. exit; // Exit if accessed directly.
  10. }
  11. /**
  12. * Abstract WC Log Handler Class
  13. *
  14. * @version 1.0.0
  15. * @package WooCommerce/Abstracts
  16. */
  17. abstract class WC_Log_Handler implements WC_Log_Handler_Interface {
  18. /**
  19. * Formats a timestamp for use in log messages.
  20. *
  21. * @param int $timestamp Log timestamp.
  22. * @return string Formatted time for use in log entry.
  23. */
  24. protected static function format_time( $timestamp ) {
  25. return date( 'c', $timestamp );
  26. }
  27. /**
  28. * Builds a log entry text from level, timestamp and message.
  29. *
  30. * @param int $timestamp Log timestamp.
  31. * @param string $level emergency|alert|critical|error|warning|notice|info|debug.
  32. * @param string $message Log message.
  33. * @param array $context Additional information for log handlers.
  34. *
  35. * @return string Formatted log entry.
  36. */
  37. protected static function format_entry( $timestamp, $level, $message, $context ) {
  38. $time_string = self::format_time( $timestamp );
  39. $level_string = strtoupper( $level );
  40. $entry = "{$time_string} {$level_string} {$message}";
  41. return apply_filters( 'woocommerce_format_log_entry', $entry, array(
  42. 'timestamp' => $timestamp,
  43. 'level' => $level,
  44. 'message' => $message,
  45. 'context' => $context,
  46. ) );
  47. }
  48. }