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
LoggerFacade.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Logger; |
| 4 | |
| 5 | use FSVendor\Monolog\Logger; |
| 6 | use FSVendor\Psr\Log\LogLevel; |
| 7 | use WP_Error; |
| 8 | use Exception; |
| 9 | /** |
| 10 | * Facilitates creation of logger with default WPDesk settings |
| 11 | * |
| 12 | * @deprecated Only for backward compatibility. Please use injected Logger compatible with PSR |
| 13 | * |
| 14 | * @package WPDesk\Logger |
| 15 | */ |
| 16 | class LoggerFacade |
| 17 | { |
| 18 | const BACKTRACE_FILENAME_KEY = 'file'; |
| 19 | /** @var WPDeskLoggerFactory */ |
| 20 | private static $factory; |
| 21 | /** |
| 22 | * Get logger by name. If not exists create one. |
| 23 | * |
| 24 | * @param string $name Name of the logger |
| 25 | * @return Logger |
| 26 | */ |
| 27 | public static function getLogger($name = WPDeskLoggerFactory::DEFAULT_LOGGER_CHANNEL_NAME) |
| 28 | { |
| 29 | if (self::$factory === null) { |
| 30 | self::$factory = new WPDeskLoggerFactory(); |
| 31 | } |
| 32 | return self::$factory->createWPDeskLogger($name); |
| 33 | } |
| 34 | /** |
| 35 | * Snake case alias for getLogger |
| 36 | * |
| 37 | * @param string $name |
| 38 | * |
| 39 | * @return Logger |
| 40 | */ |
| 41 | public static function get_logger($name = WPDeskLoggerFactory::DEFAULT_LOGGER_CHANNEL_NAME) |
| 42 | { |
| 43 | return self::getLogger($name); |
| 44 | } |
| 45 | /** |
| 46 | * If set, logs are disabled |
| 47 | * |
| 48 | * @param string $name Name of the logger |
| 49 | */ |
| 50 | public static function set_disable_log($name = WPDeskLoggerFactory::DEFAULT_LOGGER_CHANNEL_NAME) |
| 51 | { |
| 52 | self::$factory->disableLog($name); |
| 53 | } |
| 54 | /** |
| 55 | * Log this exception into WPDesk logger |
| 56 | * |
| 57 | * @param WP_Error $e Error to log. |
| 58 | * @param array $backtrace Backtrace information with snapshot of error env. |
| 59 | * @param array $context Context to log |
| 60 | * @param string $level Level of error. |
| 61 | * |
| 62 | * @see http://php.net/manual/en/function.debug-backtrace.php |
| 63 | */ |
| 64 | public static function log_wp_error(WP_Error $e, array $backtrace, array $context = array(), $level = LogLevel::ERROR) |
| 65 | { |
| 66 | $message = 'Error: ' . get_class($e) . ' Code: ' . $e->get_error_code() . ' Message: ' . $e->get_error_message(); |
| 67 | self::log_message_backtrace($message, $backtrace, $context, $level); |
| 68 | } |
| 69 | /** |
| 70 | * Log this exception into WPDesk logger |
| 71 | * |
| 72 | * @param Exception $e Exception to log. |
| 73 | * @param array $context Context to log |
| 74 | * @param string $level Level of error. |
| 75 | */ |
| 76 | public static function log_exception(Exception $e, array $context = array(), $level = LogLevel::ERROR) |
| 77 | { |
| 78 | $message = 'Exception: ' . get_class($e) . ' Code: ' . $e->getCode() . ' Message: ' . $e->getMessage() . ' Stack: ' . $e->getTraceAsString(); |
| 79 | self::log_message($message, array_merge($context, ['exception' => $e]), $e->getFile(), $level); |
| 80 | } |
| 81 | /** |
| 82 | * Log message into WPDesk logger |
| 83 | * |
| 84 | * @param string $message Message to log. |
| 85 | * @param array $context Context to log |
| 86 | * @param string $source Source of the message - can be file name, class name or whatever. |
| 87 | * @param string $level Level of error. |
| 88 | */ |
| 89 | public static function log_message($message, array $context = array(), $source = null, $level = LogLevel::DEBUG) |
| 90 | { |
| 91 | $logger = self::getLogger(); |
| 92 | if ($source !== null) { |
| 93 | $context = array_merge($context, ['source' => $source]); |
| 94 | } |
| 95 | $logger->log($level, $message, $context); |
| 96 | } |
| 97 | /** |
| 98 | * Log message into WPDesk logger |
| 99 | * |
| 100 | * @param string $message Message to log. |
| 101 | * @param array $backtrace Backtrace information with snapshot of error env. |
| 102 | * @param array $context Context to log |
| 103 | * @param string $level Level of error. |
| 104 | */ |
| 105 | public static function log_message_backtrace($message, array $backtrace, array $context = array(), $level = LogLevel::DEBUG) |
| 106 | { |
| 107 | $message .= ' Backtrace: ' . json_encode($backtrace); |
| 108 | $source = null; |
| 109 | if (isset($backtrace[self::BACKTRACE_FILENAME_KEY])) { |
| 110 | $source = $backtrace[self::BACKTRACE_FILENAME_KEY]; |
| 111 | } |
| 112 | self::log_message($message, $context, $source, $level); |
| 113 | } |
| 114 | } |
| 115 |