woocommerce
/
packages
/
email-editor
/
src
/
Engine
/
Logger
/
class-default-email-editor-logger.php
class-default-email-editor-logger.php
10 months ago
class-email-editor-logger-interface.php
1 year ago
class-email-editor-logger.php
1 year ago
class-default-email-editor-logger.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types = 1); |
| 9 | |
| 10 | namespace Automattic\WooCommerce\EmailEditor\Engine\Logger; |
| 11 | |
| 12 | /** |
| 13 | * Default implementation of the email editor logger that writes to WordPress debug log. |
| 14 | */ |
| 15 | class Default_Email_Editor_Logger implements Email_Editor_Logger_Interface { |
| 16 | /** |
| 17 | * Log levels. |
| 18 | */ |
| 19 | public const EMERGENCY = 'emergency'; |
| 20 | public const ALERT = 'alert'; |
| 21 | public const CRITICAL = 'critical'; |
| 22 | public const ERROR = 'error'; |
| 23 | public const WARNING = 'warning'; |
| 24 | public const NOTICE = 'notice'; |
| 25 | public const INFO = 'info'; |
| 26 | public const DEBUG = 'debug'; |
| 27 | |
| 28 | /** |
| 29 | * Path to the log file. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | private $log_file; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | if ( defined( 'WP_DEBUG_LOG' ) ) { |
| 40 | if ( true === WP_DEBUG_LOG ) { |
| 41 | $this->log_file = WP_CONTENT_DIR . '/debug.log'; |
| 42 | } elseif ( is_string( WP_DEBUG_LOG ) && ! empty( WP_DEBUG_LOG ) ) { |
| 43 | $this->log_file = WP_DEBUG_LOG; |
| 44 | } else { |
| 45 | $this->log_file = ''; |
| 46 | } |
| 47 | } else { |
| 48 | $this->log_file = ''; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * System is unusable. |
| 54 | * |
| 55 | * @param string $message The log message. |
| 56 | * @param array $context The log context. |
| 57 | * @return void |
| 58 | */ |
| 59 | public function emergency( string $message, array $context = array() ): void { |
| 60 | $this->log( self::EMERGENCY, $message, $context ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Action must be taken immediately. |
| 65 | * |
| 66 | * @param string $message The log message. |
| 67 | * @param array $context The log context. |
| 68 | * @return void |
| 69 | */ |
| 70 | public function alert( string $message, array $context = array() ): void { |
| 71 | $this->log( self::ALERT, $message, $context ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Critical conditions. |
| 76 | * |
| 77 | * @param string $message The log message. |
| 78 | * @param array $context The log context. |
| 79 | * @return void |
| 80 | */ |
| 81 | public function critical( string $message, array $context = array() ): void { |
| 82 | $this->log( self::CRITICAL, $message, $context ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Runtime errors that do not require immediate action but should typically |
| 87 | * be logged and monitored. |
| 88 | * |
| 89 | * @param string $message The log message. |
| 90 | * @param array $context The log context. |
| 91 | * @return void |
| 92 | */ |
| 93 | public function error( string $message, array $context = array() ): void { |
| 94 | $this->log( self::ERROR, $message, $context ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Exceptional occurrences that are not errors. |
| 99 | * |
| 100 | * @param string $message The log message. |
| 101 | * @param array $context The log context. |
| 102 | * @return void |
| 103 | */ |
| 104 | public function warning( string $message, array $context = array() ): void { |
| 105 | $this->log( self::WARNING, $message, $context ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Normal but significant events. |
| 110 | * |
| 111 | * @param string $message The log message. |
| 112 | * @param array $context The log context. |
| 113 | * @return void |
| 114 | */ |
| 115 | public function notice( string $message, array $context = array() ): void { |
| 116 | $this->log( self::NOTICE, $message, $context ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Interesting events. |
| 121 | * |
| 122 | * @param string $message The log message. |
| 123 | * @param array $context The log context. |
| 124 | * @return void |
| 125 | */ |
| 126 | public function info( string $message, array $context = array() ): void { |
| 127 | $this->log( self::INFO, $message, $context ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Detailed debug information. |
| 132 | * |
| 133 | * @param string $message The log message. |
| 134 | * @param array $context The log context. |
| 135 | * @return void |
| 136 | */ |
| 137 | public function debug( string $message, array $context = array() ): void { |
| 138 | $this->log( self::DEBUG, $message, $context ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Logs with an arbitrary level. |
| 143 | * |
| 144 | * @param string $level The log level. |
| 145 | * @param string $message The log message. |
| 146 | * @param array $context The log context. |
| 147 | * @return void |
| 148 | */ |
| 149 | public function log( string $level, string $message, array $context = array() ): void { |
| 150 | if ( ! $this->log_file ) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | $entry = sprintf( |
| 155 | '[%s] %s: %s %s', |
| 156 | gmdate( 'Y-m-d H:i:s' ), |
| 157 | strtoupper( $level ), |
| 158 | $message, |
| 159 | ! empty( $context ) ? wp_json_encode( $context ) : '' |
| 160 | ); |
| 161 | |
| 162 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- This is a logging class, error_log is the intended functionality. |
| 163 | error_log( $entry . PHP_EOL, 3, $this->log_file ); |
| 164 | } |
| 165 | } |
| 166 |