llwebsol /
rAPId
| 1 | <?php |
||
| 2 | |||
| 3 | namespace rAPId\Debug; |
||
| 4 | |||
| 5 | use rAPId\Config\Config; |
||
| 6 | use Throwable; |
||
| 7 | |||
| 8 | class ServerLog implements Logger |
||
| 9 | { |
||
| 10 | public static function logMessage($message) { |
||
| 11 | $message = self::getLogMessage($message); |
||
| 12 | self::write_to_log_file($message); |
||
| 13 | } |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @param string $message |
||
| 17 | * |
||
| 18 | * @return string |
||
| 19 | */ |
||
| 20 | protected static function getLogMessage($message) { |
||
| 21 | $log_message = PHP_EOL . date('Y-m-d h:i:s A T') . PHP_EOL; |
||
| 22 | $log_message .= '==================================' . PHP_EOL; |
||
| 23 | $log_message .= $message . PHP_EOL; |
||
| 24 | |||
| 25 | return $log_message; |
||
| 26 | } |
||
| 27 | |||
| 28 | protected static function write_to_log_file($log_entry) { |
||
| 29 | $log_path = Config::val('log_path', 'logs/'); |
||
| 30 | |||
| 31 | // Make sure directory Exists |
||
| 32 | if (!file_exists($log_path)) { |
||
| 33 | mkdir($log_path, 0744); |
||
| 34 | } |
||
| 35 | |||
| 36 | $filename = $log_path . date('Y-m-d') . '.txt'; |
||
| 37 | |||
| 38 | $handle = fopen($filename, 'a'); |
||
| 39 | if ($handle) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 40 | fwrite($handle, $log_entry); |
||
| 41 | fclose($handle); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param int $errno |
||
| 47 | * @param string $errstr |
||
| 48 | * @param string $errfile |
||
| 49 | * @param string $errline |
||
| 50 | */ |
||
| 51 | public static function logError($errno, $errstr, $errfile, $errline) { |
||
| 52 | $name = self::getErrorName($errno); |
||
| 53 | $log_entry = self::getLogErrorMessage($name, $errstr, $errfile, $errline); |
||
| 54 | self::write_to_log_file($log_entry); |
||
| 55 | } |
||
| 56 | |||
| 57 | protected static function getErrorName($errno) { |
||
| 58 | switch ($errno) { |
||
| 59 | case E_ERROR: |
||
| 60 | case E_USER_ERROR: |
||
| 61 | return 'ERROR'; |
||
| 62 | break; |
||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||
| 63 | case E_WARNING: |
||
| 64 | case E_USER_WARNING: |
||
| 65 | return 'WARNING'; |
||
| 66 | break; |
||
| 67 | case E_NOTICE: |
||
| 68 | case E_USER_NOTICE: |
||
| 69 | return 'NOTICE'; |
||
| 70 | break; |
||
| 71 | case E_DEPRECATED: |
||
| 72 | case E_USER_DEPRECATED: |
||
| 73 | return 'DEPRECATION WARNING:'; |
||
| 74 | break; |
||
| 75 | default: |
||
| 76 | return 'UNKNOWN ERROR'; |
||
| 77 | break; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | protected static function getLogErrorMessage($name, $message, $file, $line) { |
||
| 82 | $log_message = $name . PHP_EOL; |
||
| 83 | |||
| 84 | $log_message .= "In $file on line:$line" . PHP_EOL; |
||
| 85 | $log_message .= $message; |
||
| 86 | |||
| 87 | return self::getLogMessage($log_message); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param Throwable $exception |
||
| 92 | */ |
||
| 93 | public static function logException(Throwable $exception) { |
||
| 94 | $name = get_class($exception) . ' { ' . $exception->getCode() . ' }'; |
||
| 95 | $log_entry = self::getLogErrorMessage($name, $exception->getMessage(), $exception->getFile(), $exception->getLine()); |
||
| 96 | self::write_to_log_file($log_entry); |
||
| 97 | } |
||
| 98 | } |