You've already forked Php-Template
All checks were successful
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 2m13s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Successful in 2m4s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 2m16s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 2m47s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 2m20s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m51s
118 lines
3.5 KiB
PHP
118 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Siteworxpro\App\Log;
|
|
|
|
use Monolog\Formatter\JsonFormatter;
|
|
use Monolog\Handler\StreamHandler;
|
|
use Psr\Log\LoggerInterface;
|
|
use Psr\Log\LogLevel;
|
|
use RoadRunner\Logger\Logger as RRLogger;
|
|
use Spiral\Goridge\RPC\RPC;
|
|
|
|
class Logger implements LoggerInterface
|
|
{
|
|
private ?RRLogger $rpcLogger = null;
|
|
|
|
private \Monolog\Logger $monologLogger;
|
|
|
|
private array $levels = [
|
|
LogLevel::EMERGENCY => 0,
|
|
LogLevel::ALERT => 1,
|
|
LogLevel::CRITICAL => 2,
|
|
LogLevel::ERROR => 3,
|
|
LogLevel::WARNING => 4,
|
|
LogLevel::NOTICE => 5,
|
|
LogLevel::INFO => 6,
|
|
LogLevel::DEBUG => 7,
|
|
];
|
|
|
|
public function __construct(private readonly string $level = LogLevel::DEBUG)
|
|
{
|
|
if (isset($_SERVER['RR_RPC'])) {
|
|
$rpc = RPC::create('tcp://127.0.0.1:6001');
|
|
$this->rpcLogger = new RRLogger($rpc);
|
|
}
|
|
|
|
$this->monologLogger = new \Monolog\Logger('app_logger');
|
|
$formatter = new JsonFormatter();
|
|
$this->monologLogger->pushHandler(new StreamHandler('php://stdout')->setFormatter($formatter));
|
|
}
|
|
|
|
public function emergency(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::EMERGENCY, $message, $context);
|
|
}
|
|
|
|
public function alert(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::ALERT, $message, $context);
|
|
}
|
|
|
|
public function critical(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::CRITICAL, $message, $context);
|
|
}
|
|
|
|
public function error(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::ERROR, $message, $context);
|
|
}
|
|
|
|
public function warning(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::WARNING, $message, $context);
|
|
}
|
|
|
|
public function notice(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::NOTICE, $message, $context);
|
|
}
|
|
|
|
public function info(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::INFO, $message, $context);
|
|
}
|
|
|
|
public function debug(\Stringable|string $message, array $context = []): void
|
|
{
|
|
$this->log(LogLevel::DEBUG, $message, $context);
|
|
}
|
|
|
|
public function log($level, \Stringable|string $message, array $context = []): void
|
|
{
|
|
if ($this->levels[$level] > $this->levels[$this->level]) {
|
|
return;
|
|
}
|
|
|
|
if ($this->rpcLogger) {
|
|
switch ($level) {
|
|
case LogLevel::DEBUG:
|
|
$this->rpcLogger->debug((string)$message, $context);
|
|
break;
|
|
case LogLevel::NOTICE:
|
|
case LogLevel::INFO:
|
|
$this->rpcLogger->info((string)$message, $context);
|
|
break;
|
|
case LogLevel::WARNING:
|
|
$this->rpcLogger->warning((string)$message, $context);
|
|
break;
|
|
case LogLevel::CRITICAL:
|
|
case LogLevel::ERROR:
|
|
case LogLevel::ALERT:
|
|
case LogLevel::EMERGENCY:
|
|
$this->rpcLogger->error((string)$message, $context);
|
|
break;
|
|
default:
|
|
$this->rpcLogger->log((string)$message, $context);
|
|
break;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$this->monologLogger->log($this->levels[$level], (string)$message, $context);
|
|
}
|
|
}
|