Processor
1 week ago
WC
1 week ago
WP
1 week ago
deprecated
1 week ago
BasicLoggerFactory.php
1 week ago
LoggerFacade.php
1 week ago
LoggerFactory.php
1 week ago
Settings.php
1 week ago
SimpleLoggerFactory.php
1 week ago
WPDeskLoggerFactory.php
1 week ago
WPDeskLoggerFactory.php
212 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Logger; |
| 4 | |
| 5 | use Exception; |
| 6 | use InvalidArgumentException; |
| 7 | use LogicException; |
| 8 | use FSVendor\Monolog\Handler\NullHandler; |
| 9 | use FSVendor\Monolog\Logger; |
| 10 | use FSVendor\Monolog\Registry; |
| 11 | use FSVendor\Monolog\Handler\StreamHandler; |
| 12 | use FSVendor\Psr\Log\LogLevel; |
| 13 | use FSVendor\WPDesk\Logger\WC\WooCommerceCapture; |
| 14 | use FSVendor\WPDesk\Logger\WP\WPCapture; |
| 15 | /** |
| 16 | * Manages and facilitates creation of logger |
| 17 | * |
| 18 | * @deprecated 1.13.0 Creates shared log file, which is discouraged. Prefer using {@see SimpleLoggerFactory}. |
| 19 | * |
| 20 | * @package WPDesk\Logger |
| 21 | */ |
| 22 | class WPDeskLoggerFactory extends BasicLoggerFactory |
| 23 | { |
| 24 | const DEFAULT_LOGGER_CHANNEL_NAME = 'wpdesk'; |
| 25 | /** @var string Log to file when level is */ |
| 26 | const LEVEL_WPDESK_FILE = LogLevel::DEBUG; |
| 27 | /** @var string Log to wc logger when level is */ |
| 28 | const LEVEL_WC = LogLevel::ERROR; |
| 29 | /** @var bool Will factory return null logger or not */ |
| 30 | public static $shouldLoggerBeActivated = \true; |
| 31 | /** |
| 32 | * Remove static instances. In general should be use only testing purposes. |
| 33 | * |
| 34 | * @param string $name Name of the logger |
| 35 | */ |
| 36 | public static function tearDown($name = self::DEFAULT_LOGGER_CHANNEL_NAME) |
| 37 | { |
| 38 | if (Registry::hasLogger($name)) { |
| 39 | Registry::removeLogger($name); |
| 40 | } |
| 41 | } |
| 42 | /** |
| 43 | * Disable logger. Still exists but logs won't be saved |
| 44 | * |
| 45 | * @param string $name Name of the logger |
| 46 | */ |
| 47 | public function disableLog($name = self::DEFAULT_LOGGER_CHANNEL_NAME) |
| 48 | { |
| 49 | if (!Registry::hasLogger($name)) { |
| 50 | $this->createWPDeskLogger($name); |
| 51 | } |
| 52 | if (Registry::hasLogger($name)) { |
| 53 | /** @var Logger $logger */ |
| 54 | $logger = Registry::getInstance($name); |
| 55 | $this->removeAllHandlers($logger); |
| 56 | } |
| 57 | } |
| 58 | /** |
| 59 | * Creates default WPDesk logger. |
| 60 | * |
| 61 | * Requirements: |
| 62 | * - get_option, add/remove_action, add/remove filter and WP_CONTENT_DIR should be available for logger. |
| 63 | * |
| 64 | * Assumptions: |
| 65 | * - logger is actively working when 'wpdesk_helper_options' has 'debug_log' set to '1'; |
| 66 | * - fatal errors, exception and standard errors are recorded but in a transparent way; |
| 67 | * - WooCommerce logger is captured and returns this logger; That is true of default logger is instantiated. |
| 68 | * - logs are still correctly written to WooCommerce subsystem in a transparent way; |
| 69 | * - all recorded errors are written to WPDesk file. |
| 70 | * |
| 71 | * @param string $name Name of the logger |
| 72 | * @return Logger |
| 73 | */ |
| 74 | public function createWPDeskLogger($name = self::DEFAULT_LOGGER_CHANNEL_NAME) |
| 75 | { |
| 76 | if (!self::$shouldLoggerBeActivated) { |
| 77 | return new Logger($name); |
| 78 | } |
| 79 | if (Registry::hasLogger($name)) { |
| 80 | return Registry::getInstance($name); |
| 81 | } |
| 82 | $logger = $this->createLogger($name); |
| 83 | if (self::isWPLogPermitted()) { |
| 84 | $this->appendMainLog($logger); |
| 85 | } |
| 86 | if ($name !== self::DEFAULT_LOGGER_CHANNEL_NAME) { |
| 87 | $this->appendFileLog($logger, $this->getFileName($name)); |
| 88 | } else { |
| 89 | $this->captureWooCommerce($logger); |
| 90 | } |
| 91 | return $logger; |
| 92 | } |
| 93 | /** |
| 94 | * Is capturing the php log is permitted. |
| 95 | * |
| 96 | * @return bool |
| 97 | */ |
| 98 | public static function isWPLogPermitted() |
| 99 | { |
| 100 | return apply_filters('wpdesk_is_wp_log_capture_permitted', \true); |
| 101 | } |
| 102 | /** |
| 103 | * @param $logger |
| 104 | */ |
| 105 | private function appendMainLog($logger) |
| 106 | { |
| 107 | $wpCapture = $this->captureWPLog(); |
| 108 | if (is_writable($wpCapture->get_log_file())) { |
| 109 | $this->appendFileLog($logger, $wpCapture->get_log_file()); |
| 110 | } |
| 111 | } |
| 112 | /** |
| 113 | * @param Logger $logger |
| 114 | */ |
| 115 | private function appendFileLog($logger, $filename) |
| 116 | { |
| 117 | try { |
| 118 | $this->pushFileHandle($filename, $logger); |
| 119 | } catch (InvalidArgumentException $e) { |
| 120 | $logger->emergency('Main log file could not be created - invalid filename.'); |
| 121 | } catch (Exception $e) { |
| 122 | $logger->emergency('Main log file could not be written.'); |
| 123 | } |
| 124 | } |
| 125 | /** |
| 126 | * @return WPCapture |
| 127 | */ |
| 128 | private function captureWPLog() |
| 129 | { |
| 130 | static $wpCapture; |
| 131 | if (!$wpCapture) { |
| 132 | $wpCapture = new WPCapture(basename($this->getFileName())); |
| 133 | $wpCapture->init_debug_log_file(); |
| 134 | } |
| 135 | return $wpCapture; |
| 136 | } |
| 137 | /** |
| 138 | * Capture WooCommerce and add handle |
| 139 | * |
| 140 | * @param Logger $logger |
| 141 | */ |
| 142 | private function captureWooCommerce(Logger $logger) |
| 143 | { |
| 144 | if (!defined('FSVendor\WC_LOG_THRESHOLD')) { |
| 145 | define('FSVendor\WC_LOG_THRESHOLD', self::LEVEL_WC); |
| 146 | } |
| 147 | $wcIntegration = new WooCommerceCapture($logger); |
| 148 | $wcIntegration->captureWcLogger(); |
| 149 | } |
| 150 | /** |
| 151 | * Add WPDesk log file handle |
| 152 | * |
| 153 | * @param Logger $logger |
| 154 | * @param string $filename Name of file with path |
| 155 | * |
| 156 | * @throws Exception If a missing directory is not buildable |
| 157 | * @throws InvalidArgumentException If stream is not a resource or string |
| 158 | */ |
| 159 | private function pushFileHandle($filename, Logger $logger) |
| 160 | { |
| 161 | $logger->pushHandler(new StreamHandler($filename, self::LEVEL_WPDESK_FILE)); |
| 162 | } |
| 163 | /** |
| 164 | * Get filename old way |
| 165 | * |
| 166 | * @deprecated not sure if can remove |
| 167 | */ |
| 168 | public function getWPDeskFileName() |
| 169 | { |
| 170 | return $this->getFileName(); |
| 171 | } |
| 172 | /** |
| 173 | * Returns WPDesk filename. |
| 174 | * |
| 175 | * @param string $name Name of the logger |
| 176 | * |
| 177 | * @return string |
| 178 | */ |
| 179 | public function getFileName($name = self::DEFAULT_LOGGER_CHANNEL_NAME) |
| 180 | { |
| 181 | $upload_dir = wp_upload_dir(); |
| 182 | return trailingslashit(untrailingslashit($upload_dir['basedir'])) . WPCapture::LOG_DIR . \DIRECTORY_SEPARATOR . $name . '_debug.log'; |
| 183 | } |
| 184 | /** |
| 185 | * Removes all handlers from logger |
| 186 | * |
| 187 | * @param Logger $logger |
| 188 | * |
| 189 | * @return void |
| 190 | */ |
| 191 | private function removeAllHandlers(Logger $logger) |
| 192 | { |
| 193 | try { |
| 194 | while (\true) { |
| 195 | $logger->popHandler(); |
| 196 | } |
| 197 | } catch (LogicException $e) { |
| 198 | $logger->pushHandler(new NullHandler()); |
| 199 | } |
| 200 | } |
| 201 | /** |
| 202 | * is WPDesk file log is working(writable, exists, connected). |
| 203 | * @param string $name Name of the logger |
| 204 | * |
| 205 | * @return bool |
| 206 | */ |
| 207 | public function isLogWorking($name = self::DEFAULT_LOGGER_CHANNEL_NAME) |
| 208 | { |
| 209 | return Registry::hasLogger($name); |
| 210 | } |
| 211 | } |
| 212 |