RestApi
1 month ago
LogHandler.php
10 months ago
LogListingRepository.php
1 month ago
LogRepository.php
2 weeks ago
LoggerFactory.php
10 months ago
LogsDownload.php
2 weeks ago
PluginVersionProcessor.php
3 years ago
index.php
3 years ago
LoggerFactory.php
120 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Logging; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\DI\ContainerWrapper; |
| 9 | use MailPoet\Settings\SettingsController; |
| 10 | use MailPoetVendor\Monolog\Processor\IntrospectionProcessor; |
| 11 | use MailPoetVendor\Monolog\Processor\MemoryUsageProcessor; |
| 12 | use MailPoetVendor\Monolog\Processor\WebProcessor; |
| 13 | |
| 14 | /** |
| 15 | * Usage: |
| 16 | * $logger = Logger::getLogger('logger name'); |
| 17 | * $logger->debug('This is a debug message'); |
| 18 | * $logger->info('This is an info'); |
| 19 | * $logger->warning('This is a warning'); |
| 20 | * $logger->error('This is an error message'); |
| 21 | * |
| 22 | * By default only errors are saved but can be changed in settings to save everything or nothing |
| 23 | * |
| 24 | * Name is anything which will be found in the log table. |
| 25 | * We can use it for separating different messages like: 'cron', 'rendering', 'export', ... |
| 26 | * |
| 27 | * If WP_DEBUG is true additional information will be added to every log message. |
| 28 | */ |
| 29 | class LoggerFactory { |
| 30 | const TOPIC_NEWSLETTERS = 'newsletters'; |
| 31 | const TOPIC_POST_NOTIFICATIONS = 'post-notifications'; |
| 32 | const TOPIC_MSS = 'mss'; |
| 33 | const TOPIC_PREMIUM = 'premium'; |
| 34 | const TOPIC_BRIDGE = 'bridge-api'; |
| 35 | const TOPIC_SENDING = 'sending'; |
| 36 | const TOPIC_CRON = 'cron'; |
| 37 | const TOPIC_API = 'api'; |
| 38 | const TOPIC_TRACKING = 'tracking'; |
| 39 | const TOPIC_COUPONS = 'coupons'; |
| 40 | const TOPIC_PROVISIONING = 'provisioning'; |
| 41 | const TOPIC_SEGMENTS = 'segments'; |
| 42 | const TOPIC_EMAIL_EDITOR = 'email-editor'; |
| 43 | |
| 44 | /** @var LoggerFactory */ |
| 45 | private static $instance; |
| 46 | |
| 47 | /** @var \MailPoetVendor\Monolog\Logger[] */ |
| 48 | private $loggerInstances = []; |
| 49 | |
| 50 | /** @var SettingsController */ |
| 51 | private $settings; |
| 52 | |
| 53 | /** @var LogRepository */ |
| 54 | private $logRepository; |
| 55 | |
| 56 | public function __construct( |
| 57 | LogRepository $logRepository, |
| 58 | SettingsController $settings |
| 59 | ) { |
| 60 | $this->settings = $settings; |
| 61 | $this->logRepository = $logRepository; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param string $name |
| 66 | * @param bool $attachOptionalProcessors |
| 67 | * |
| 68 | * @return \MailPoetVendor\Monolog\Logger |
| 69 | */ |
| 70 | public function getLogger($name = 'MailPoet', $attachOptionalProcessors = WP_DEBUG) { |
| 71 | if (!isset($this->loggerInstances[$name])) { |
| 72 | $this->loggerInstances[$name] = new \MailPoetVendor\Monolog\Logger($name); |
| 73 | |
| 74 | if ($attachOptionalProcessors) { |
| 75 | // Adds the line/file/class/method from which the log call originated |
| 76 | $this->loggerInstances[$name]->pushProcessor(new IntrospectionProcessor()); |
| 77 | // Adds the current request URI, request method and client IP to a log record |
| 78 | $this->loggerInstances[$name]->pushProcessor(new WebProcessor()); |
| 79 | // Adds the current memory usage to a log record |
| 80 | $this->loggerInstances[$name]->pushProcessor(new MemoryUsageProcessor()); |
| 81 | } |
| 82 | |
| 83 | // Adds the plugin's versions to the log, we always want to see this |
| 84 | $this->loggerInstances[$name]->pushProcessor(new PluginVersionProcessor()); |
| 85 | |
| 86 | $this->loggerInstances[$name]->pushHandler(new LogHandler( |
| 87 | $this->logRepository, |
| 88 | $this->getDefaultLogLevel() |
| 89 | )); |
| 90 | } |
| 91 | return $this->loggerInstances[$name]; |
| 92 | } |
| 93 | |
| 94 | public static function getInstance() { |
| 95 | if (!self::$instance instanceof LoggerFactory) { |
| 96 | self::$instance = new LoggerFactory( |
| 97 | ContainerWrapper::getInstance()->get(LogRepository::class), |
| 98 | SettingsController::getInstance() |
| 99 | ); |
| 100 | } |
| 101 | return self::$instance; |
| 102 | } |
| 103 | |
| 104 | public function clearLoggerInstances() { |
| 105 | $this->loggerInstances = []; |
| 106 | } |
| 107 | |
| 108 | private function getDefaultLogLevel() { |
| 109 | $logLevel = $this->settings->get('logging', 'errors'); |
| 110 | switch ($logLevel) { |
| 111 | case 'everything': |
| 112 | return \MailPoetVendor\Monolog\Logger::DEBUG; |
| 113 | case 'nothing': |
| 114 | return \MailPoetVendor\Monolog\Logger::EMERGENCY; |
| 115 | default: |
| 116 | return \MailPoetVendor\Monolog\Logger::ERROR; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |