HexMakina /
LogLaddy
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * LogLaddy |
||
| 4 | * |
||
| 5 | * I carry a log – yes. Is it funny to you? It is not to me. |
||
| 6 | * Behind all things are reasons. Reasons can even explain the absurd. |
||
| 7 | * |
||
| 8 | * LogLaddy manages error reporting |
||
| 9 | * PSR-3 Compliant, with a NICE bonus |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace HexMakina\LogLaddy; |
||
| 13 | |||
| 14 | class LogLaddy implements LoggerInterface |
||
| 15 | { |
||
| 16 | use \Psr\Log\LoggerTrait; // PSR implementation |
||
| 17 | use \HexMakina\Debugger\Debugger; // Debugger |
||
| 18 | |||
| 19 | const REPORTING_USER = 'user_messages'; |
||
| 20 | const INTERNAL_ERROR = 'error'; |
||
| 21 | const USER_EXCEPTION = 'exception'; |
||
| 22 | const LOG_LEVEL_SUCCESS = 'ok'; |
||
| 23 | |||
| 24 | private $has_halting_messages = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Everything went fine, which is always nice. |
||
| 28 | * LogLaddy is a bit more optimistic than PSRLog |
||
| 29 | * @param string $message |
||
| 30 | * @param array $context |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | |||
| 35 | public function nice($message, array $context = array()) |
||
| 36 | { |
||
| 37 | $this->log(LogLevel::NICE, $message, $context); |
||
| 38 | } |
||
| 39 | |||
| 40 | // -- Static handlers for error, use set_error_handler('\HexMakina\kadro\Logger\LogLaddy::error_handler') |
||
| 41 | public static function error_handler($level, $message, $file = '', $line = 0) |
||
| 42 | { |
||
| 43 | $loglevel = self::map_error_level_to_log_level($level); |
||
| 44 | |||
| 45 | (new LogLaddy())->$loglevel($message, ['file' => $file, 'line' => $line, 'trace' => debug_backtrace()]); |
||
| 46 | } |
||
| 47 | |||
| 48 | // -- Static handlers for throwables, use set_exception_handler('\HexMakina\kadro\Logger\LogLaddy::exception_handler'); |
||
| 49 | public static function exception_handler(\Throwable $throwable) |
||
| 50 | { |
||
| 51 | $context = []; |
||
| 52 | $context['text'] = $throwable->getMessage(); |
||
| 53 | $context['file'] = $throwable->getFile(); |
||
| 54 | $context['line'] = $throwable->getLine(); |
||
| 55 | $context['code'] = $throwable->getCode(); |
||
| 56 | $context['class'] = get_class($throwable); |
||
| 57 | $context['trace'] = $throwable->getTrace(); |
||
| 58 | |||
| 59 | $lad = new LogLaddy(); |
||
| 60 | if(is_subclass_of($throwable, 'Error') || get_class($throwable) === 'Error') |
||
| 61 | $lad->alert(self::INTERNAL_ERROR, $context); |
||
| 62 | elseif(is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') |
||
| 63 | $lad->notice(self::USER_EXCEPTION, $context); |
||
| 64 | else |
||
| 65 | $lad->critical('Caught a Throwable that is not an Error or an Exception. This breaks everything.', $context); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function system_halted($level) |
||
| 69 | { |
||
| 70 | switch($level) |
||
| 71 | { |
||
| 72 | case LogLevel::ERROR: |
||
| 73 | case LogLevel::CRITICAL: |
||
| 74 | case LogLevel::ALERT: |
||
| 75 | case LogLevel::EMERGENCY: |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 81 | // -- Implementation of LoggerInterface::log(), all other methods are in LoggerTrait |
||
| 82 | |||
| 83 | public function log($level, $message, array $context = []) |
||
| 84 | { |
||
| 85 | $display_error = null; |
||
| 86 | |||
| 87 | // --- Handles Throwables (exception_handler()) |
||
| 88 | if($message==self::INTERNAL_ERROR || $message== self::USER_EXCEPTION) |
||
| 89 | { |
||
| 90 | $this->has_halting_messages = true; |
||
| 91 | $display_error = self::format_throwable_message($context['class'], $context['code'], $context['file'], $context['line'], $context['text']); |
||
| 92 | error_log($display_error); |
||
| 93 | $display_error.= self::format_trace($context['trace'], false); |
||
| 94 | self::HTTP_500($display_error); |
||
| 95 | } |
||
| 96 | elseif($this->system_halted($level)) // analyses error level |
||
| 97 | { |
||
| 98 | $display_error = sprintf(PHP_EOL.'%s in file %s:%d'.PHP_EOL.'%s', $level, self::format_file($context['file']), $context['line'], $message); |
||
| 99 | error_log($display_error); |
||
| 100 | $display_error.= self::format_trace($context['trace'], true); |
||
| 101 | self::HTTP_500($display_error); |
||
| 102 | } |
||
| 103 | else |
||
| 104 | {// --- Handles user messages, through SESSION storage |
||
| 105 | $this->report_to_user($level, $message, $context); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | public static function HTTP_500($display_error) |
||
| 110 | { |
||
| 111 | self::display_errors($display_error); |
||
| 112 | http_response_code(500); |
||
| 113 | die; |
||
|
0 ignored issues
–
show
|
|||
| 114 | } |
||
| 115 | // -- Allows to know if script must be halted after fatal error |
||
| 116 | // TODO NEH.. not good |
||
| 117 | public function has_halting_messages() |
||
| 118 | { |
||
| 119 | return $this->has_halting_messages === true; |
||
| 120 | } |
||
| 121 | |||
| 122 | // -- User messages |
||
| 123 | |||
| 124 | // -- User messages:add one |
||
| 125 | public function report_to_user($level, $message, $context = []) |
||
| 126 | { |
||
| 127 | if(!isset($_SESSION[self::REPORTING_USER])) |
||
| 128 | $_SESSION[self::REPORTING_USER] = []; |
||
| 129 | |||
| 130 | if(!isset($_SESSION[self::REPORTING_USER][$level])) |
||
| 131 | $_SESSION[self::REPORTING_USER][$level] = []; |
||
| 132 | |||
| 133 | $_SESSION[self::REPORTING_USER][$level][] = [$message, $context]; |
||
| 134 | } |
||
| 135 | |||
| 136 | // -- User messages:get all |
||
| 137 | public function get_user_report() |
||
| 138 | { |
||
| 139 | return $_SESSION[self::REPORTING_USER] ?? []; |
||
| 140 | } |
||
| 141 | |||
| 142 | // -- User messages:reset all |
||
| 143 | public function clean_user_report() |
||
| 144 | { |
||
| 145 | unset($_SESSION[self::REPORTING_USER]); |
||
| 146 | } |
||
| 147 | |||
| 148 | // -- Error level mapping from \Psr\Log\LogLevel.php & http://php.net/manual/en/errorfunc.constants.php |
||
| 149 | /** Error level meaning , from \Psr\Log\LogLevel.php |
||
| 150 | * const EMERGENCY = 'emergency'; // System is unusable. |
||
| 151 | * const ALERT = 'alert'; // Action must be taken immediately, Example: Entire website down, database unavailable, etc. |
||
| 152 | * const CRITICAL = 'critical'; // Application component unavailable, unexpected exception. |
||
| 153 | * const ERROR = 'error'; // Run time errors that do not require immediate action |
||
| 154 | * const WARNING = 'warning'; // Exceptional occurrences that are not errors, undesirable things that are not necessarily wrong |
||
| 155 | * const NOTICE = 'notice'; // Normal but significant events. |
||
| 156 | * const INFO = 'info'; // Interesting events. User logs in, SQL logs. |
||
| 157 | * const DEBUG = 'debug'; // Detailed debug information. |
||
| 158 | */ |
||
| 159 | private static function map_error_level_to_log_level($level) : string |
||
| 160 | { |
||
| 161 | // http://php.net/manual/en/errorfunc.constants.php |
||
| 162 | $m=[]; |
||
| 163 | |||
| 164 | $m[E_ERROR]=$m[E_PARSE]=$m[E_CORE_ERROR]=$m[E_COMPILE_ERROR]=$m[E_USER_ERROR]=$m[E_RECOVERABLE_ERROR]=LogLevel::ALERT; |
||
| 165 | $m[1]=$m[4]=$m[16]=$m[64]=$m[256]=$m[4096]=LogLevel::ALERT; |
||
| 166 | |||
| 167 | $m[E_WARNING]=$m[E_CORE_WARNING]=$m[E_COMPILE_WARNING]=$m[E_USER_WARNING]=LogLevel::CRITICAL; |
||
| 168 | $m[2]=$m[32]=$m[128]=$m[512]=LogLevel::CRITICAL; |
||
| 169 | |||
| 170 | $m[E_NOTICE]=$m[E_USER_NOTICE]=LogLevel::ERROR; |
||
| 171 | $m[8]=$m[1024]=LogLevel::ERROR; |
||
| 172 | |||
| 173 | $m[E_STRICT]=$m[E_DEPRECATED]=$m[E_USER_DEPRECATED]=$m[E_ALL]=LogLevel::DEBUG; |
||
| 174 | $m[2048]=$m[8192]=$m[16384]=$m[32767]=LogLevel::DEBUG; |
||
| 175 | |||
| 176 | if(isset($m[$level])) |
||
| 177 | return $m[$level]; |
||
| 178 | |||
| 179 | throw new \Exception(__FUNCTION__."($level): $level is unknown"); |
||
| 180 | } |
||
| 181 | } |
||
| 182 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.