HexMakina /
LogLaddy
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * LogLaddy |
||
| 5 | * |
||
| 6 | * I carry a log – yes. Is it funny to you? It is not to me. |
||
| 7 | * Behind all things are reasons. Reasons can even explain the absurd. |
||
| 8 | * |
||
| 9 | * LogLaddy manages error reporting |
||
| 10 | * PSR-3 Compliant, with a NICE bonus |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace HexMakina\LogLaddy; |
||
| 14 | |||
| 15 | // Debugger |
||
| 16 | use Psr\Log\LogLevel; |
||
| 17 | use HexMakina\Debugger\Debugger; |
||
| 18 | use HexMakina\BlackBox\StateAgentInterface; |
||
| 19 | |||
| 20 | class LogLaddy extends \Psr\Log\AbstractLogger |
||
| 21 | { |
||
| 22 | private $state_agent = null; |
||
| 23 | |||
| 24 | public function __construct(StateAgentInterface $agent) |
||
| 25 | { |
||
| 26 | $this->state_agent = $agent; |
||
| 27 | $this->setHandlers(); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function setHandlers() |
||
| 31 | { |
||
| 32 | set_error_handler([$this, 'errorHandler']); |
||
| 33 | set_exception_handler([$this, 'exceptionHandler']); |
||
| 34 | } |
||
| 35 | |||
| 36 | public function restoreHandlers() |
||
| 37 | { |
||
| 38 | restore_error_handler(); |
||
| 39 | restore_exception_handler(); |
||
| 40 | } |
||
| 41 | |||
| 42 | /* |
||
| 43 | * handler for errors |
||
| 44 | * use set_error_handler('\HexMakina\kadro\Logger\LogLaddy::error_handler') |
||
| 45 | */ |
||
| 46 | public function errorHandler($level, $message, $file = '', $line = 0) |
||
| 47 | { |
||
| 48 | $loglevel = self::mapErrorLevelToLogLevel($level); |
||
| 49 | $this->$loglevel($message, ['file' => $file, 'line' => $line, 'trace' => debug_backtrace()]); |
||
| 50 | } |
||
| 51 | |||
| 52 | /* |
||
| 53 | * static handlers for throwables, |
||
| 54 | * use set_exception_handler('\HexMakina\kadro\Logger\LogLaddy::exception_handler'); |
||
| 55 | */ |
||
| 56 | public function exceptionHandler(\Throwable $throwable) |
||
| 57 | { |
||
| 58 | $this->critical(Debugger::formatThrowable($throwable), $throwable->getTrace()); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function log($level, $message, array $context = []) |
||
| 62 | { |
||
| 63 | switch ($level) { |
||
| 64 | case LogLevel::DEBUG: |
||
| 65 | Debugger::visualDump($context, $message, true); |
||
| 66 | break; |
||
| 67 | |||
| 68 | case LogLevel::INFO: |
||
| 69 | case LogLevel::NOTICE: |
||
| 70 | case LogLevel::WARNING: |
||
| 71 | $this->state_agent->addMessage($level, $message, $context); |
||
| 72 | break; |
||
| 73 | |||
| 74 | case LogLevel::CRITICAL: |
||
| 75 | case LogLevel::ALERT: |
||
| 76 | case LogLevel::EMERGENCY: |
||
| 77 | // if dev, show, else logto file |
||
| 78 | echo Debugger::toHTML($message, $level, $context, true); |
||
|
0 ignored issues
–
show
|
|||
| 79 | http_response_code(500); |
||
| 80 | die; |
||
|
1 ignored issue
–
show
|
|||
| 81 | break; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | |||
| 86 | // -- Error level mapping from \Psr\Log\LogLevel.php & http://php.net/manual/en/errorfunc.constants.php |
||
| 87 | /** Error level meaning , from \Psr\Log\LogLevel.php |
||
| 88 | * const EMERGENCY = 'emergency'; // System is unusable. |
||
| 89 | * const ALERT = 'alert'; // Action must be taken immediately, Example: Entire website down, database unavailable, etc. |
||
| 90 | * const CRITICAL = 'critical'; // Application component unavailable, unexpected exception. |
||
| 91 | * const ERROR = 'error'; // Run time errors that do not require immediate action |
||
| 92 | * const WARNING = 'warning'; // Exceptional occurrences that are not errors, undesirable things that are not necessarily wrong |
||
| 93 | * const NOTICE = 'notice'; // Normal but significant events. |
||
| 94 | * const INFO = 'info'; // Interesting events. User logs in, SQL logs. |
||
| 95 | * const DEBUG = 'debug'; // Detailed debug information. |
||
| 96 | */ |
||
| 97 | private static function mapErrorLevelToLogLevel($level): string |
||
| 98 | { |
||
| 99 | // http://php.net/manual/en/errorfunc.constants.php |
||
| 100 | $m = []; |
||
| 101 | |||
| 102 | $m[E_ERROR] = $m[E_PARSE] = $m[E_CORE_ERROR] = $m[E_COMPILE_ERROR] = $m[E_USER_ERROR] = $m[E_RECOVERABLE_ERROR] = LogLevel::ALERT; |
||
| 103 | $m[1] = $m[4] = $m[16] = $m[64] = $m[256] = $m[4096] = LogLevel::ALERT; |
||
| 104 | |||
| 105 | $m[E_WARNING] = $m[E_CORE_WARNING] = $m[E_COMPILE_WARNING] = $m[E_USER_WARNING] = LogLevel::CRITICAL; |
||
| 106 | $m[2] = $m[32] = $m[128] = $m[512] = LogLevel::CRITICAL; |
||
| 107 | |||
| 108 | $m[E_NOTICE] = $m[E_USER_NOTICE] = LogLevel::ERROR; |
||
| 109 | $m[8] = $m[1024] = LogLevel::ERROR; |
||
| 110 | |||
| 111 | $m[E_STRICT] = $m[E_DEPRECATED] = $m[E_USER_DEPRECATED] = $m[E_ALL] = LogLevel::DEBUG; |
||
| 112 | $m[2048] = $m[8192] = $m[16384] = $m[32767] = LogLevel::DEBUG; |
||
| 113 | |||
| 114 | if (isset($m[$level])) { |
||
| 115 | return $m[$level]; |
||
| 116 | } |
||
| 117 | |||
| 118 | throw new \Exception(__FUNCTION__ . "($level): $level is unknown"); |
||
| 119 | } |
||
| 120 | } |
||
| 121 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.