diff --git a/README.md b/README.md index d6f11e8..5a302e5 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,11 @@

-PHP Logger Release +PHP Logger Release PHP Logger Repo stars License PHP Version +psr/log Version

# PHP Request Logger diff --git a/src/index.php b/src/index.php index 1f6aae4..5ac460f 100644 --- a/src/index.php +++ b/src/index.php @@ -1,4 +1,7 @@ debug('Log from index.php', $data); +} + +/** + * Request logged with manually + */ +/*try { header("Content-Type: application/json; charset=UTF-8"); $response = new \App\Library\Response(); $db = \App\Config\DB\getDatabaseConnection(); @@ -66,7 +81,27 @@ function logRequestToDatabase($pdo, $data) logRequestToDatabase($db, $requestData); $response->setStatus(true); $response->setStatusCode(200); - $response->setMessage("Request logged."); + $response->setMessage("Request logged with manually."); + $response->setDate($date); + echo $response->toJson(); + exit(); +} catch (\Exception $e) { + throw new \Exception($e->getMessage(), (int) $e->getCode()); +}*/ + +/** + * Request logged with psr/log package + */ +try { + header("Content-Type: application/json; charset=UTF-8"); + $response = new \App\Library\Response(); + $db = \App\Config\DB\getDatabaseConnection(); + $requestData = getRequestData(); + logFileWithPsrLog($requestData); + logRequestToDatabase($db, $requestData); + $response->setStatus(true); + $response->setStatusCode(200); + $response->setMessage("Request logged with psr/log package."); $response->setDate($date); echo $response->toJson(); exit(); diff --git a/src/lib/FileLogger.php b/src/lib/FileLogger.php new file mode 100644 index 0000000..c3ea1bf --- /dev/null +++ b/src/lib/FileLogger.php @@ -0,0 +1,79 @@ +log(LogLevel::EMERGENCY, $message, $context); + } + + public function alert($message, array $context = []): void + { + $this->log(LogLevel::ALERT, $message, $context); + } + + public function critical($message, array $context = []): void + { + $this->log(LogLevel::CRITICAL, $message, $context); + } + + public function error($message, array $context = []): void + { + $this->log(LogLevel::ERROR, $message, $context); + } + + public function warning($message, array $context = []): void + { + $this->log(LogLevel::WARNING, $message, $context); + } + + public function notice($message, array $context = []): void + { + $this->log(LogLevel::NOTICE, $message, $context); + } + + public function info($message, array $context = []): void + { + $this->log(LogLevel::INFO, $message, $context); + } + + public function debug($message, array $context = []): void + { + $this->log(LogLevel::DEBUG, $message, $context); + } + + public function log($level, $message, array $context = []): void + { + // Current date in 1970-12-01 23:59:59 format + $dateFormatted = date('Y-m-d H:i:s'); + + // Build the message with the current date, log level, + // and the string from the arguments + $contextString = json_encode($context); + $message = sprintf( + '[%s] %s: %s %s%s', + $dateFormatted, + $level, + $message, + $contextString, + PHP_EOL // Line break + ); + + // Writing to the file + $logDir = './logs'; + if (!file_exists($logDir)) { + mkdir($logDir, 0777, true); + } + $logFileName = date('Y-m-d').'.log'; + $logFile = $logDir.'/'.$logFileName; + file_put_contents($logFile, $message, FILE_APPEND); + // FILE_APPEND flag prevents flushing the file content on each call + // and simply adds a new string to it + } +} \ No newline at end of file