shubraVeil/includes/Logger.php
2024-12-25 13:05:50 +02:00

73 lines
1.9 KiB
PHP

<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Formatter\LineFormatter;
class AppLogger {
private static $instance = null;
private $logger;
private function __construct() {
$this->logger = new Logger('shubraveil');
// Create logs directory if it doesn't exist
if (!file_exists(__DIR__ . '/../logs')) {
mkdir(__DIR__ . '/../logs', 0755, true);
}
// Add handlers
$this->addFileHandler();
if (getenv('DEBUG_MODE') === 'true') {
$this->addDebugHandler();
}
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
private function addFileHandler() {
$formatter = new LineFormatter(
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
"Y-m-d H:i:s"
);
$handler = new RotatingFileHandler(
__DIR__ . '/../logs/app.log',
30, // Keep last 30 days of logs
Logger::INFO
);
$handler->setFormatter($formatter);
$this->logger->pushHandler($handler);
}
private function addDebugHandler() {
$handler = new StreamHandler(
__DIR__ . '/../logs/debug.log',
Logger::DEBUG
);
$this->logger->pushHandler($handler);
}
public function info($message, array $context = []) {
$this->logger->info($message, $context);
}
public function error($message, array $context = []) {
$this->logger->error($message, $context);
}
public function debug($message, array $context = []) {
$this->logger->debug($message, $context);
}
public function warning($message, array $context = []) {
$this->logger->warning($message, $context);
}
}