Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
13 / 14 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Debug_Logger | |
100.00% |
13 / 13 |
|
100.00% |
6 / 6 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| log_exception | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| log_error | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| log_message | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| log_response | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| log | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Debug logger for the CSV report export pipeline. |
| 4 | * |
| 5 | * @package automattic/jetpack-premium-analytics |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types=1 ); |
| 9 | |
| 10 | namespace Automattic\Jetpack\PremiumAnalytics\Reports\Export\Logging; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | use Throwable; |
| 15 | use WC_Log_Levels; |
| 16 | use WC_Logger_Interface; |
| 17 | |
| 18 | /** |
| 19 | * Class Debug_Logger |
| 20 | * |
| 21 | * @since $$next-version$$ |
| 22 | */ |
| 23 | class Debug_Logger implements Logger_Interface { |
| 24 | |
| 25 | /** |
| 26 | * WooCommerce logger class instance. |
| 27 | * |
| 28 | * @var WC_Logger_Interface |
| 29 | */ |
| 30 | private $logger; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | * |
| 35 | * @param WC_Logger_Interface $wc_logger The WooCommerce logger. |
| 36 | */ |
| 37 | public function __construct( WC_Logger_Interface $wc_logger ) { |
| 38 | $this->logger = $wc_logger; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Log an exception. |
| 43 | * |
| 44 | * @param Throwable $exception The exception to log. |
| 45 | * @param string $method The method that threw the exception. |
| 46 | */ |
| 47 | public function log_exception( Throwable $exception, string $method ): void { |
| 48 | $this->log( $exception->getMessage(), $method, WC_Log_Levels::ERROR ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Log an error. |
| 53 | * |
| 54 | * @param string $message The error message. |
| 55 | * @param string $method The method that threw the error. |
| 56 | */ |
| 57 | public function log_error( string $message, string $method ): void { |
| 58 | $this->log( $message, $method, WC_Log_Levels::ERROR ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Log a generic note. |
| 63 | * |
| 64 | * @param string $message The message to log. |
| 65 | * @param string $method The method that generated the message. |
| 66 | */ |
| 67 | public function log_message( string $message, string $method ): void { |
| 68 | $this->log( $message, $method ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Log a JSON response. |
| 73 | * |
| 74 | * @param mixed $response The response to log. |
| 75 | * @param string $method The method that generated the response. |
| 76 | */ |
| 77 | public function log_response( $response, string $method ): void { |
| 78 | $message = wp_json_encode( $response, JSON_PRETTY_PRINT ); |
| 79 | // wp_json_encode() returns false on failure; keep log() (string, strict_types) safe. |
| 80 | $this->log( false === $message ? '' : $message, $method ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Log a message as a debug log entry. |
| 85 | * |
| 86 | * @param string $message The message to log. |
| 87 | * @param string $method The method that generated the message. |
| 88 | * @param string $level The log level. |
| 89 | */ |
| 90 | protected function log( string $message, string $method, string $level = WC_Log_Levels::DEBUG ) { |
| 91 | $this->logger->log( |
| 92 | $level, |
| 93 | sprintf( '%s %s', $method, $message ), |
| 94 | array( |
| 95 | 'source' => 'jetpack-premium-analytics', |
| 96 | ) |
| 97 | ); |
| 98 | } |
| 99 | } |