|
| 1 | +<?php |
| 2 | +namespace Portainer\Helper; |
| 3 | + |
| 4 | +class Log extends \Exception { |
| 5 | + |
| 6 | + /** |
| 7 | + * Log constructor. |
| 8 | + * @param string $message The exception message |
| 9 | + * @param int $code The exception code |
| 10 | + * @param \Exception|null $previous A reference to the previous exception |
| 11 | + */ |
| 12 | + public function __construct($message, $code = 0, \Exception $previous = null){ |
| 13 | + $trace = $this->getTraceAsString() ?? "N/A"; |
| 14 | + $this->error_rep(message: $message, method: $trace); |
| 15 | + parent::__construct($message, $code, $previous); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @return string |
| 20 | + */ |
| 21 | + public function __toString(): string { |
| 22 | + return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * `error_rep()` function is used to log the error message to the error log file. |
| 27 | + * @param string $message The error message to be logged. |
| 28 | + * @param string $method Name of either the method name that called the function or the request method. |
| 29 | + */ |
| 30 | + public static function error_rep($message, $method = null){ |
| 31 | + $error_file = self::logrotate(); // file on your fs, e.g. /var/www/html/error.log |
| 32 | + $version = NULL; |
| 33 | + if($method == null){ |
| 34 | + $method = @$_SERVER["REQUEST_METHOD"]; |
| 35 | + } |
| 36 | + $uri = @$_SERVER["REQUEST_URI"]; |
| 37 | + $host = @$_SERVER["HTTP_HOST"]; |
| 38 | + $port = @$_SERVER["SERVER_PORT"]; |
| 39 | + $script = @$_SERVER["SCRIPT_FILENAME"]; |
| 40 | + $name = @$_SERVER["SERVER_NAME"] ?? "N/A"; |
| 41 | + $addr = @$_SERVER["SERVER_ADDR"] ?? "N/A"; |
| 42 | + $rhost = @$_SERVER["REMOTE_HOST"] ?? "N/A"; |
| 43 | + $time = date("[d.m.Y | H:i:s]"); |
| 44 | + error_log("{$time} \"{$message}\"\nURL: {$host}{$uri} \nVersion: {$version} Server IP:{$addr} - Server Name: {$name} - Request Method: '{$method}'\nRemote Addresse: {$addr} - Remote Name: '{$rhost}' - Remote Port: {$port}\nScript Name: '{$script}'\n=======================\n", 3, $error_file); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * `getSpecificLogFilePath()` function is used to get the specific log file path. |
| 50 | + * @param string|null $date The date to get the log file path for. Format: "Y-m-d". |
| 51 | + * @return string The specific log file path. |
| 52 | + */ |
| 53 | + private static function getSpecificLogFilePath($date = null){ |
| 54 | + if($date == null){ |
| 55 | + $date = date("Y-m-d"); |
| 56 | + return __DIR__ . "/data/logs/log-{$date}.log"; |
| 57 | + } else { |
| 58 | + preg_match("/^\d{4}-\d{2}-\d{2}$/m", $date, $match); |
| 59 | + if($match[0] == null){ |
| 60 | + self::getSpecificLogFilePath(); |
| 61 | + } |
| 62 | + Log::error_rep("Trying to get log file for date '$date'"); |
| 63 | + return __DIR__ . "/data/logs/log-{$match[0]}.log"; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * `logrotate()` function is used to rotate the log file. |
| 69 | + * @return string The path to the log file. |
| 70 | + */ |
| 71 | + private static function logrotate(){ |
| 72 | + $logpath = __DIR__ . "/data/logs/"; |
| 73 | + $date = date("Y-m-d"); |
| 74 | + $lastrotate = @file_get_contents(__DIR__ . "/data/logs/logrotate-cache.txt"); |
| 75 | + |
| 76 | + if($date != $lastrotate){ |
| 77 | + if(!file_exists($logpath . "log-{$date}.log")){ |
| 78 | + $newlog = $logpath . "log-{$date}.log"; |
| 79 | + file_put_contents($newlog, ""); |
| 80 | + file_put_contents(__DIR__ . "/data/logs/logrotate-cache.txt", $date); |
| 81 | + } |
| 82 | + return __DIR__ . "/data/logs/log-{$date}.log"; |
| 83 | + } |
| 84 | + return __DIR__ . "/data/logs/log-{$date}.log"; |
| 85 | + } |
| 86 | +} |
0 commit comments