Update -> add new log and analytices users
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/logger.php';
|
||||
|
||||
$config = require __DIR__ . '/../config/config.php';
|
||||
|
||||
try {
|
||||
@@ -14,6 +16,7 @@ try {
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
]);
|
||||
} catch (PDOException $e) {
|
||||
(new Logger('db'))->error('Database connection failed', ['error' => $e->getMessage()]);
|
||||
http_response_code(503);
|
||||
die(json_encode(['success' => false, 'message' => 'Database connection error.']));
|
||||
}
|
||||
|
||||
76
app/core/logger.php
Normal file
76
app/core/logger.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
class Logger
|
||||
{
|
||||
const INFO = 'INFO';
|
||||
const WARNING = 'WARNING';
|
||||
const ERROR = 'ERROR';
|
||||
|
||||
private string $logDir;
|
||||
private string $context;
|
||||
|
||||
public function __construct(string $context = 'app')
|
||||
{
|
||||
$this->logDir = __DIR__ . '/../../storage/logs';
|
||||
$this->context = $context;
|
||||
|
||||
if (!is_dir($this->logDir)) {
|
||||
mkdir($this->logDir, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
public function info(string $message, array $extra = []): void
|
||||
{
|
||||
$this->write(self::INFO, $message, $extra);
|
||||
}
|
||||
|
||||
public function warning(string $message, array $extra = []): void
|
||||
{
|
||||
$this->write(self::WARNING, $message, $extra);
|
||||
}
|
||||
|
||||
public function error(string $message, array $extra = []): void
|
||||
{
|
||||
$this->write(self::ERROR, $message, $extra);
|
||||
}
|
||||
|
||||
private function write(string $level, string $message, array $extra = []): void
|
||||
{
|
||||
$date = date('Y-m-d');
|
||||
$file = "{$this->logDir}/{$date}.log";
|
||||
$time = date('Y-m-d H:i:s');
|
||||
$ip = $this->getClientIp();
|
||||
$extraStr = empty($extra) ? '' : ' ' . json_encode($extra, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$line = "[{$time}] [{$level}] [{$this->context}] [{$ip}] {$message}{$extraStr}" . PHP_EOL;
|
||||
|
||||
file_put_contents($file, $line, FILE_APPEND | LOCK_EX);
|
||||
}
|
||||
|
||||
private function getClientIp(): string
|
||||
{
|
||||
foreach (['HTTP_X_FORWARDED_FOR', 'HTTP_X_REAL_IP', 'REMOTE_ADDR'] as $key) {
|
||||
if (!empty($_SERVER[$key])) {
|
||||
$ip = trim(explode(',', $_SERVER[$key])[0]);
|
||||
if (filter_var($ip, FILTER_VALIDATE_IP)) {
|
||||
return $ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
public static function cleanOldLogs(int $keepDays = 30): void
|
||||
{
|
||||
$logDir = __DIR__ . '/../../storage/logs';
|
||||
if (!is_dir($logDir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (glob("{$logDir}/*.log") as $file) {
|
||||
if (filemtime($file) < time() - ($keepDays * 86400)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user