Blocks
10 months ago
Coupons
1 month ago
Endpoints
2 months ago
Patterns
1 month ago
PersonalizationTags
4 days ago
ProductCollection
1 month ago
Templates
2 months ago
AutomationEmailContextProvider.php
2 months ago
AutomationEmailPreviewOrderProvider.php
2 months ago
BlockEmailContentDetector.php
2 months ago
Cli.php
2 weeks ago
DependencyNotice.php
11 months ago
EditorPageRenderer.php
1 week ago
EmailApiController.php
1 week ago
EmailEditor.php
2 months ago
EmailEditorPreviewEmail.php
9 months ago
Logger.php
10 months ago
MailPoetCssInliner.php
8 months ago
MailpoetCssInlinerFactory.php
1 year ago
PersonalizationTagManager.php
5 days ago
index.php
2 years ago
Logger.php
183 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\EmailEditor\Integrations\MailPoet; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use Automattic\WooCommerce\EmailEditor\Engine\Logger\Email_Editor_Logger_Interface; |
| 9 | use MailPoet\Logging\LoggerFactory; |
| 10 | use MailPoet\Settings\SettingsController; |
| 11 | use MailPoetVendor\Monolog\Logger as MonologLogger; |
| 12 | |
| 13 | /** |
| 14 | * MailPoet logger adapter for the email editor. |
| 15 | * |
| 16 | * This class adapts the a logger instance from the factory to work with the email editor logging interface. |
| 17 | */ |
| 18 | class Logger implements Email_Editor_Logger_Interface { |
| 19 | |
| 20 | private MonologLogger $mailpoetLogger; |
| 21 | private SettingsController $settings; |
| 22 | |
| 23 | public function __construct() { |
| 24 | $this->mailpoetLogger = LoggerFactory::getInstance()->getLogger(LoggerFactory::TOPIC_EMAIL_EDITOR); |
| 25 | $this->settings = SettingsController::getInstance(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Determines if a log level should be logged based on MailPoet settings and WP_DEBUG. |
| 30 | * |
| 31 | * @param int $level The Monolog log level constant |
| 32 | * @return bool True if the log level should be logged, false otherwise |
| 33 | */ |
| 34 | private function shouldLogLevel(int $level): bool { |
| 35 | $mailpoetLogLevel = $this->settings->get('logging', 'errors'); |
| 36 | |
| 37 | if ($mailpoetLogLevel === 'nothing') { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | if ($mailpoetLogLevel === 'errors') { |
| 42 | return $level >= MonologLogger::ERROR; |
| 43 | } |
| 44 | |
| 45 | if ($mailpoetLogLevel === 'everything') { |
| 46 | if (defined('WP_DEBUG') && WP_DEBUG) { |
| 47 | return $level >= MonologLogger::DEBUG; |
| 48 | } |
| 49 | |
| 50 | // If WP_DEBUG is disabled, log INFO level and above to reduce log entries. |
| 51 | return $level >= MonologLogger::INFO; |
| 52 | } |
| 53 | |
| 54 | return $level >= MonologLogger::ERROR; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Adds emergency level log message. |
| 59 | * |
| 60 | * @param string $message The log message. |
| 61 | * @param array $context The log context. |
| 62 | * @return void |
| 63 | */ |
| 64 | public function emergency(string $message, array $context = []): void { |
| 65 | if ($this->shouldLogLevel(MonologLogger::EMERGENCY)) { |
| 66 | $this->mailpoetLogger->emergency($message, $context); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Adds alert level log message. |
| 72 | * |
| 73 | * @param string $message The log message. |
| 74 | * @param array $context The log context. |
| 75 | * @return void |
| 76 | */ |
| 77 | public function alert(string $message, array $context = []): void { |
| 78 | if ($this->shouldLogLevel(MonologLogger::ALERT)) { |
| 79 | $this->mailpoetLogger->alert($message, $context); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Adds critical level log message. |
| 85 | * |
| 86 | * @param string $message The log message. |
| 87 | * @param array $context The log context. |
| 88 | * @return void |
| 89 | */ |
| 90 | public function critical(string $message, array $context = []): void { |
| 91 | if ($this->shouldLogLevel(MonologLogger::CRITICAL)) { |
| 92 | $this->mailpoetLogger->critical($message, $context); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Adds error level log message. |
| 98 | * |
| 99 | * @param string $message The log message. |
| 100 | * @param array $context The log context. |
| 101 | * @return void |
| 102 | */ |
| 103 | public function error(string $message, array $context = []): void { |
| 104 | if ($this->shouldLogLevel(MonologLogger::ERROR)) { |
| 105 | $this->mailpoetLogger->error($message, $context); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Adds warning level log message. |
| 111 | * |
| 112 | * @param string $message The log message. |
| 113 | * @param array $context The log context. |
| 114 | * @return void |
| 115 | */ |
| 116 | public function warning(string $message, array $context = []): void { |
| 117 | if ($this->shouldLogLevel(MonologLogger::WARNING)) { |
| 118 | $this->mailpoetLogger->warning($message, $context); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Adds notice level log message. |
| 124 | * |
| 125 | * @param string $message The log message. |
| 126 | * @param array $context The log context. |
| 127 | * @return void |
| 128 | */ |
| 129 | public function notice(string $message, array $context = []): void { |
| 130 | if ($this->shouldLogLevel(MonologLogger::NOTICE)) { |
| 131 | $this->mailpoetLogger->notice($message, $context); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Adds info level log message. |
| 137 | * |
| 138 | * @param string $message The log message. |
| 139 | * @param array $context The log context. |
| 140 | * @return void |
| 141 | */ |
| 142 | public function info(string $message, array $context = []): void { |
| 143 | if ($this->shouldLogLevel(MonologLogger::INFO)) { |
| 144 | $this->mailpoetLogger->info($message, $context); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Adds debug level log message. |
| 150 | * |
| 151 | * @param string $message The log message. |
| 152 | * @param array $context The log context. |
| 153 | * @return void |
| 154 | */ |
| 155 | public function debug(string $message, array $context = []): void { |
| 156 | if ($this->shouldLogLevel(MonologLogger::DEBUG)) { |
| 157 | $this->mailpoetLogger->debug($message, $context); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Logs with an arbitrary level. |
| 163 | * |
| 164 | * @param string $level The log level. |
| 165 | * @param string $message The log message. |
| 166 | * @param array $context The log context. |
| 167 | * @return void |
| 168 | */ |
| 169 | public function log(string $level, string $message, array $context = []): void { |
| 170 | try { |
| 171 | /** @phpstan-ignore-next-line toMonologLevel expects specific string or numeric values, but we handle invalid values gracefully with fallback */ |
| 172 | $monologLevel = MonologLogger::toMonologLevel($level); |
| 173 | } catch (\Exception $e) { |
| 174 | $monologLevel = MonologLogger::DEBUG; |
| 175 | } |
| 176 | |
| 177 | if ($this->shouldLogLevel($monologLevel)) { |
| 178 | /** @phpstan-ignore-next-line PHPStan reports string in level as an error but it's okay */ |
| 179 | $this->mailpoetLogger->log($level, $message, $context); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 |