@@ -7,5 +7,5 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class LogLevel extends \Psr\Log\LogLevel |
| 9 | 9 | { |
| 10 | - const NICE = 'success'; |
|
| 10 | + const NICE='success'; |
|
| 11 | 11 | } |
@@ -159,7 +159,7 @@ |
||
| 159 | 159 | * const NOTICE = 'notice'; // Normal but significant events. |
| 160 | 160 | * const INFO = 'info'; // Interesting events. User logs in, SQL logs. |
| 161 | 161 | * const DEBUG = 'debug'; // Detailed debug information. |
| 162 | - */ |
|
| 162 | + */ |
|
| 163 | 163 | private static function map_error_level_to_log_level($level) : string |
| 164 | 164 | { |
| 165 | 165 | // http://php.net/manual/en/errorfunc.constants.php |
@@ -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,16 +48,16 @@ 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) |
| 50 | 50 | { |
| 51 | - $context['text'] = $throwable->getMessage(); |
|
| 52 | - $context['file'] = $throwable->getFile(); |
|
| 53 | - $context['line'] = $throwable->getLine(); |
|
| 54 | - $context['code'] = $throwable->getCode(); |
|
| 55 | - $context['class'] = get_class($throwable); |
|
| 56 | - $context['trace'] = $throwable->getTrace(); |
|
| 57 | - |
|
| 58 | - if(is_subclass_of($throwable, 'Error') || get_class($throwable) === 'Error') |
|
| 51 | + $context['text']=$throwable->getMessage(); |
|
| 52 | + $context['file']=$throwable->getFile(); |
|
| 53 | + $context['line']=$throwable->getLine(); |
|
| 54 | + $context['code']=$throwable->getCode(); |
|
| 55 | + $context['class']=get_class($throwable); |
|
| 56 | + $context['trace']=$throwable->getTrace(); |
|
| 57 | + |
|
| 58 | + if (is_subclass_of($throwable, 'Error') || get_class($throwable) === 'Error') |
|
| 59 | 59 | (new LogLaddy())->alert(self::INTERNAL_ERROR, $context); |
| 60 | - elseif(is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') |
|
| 60 | + elseif (is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') |
|
| 61 | 61 | (new LogLaddy())->notice(self::USER_EXCEPTION, $context); |
| 62 | 62 | else |
| 63 | 63 | { |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | |
| 69 | 69 | public function system_halted($level) |
| 70 | 70 | { |
| 71 | - switch($level) |
|
| 71 | + switch ($level) |
|
| 72 | 72 | { |
| 73 | 73 | case LogLevel::ERROR: |
| 74 | 74 | case LogLevel::CRITICAL: |
@@ -81,24 +81,24 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | // ----------------------------------------------------------- Implementation of LoggerInterface::log(), all other methods are in LoggerTrait |
| 83 | 83 | |
| 84 | - public function log($level, $message, array $context = []) |
|
| 84 | + public function log($level, $message, array $context=[]) |
|
| 85 | 85 | { |
| 86 | - $display_error = null; |
|
| 86 | + $display_error=null; |
|
| 87 | 87 | |
| 88 | 88 | // --- Handles Throwables (exception_handler()) |
| 89 | - if($message==self::INTERNAL_ERROR || $message== self::USER_EXCEPTION) |
|
| 89 | + if ($message == self::INTERNAL_ERROR || $message == self::USER_EXCEPTION) |
|
| 90 | 90 | { |
| 91 | - $this->has_halting_messages = true; |
|
| 92 | - $display_error = self::format_throwable_message($context['class'], $context['code'], $context['file'], $context['line'], $context['text']); |
|
| 91 | + $this->has_halting_messages=true; |
|
| 92 | + $display_error=self::format_throwable_message($context['class'], $context['code'], $context['file'], $context['line'], $context['text']); |
|
| 93 | 93 | error_log($display_error); |
| 94 | - $display_error.= self::format_trace($context['trace'], false); |
|
| 94 | + $display_error.=self::format_trace($context['trace'], false); |
|
| 95 | 95 | self::HTTP_500($display_error); |
| 96 | 96 | } |
| 97 | - elseif($this->system_halted($level)) // analyses error level |
|
| 97 | + elseif ($this->system_halted($level)) // analyses error level |
|
| 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); |
|
| 99 | + $display_error=sprintf(PHP_EOL.'%s in file %s:%d'.PHP_EOL.'%s', $level, self::format_file($context['file']), $context['line'], $message); |
|
| 100 | 100 | error_log($display_error); |
| 101 | - $display_error.= self::format_trace($context['trace'], true); |
|
| 101 | + $display_error.=self::format_trace($context['trace'], true); |
|
| 102 | 102 | self::HTTP_500($display_error); |
| 103 | 103 | } |
| 104 | 104 | else |
@@ -126,15 +126,15 @@ discard block |
||
| 126 | 126 | // ----------------------------------------------------------- User messages |
| 127 | 127 | |
| 128 | 128 | // ----------------------------------------------------------- User messages:add one |
| 129 | - public function report_to_user($level, $message, $context = []) |
|
| 129 | + public function report_to_user($level, $message, $context=[]) |
|
| 130 | 130 | { |
| 131 | - if(!isset($_SESSION[self::REPORTING_USER])) |
|
| 132 | - $_SESSION[self::REPORTING_USER] = []; |
|
| 131 | + if (!isset($_SESSION[self::REPORTING_USER])) |
|
| 132 | + $_SESSION[self::REPORTING_USER]=[]; |
|
| 133 | 133 | |
| 134 | - if(!isset($_SESSION[self::REPORTING_USER][$level])) |
|
| 135 | - $_SESSION[self::REPORTING_USER][$level] = []; |
|
| 134 | + if (!isset($_SESSION[self::REPORTING_USER][$level])) |
|
| 135 | + $_SESSION[self::REPORTING_USER][$level]=[]; |
|
| 136 | 136 | |
| 137 | - $_SESSION[self::REPORTING_USER][$level][] = [$message, $context]; |
|
| 137 | + $_SESSION[self::REPORTING_USER][$level][]=[$message, $context]; |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | // ----------------------------------------------------------- User messages:get all |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | $m[E_STRICT]=$m[E_DEPRECATED]=$m[E_USER_DEPRECATED]=$m[E_ALL]=LogLevel::DEBUG; |
| 178 | 178 | $m[2048]=$m[8192]=$m[16384]=$m[32767]=LogLevel::DEBUG; |
| 179 | 179 | |
| 180 | - if(isset($m[$level])) |
|
| 180 | + if (isset($m[$level])) |
|
| 181 | 181 | return $m[$level]; |
| 182 | 182 | |
| 183 | 183 | throw new \Exception(__FUNCTION__."($level): $level is unknown"); |
@@ -55,11 +55,11 @@ discard block |
||
| 55 | 55 | $context['class'] = get_class($throwable); |
| 56 | 56 | $context['trace'] = $throwable->getTrace(); |
| 57 | 57 | |
| 58 | - if(is_subclass_of($throwable, 'Error') || get_class($throwable) === 'Error') |
|
| 59 | - (new LogLaddy())->alert(self::INTERNAL_ERROR, $context); |
|
| 60 | - elseif(is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') |
|
| 61 | - (new LogLaddy())->notice(self::USER_EXCEPTION, $context); |
|
| 62 | - else |
|
| 58 | + if(is_subclass_of($throwable, 'Error') || get_class($throwable) === 'Error') { |
|
| 59 | + (new LogLaddy())->alert(self::INTERNAL_ERROR, $context); |
|
| 60 | + } elseif(is_subclass_of($throwable, 'Exception') || get_class($throwable) === 'Exception') { |
|
| 61 | + (new LogLaddy())->notice(self::USER_EXCEPTION, $context); |
|
| 62 | + } else |
|
| 63 | 63 | { |
| 64 | 64 | die('This Throwable is not an Error or an Exception. This is unfortunate.'); |
| 65 | 65 | } |
@@ -93,15 +93,15 @@ discard block |
||
| 93 | 93 | error_log($display_error); |
| 94 | 94 | $display_error.= self::format_trace($context['trace'], false); |
| 95 | 95 | self::HTTP_500($display_error); |
| 96 | - } |
|
| 97 | - elseif($this->system_halted($level)) // analyses error level |
|
| 96 | + } elseif($this->system_halted($level)) { |
|
| 97 | + // analyses error level |
|
| 98 | 98 | { |
| 99 | 99 | $display_error = sprintf(PHP_EOL.'%s in file %s:%d'.PHP_EOL.'%s', $level, self::format_file($context['file']), $context['line'], $message); |
| 100 | + } |
|
| 100 | 101 | error_log($display_error); |
| 101 | 102 | $display_error.= self::format_trace($context['trace'], true); |
| 102 | 103 | self::HTTP_500($display_error); |
| 103 | - } |
|
| 104 | - else |
|
| 104 | + } else |
|
| 105 | 105 | {// --- Handles user messages, through SESSION storage |
| 106 | 106 | $this->report_to_user($level, $message, $context); |
| 107 | 107 | } |
@@ -128,11 +128,13 @@ discard block |
||
| 128 | 128 | // ----------------------------------------------------------- User messages:add one |
| 129 | 129 | public function report_to_user($level, $message, $context = []) |
| 130 | 130 | { |
| 131 | - if(!isset($_SESSION[self::REPORTING_USER])) |
|
| 132 | - $_SESSION[self::REPORTING_USER] = []; |
|
| 131 | + if(!isset($_SESSION[self::REPORTING_USER])) { |
|
| 132 | + $_SESSION[self::REPORTING_USER] = []; |
|
| 133 | + } |
|
| 133 | 134 | |
| 134 | - if(!isset($_SESSION[self::REPORTING_USER][$level])) |
|
| 135 | - $_SESSION[self::REPORTING_USER][$level] = []; |
|
| 135 | + if(!isset($_SESSION[self::REPORTING_USER][$level])) { |
|
| 136 | + $_SESSION[self::REPORTING_USER][$level] = []; |
|
| 137 | + } |
|
| 136 | 138 | |
| 137 | 139 | $_SESSION[self::REPORTING_USER][$level][] = [$message, $context]; |
| 138 | 140 | } |
@@ -177,8 +179,9 @@ discard block |
||
| 177 | 179 | $m[E_STRICT]=$m[E_DEPRECATED]=$m[E_USER_DEPRECATED]=$m[E_ALL]=LogLevel::DEBUG; |
| 178 | 180 | $m[2048]=$m[8192]=$m[16384]=$m[32767]=LogLevel::DEBUG; |
| 179 | 181 | |
| 180 | - if(isset($m[$level])) |
|
| 181 | - return $m[$level]; |
|
| 182 | + if(isset($m[$level])) { |
|
| 183 | + return $m[$level]; |
|
| 184 | + } |
|
| 182 | 185 | |
| 183 | 186 | throw new \Exception(__FUNCTION__."($level): $level is unknown"); |
| 184 | 187 | } |
@@ -15,9 +15,9 @@ |
||
| 15 | 15 | * |
| 16 | 16 | * @return void |
| 17 | 17 | */ |
| 18 | - public function nice($message, array $context = array()); |
|
| 18 | + public function nice($message, array $context=array()); |
|
| 19 | 19 | |
| 20 | - public function report_to_user($level, $message, $context = []); |
|
| 20 | + public function report_to_user($level, $message, $context=[]); |
|
| 21 | 21 | public function get_user_report(); |
| 22 | 22 | public function clean_user_report(); |
| 23 | 23 | |