@@ -13,15 +13,15 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | class LogLaddy implements LoggerInterface |
| 15 | 15 | { |
| 16 | - use \Psr\Log\LoggerTrait; // PSR implementation |
|
| 17 | - use \HexMakina\Debugger\Debugger; // Debugger |
|
| 16 | + use \Psr\Log\LoggerTrait; // PSR implementation |
|
| 17 | + use \HexMakina\Debugger\Debugger; // Debugger |
|
| 18 | 18 | |
| 19 | - const REPORTING_USER = 'user_messages'; |
|
| 20 | - const INTERNAL_ERROR = 'error'; |
|
| 21 | - const USER_EXCEPTION = 'exception'; |
|
| 22 | - const LOG_LEVEL_SUCCESS = 'ok'; |
|
| 19 | + const REPORTING_USER='user_messages'; |
|
| 20 | + const INTERNAL_ERROR='error'; |
|
| 21 | + const USER_EXCEPTION='exception'; |
|
| 22 | + const LOG_LEVEL_SUCCESS='ok'; |
|
| 23 | 23 | |
| 24 | - private $has_halting_messages = false; |
|
| 24 | + private $has_halting_messages=false; |
|
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Everything went fine, which is always nice. |
@@ -32,15 +32,15 @@ discard block |
||
| 32 | 32 | * @return void |
| 33 | 33 | */ |
| 34 | 34 | |
| 35 | - public function nice($message, array $context = array()) |
|
| 35 | + public function nice($message, array $context=array()) |
|
| 36 | 36 | { |
| 37 | 37 | $this->log(LogLevel::NICE, $message, $context); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 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) |
|
| 41 | + public static function error_handler($level, $message, $file='', $line=0) |
|
| 42 | 42 | { |
| 43 | - $loglevel = self::map_error_level_to_log_level($level); |
|
| 43 | + $loglevel=self::map_error_level_to_log_level($level); |
|
| 44 | 44 | |
| 45 | 45 | (new LogLaddy())->$loglevel($message, ['file' => $file, 'line' => $line, 'trace' => debug_backtrace()]); |
| 46 | 46 | } |
@@ -48,18 +48,18 @@ discard block |
||
| 48 | 48 | // ----------------------------------------------------------- Static handlers for throwables, use set_exception_handler('\HexMakina\kadro\Logger\LogLaddy::exception_handler'); |
| 49 | 49 | public static function exception_handler(\Throwable $throwable) |
| 50 | 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') |
|
| 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 | 61 | $lad->alert(self::INTERNAL_ERROR, $context); |
| 62 | - elseif(is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') |
|
| 62 | + elseif (is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') |
|
| 63 | 63 | $lad->notice(self::USER_EXCEPTION, $context); |
| 64 | 64 | else |
| 65 | 65 | $lad->critical('Caught a Throwable that is not an Error or an Exception. This breaks everything.', $context); |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | public function system_halted($level) |
| 69 | 69 | { |
| 70 | - switch($level) |
|
| 70 | + switch ($level) |
|
| 71 | 71 | { |
| 72 | 72 | case LogLevel::ERROR: |
| 73 | 73 | case LogLevel::CRITICAL: |
@@ -80,24 +80,24 @@ discard block |
||
| 80 | 80 | |
| 81 | 81 | // ----------------------------------------------------------- Implementation of LoggerInterface::log(), all other methods are in LoggerTrait |
| 82 | 82 | |
| 83 | - public function log($level, $message, array $context = []) |
|
| 83 | + public function log($level, $message, array $context=[]) |
|
| 84 | 84 | { |
| 85 | - $display_error = null; |
|
| 85 | + $display_error=null; |
|
| 86 | 86 | |
| 87 | 87 | // --- Handles Throwables (exception_handler()) |
| 88 | - if($message==self::INTERNAL_ERROR || $message== self::USER_EXCEPTION) |
|
| 88 | + if ($message == self::INTERNAL_ERROR || $message == self::USER_EXCEPTION) |
|
| 89 | 89 | { |
| 90 | - $this->has_halting_messages = true; |
|
| 91 | - $display_error = self::format_throwable_message($context['class'], $context['code'], $context['file'], $context['line'], $context['text']); |
|
| 90 | + $this->has_halting_messages=true; |
|
| 91 | + $display_error=self::format_throwable_message($context['class'], $context['code'], $context['file'], $context['line'], $context['text']); |
|
| 92 | 92 | error_log($display_error); |
| 93 | - $display_error.= self::format_trace($context['trace'], false); |
|
| 93 | + $display_error.=self::format_trace($context['trace'], false); |
|
| 94 | 94 | self::HTTP_500($display_error); |
| 95 | 95 | } |
| 96 | - elseif($this->system_halted($level)) // analyses error level |
|
| 96 | + elseif ($this->system_halted($level)) // analyses error level |
|
| 97 | 97 | { |
| 98 | - $display_error = sprintf(PHP_EOL.'%s in file %s:%d'.PHP_EOL.'%s', $level, self::format_file($context['file']), $context['line'], $message); |
|
| 98 | + $display_error=sprintf(PHP_EOL.'%s in file %s:%d'.PHP_EOL.'%s', $level, self::format_file($context['file']), $context['line'], $message); |
|
| 99 | 99 | error_log($display_error); |
| 100 | - $display_error.= self::format_trace($context['trace'], true); |
|
| 100 | + $display_error.=self::format_trace($context['trace'], true); |
|
| 101 | 101 | self::HTTP_500($display_error); |
| 102 | 102 | } |
| 103 | 103 | else |
@@ -122,15 +122,15 @@ discard block |
||
| 122 | 122 | // ----------------------------------------------------------- User messages |
| 123 | 123 | |
| 124 | 124 | // ----------------------------------------------------------- User messages:add one |
| 125 | - public function report_to_user($level, $message, $context = []) |
|
| 125 | + public function report_to_user($level, $message, $context=[]) |
|
| 126 | 126 | { |
| 127 | - if(!isset($_SESSION[self::REPORTING_USER])) |
|
| 128 | - $_SESSION[self::REPORTING_USER] = []; |
|
| 127 | + if (!isset($_SESSION[self::REPORTING_USER])) |
|
| 128 | + $_SESSION[self::REPORTING_USER]=[]; |
|
| 129 | 129 | |
| 130 | - if(!isset($_SESSION[self::REPORTING_USER][$level])) |
|
| 131 | - $_SESSION[self::REPORTING_USER][$level] = []; |
|
| 130 | + if (!isset($_SESSION[self::REPORTING_USER][$level])) |
|
| 131 | + $_SESSION[self::REPORTING_USER][$level]=[]; |
|
| 132 | 132 | |
| 133 | - $_SESSION[self::REPORTING_USER][$level][] = [$message, $context]; |
|
| 133 | + $_SESSION[self::REPORTING_USER][$level][]=[$message, $context]; |
|
| 134 | 134 | } |
| 135 | 135 | |
| 136 | 136 | // ----------------------------------------------------------- User messages:get all |
@@ -173,7 +173,7 @@ discard block |
||
| 173 | 173 | $m[E_STRICT]=$m[E_DEPRECATED]=$m[E_USER_DEPRECATED]=$m[E_ALL]=LogLevel::DEBUG; |
| 174 | 174 | $m[2048]=$m[8192]=$m[16384]=$m[32767]=LogLevel::DEBUG; |
| 175 | 175 | |
| 176 | - if(isset($m[$level])) |
|
| 176 | + if (isset($m[$level])) |
|
| 177 | 177 | return $m[$level]; |
| 178 | 178 | |
| 179 | 179 | throw new \Exception(__FUNCTION__."($level): $level is unknown"); |
@@ -57,12 +57,13 @@ discard block |
||
| 57 | 57 | $context['trace'] = $throwable->getTrace(); |
| 58 | 58 | |
| 59 | 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); |
|
| 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 | + } |
|
| 66 | 67 | } |
| 67 | 68 | |
| 68 | 69 | public function system_halted($level) |
@@ -92,15 +93,15 @@ discard block |
||
| 92 | 93 | error_log($display_error); |
| 93 | 94 | $display_error.= self::format_trace($context['trace'], false); |
| 94 | 95 | self::HTTP_500($display_error); |
| 95 | - } |
|
| 96 | - elseif($this->system_halted($level)) // analyses error level |
|
| 96 | + } elseif($this->system_halted($level)) { |
|
| 97 | + // analyses error level |
|
| 97 | 98 | { |
| 98 | 99 | $display_error = sprintf(PHP_EOL.'%s in file %s:%d'.PHP_EOL.'%s', $level, self::format_file($context['file']), $context['line'], $message); |
| 100 | + } |
|
| 99 | 101 | error_log($display_error); |
| 100 | 102 | $display_error.= self::format_trace($context['trace'], true); |
| 101 | 103 | self::HTTP_500($display_error); |
| 102 | - } |
|
| 103 | - else |
|
| 104 | + } else |
|
| 104 | 105 | {// --- Handles user messages, through SESSION storage |
| 105 | 106 | $this->report_to_user($level, $message, $context); |
| 106 | 107 | } |
@@ -124,11 +125,13 @@ discard block |
||
| 124 | 125 | // ----------------------------------------------------------- User messages:add one |
| 125 | 126 | public function report_to_user($level, $message, $context = []) |
| 126 | 127 | { |
| 127 | - if(!isset($_SESSION[self::REPORTING_USER])) |
|
| 128 | - $_SESSION[self::REPORTING_USER] = []; |
|
| 128 | + if(!isset($_SESSION[self::REPORTING_USER])) { |
|
| 129 | + $_SESSION[self::REPORTING_USER] = []; |
|
| 130 | + } |
|
| 129 | 131 | |
| 130 | - if(!isset($_SESSION[self::REPORTING_USER][$level])) |
|
| 131 | - $_SESSION[self::REPORTING_USER][$level] = []; |
|
| 132 | + if(!isset($_SESSION[self::REPORTING_USER][$level])) { |
|
| 133 | + $_SESSION[self::REPORTING_USER][$level] = []; |
|
| 134 | + } |
|
| 132 | 135 | |
| 133 | 136 | $_SESSION[self::REPORTING_USER][$level][] = [$message, $context]; |
| 134 | 137 | } |
@@ -173,8 +176,9 @@ discard block |
||
| 173 | 176 | $m[E_STRICT]=$m[E_DEPRECATED]=$m[E_USER_DEPRECATED]=$m[E_ALL]=LogLevel::DEBUG; |
| 174 | 177 | $m[2048]=$m[8192]=$m[16384]=$m[32767]=LogLevel::DEBUG; |
| 175 | 178 | |
| 176 | - if(isset($m[$level])) |
|
| 177 | - return $m[$level]; |
|
| 179 | + if(isset($m[$level])) { |
|
| 180 | + return $m[$level]; |
|
| 181 | + } |
|
| 178 | 182 | |
| 179 | 183 | throw new \Exception(__FUNCTION__."($level): $level is unknown"); |
| 180 | 184 | } |