140 lines
5.5 KiB
PHP
140 lines
5.5 KiB
PHP
<?php
|
|
namespace Incoviba\Common\Implement\Log\Processor;
|
|
|
|
use DateInvalidTimeZoneException;
|
|
use DateMalformedStringException;
|
|
use DateTimeImmutable;
|
|
use DateTimeZone;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\ContainerInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use Monolog\Formatter;
|
|
use Monolog\Handler;
|
|
use Monolog\Level;
|
|
use Predis;
|
|
use Incoviba;
|
|
use Throwable;
|
|
|
|
class ArrayBuilder
|
|
{
|
|
public function __construct(protected ContainerInterface $container) {}
|
|
|
|
public function build(array $data): array
|
|
{
|
|
$handlers = [];
|
|
foreach ($data as $handlerData) {
|
|
if (in_array($handlerData['handler'], [Handler\StreamHandler::class, Handler\RotatingFileHandler::class,])) {
|
|
$params = [
|
|
"/logs/{$handlerData['filename']}",
|
|
];
|
|
if ($handlerData['handler'] === Handler\RotatingFileHandler::class) {
|
|
$params []= 10;
|
|
}
|
|
try {
|
|
$formatter = Formatter\LineFormatter::class;
|
|
if (array_key_exists('formatter', $handlerData)) {
|
|
$formatter = $handlerData['formatter'];
|
|
}
|
|
$handler = new $handlerData['handler'](...$params)
|
|
->setFormatter($this->container->get($formatter));
|
|
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
|
$this->log($exception, ['handlerData' => $handlerData]);
|
|
continue;
|
|
}
|
|
} elseif ($handlerData['handler'] === Incoviba\Common\Implement\Log\Handler\MySQL::class) {
|
|
try {
|
|
$params = [
|
|
$this->container->get(Incoviba\Common\Define\Connection::class)
|
|
];
|
|
$formatter = Incoviba\Common\Implement\Log\Formatter\PDO::class;
|
|
if (array_key_exists('formatter', $handlerData)) {
|
|
$formatter = $handlerData['formatter'];
|
|
}
|
|
$handler = new $handlerData['handler'](...$params)
|
|
->setFormatter($this->container->get($formatter));
|
|
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
|
$this->log($exception, ['handlerData' => $handlerData]);
|
|
continue;
|
|
}
|
|
} elseif ($handlerData['handler'] === Handler\RedisHandler::class) {
|
|
try {
|
|
$params = [
|
|
$this->container->get(Predis\ClientInterface::class),
|
|
"logs:{$handlerData['name']}"
|
|
];
|
|
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
|
$this->log($exception, ['handlerData' => $handlerData]);
|
|
continue;
|
|
}
|
|
$handler = new $handlerData['handler'](...$params);
|
|
}
|
|
if (!isset($handler)) {
|
|
$this->log("Invalid handler", ['handlerData' => $handlerData]);
|
|
continue;
|
|
}
|
|
$params = [
|
|
$handler,
|
|
];
|
|
if (is_array($handlerData['levels'])) {
|
|
foreach ($handlerData['levels'] as $level) {
|
|
$params []= $level;
|
|
}
|
|
} else {
|
|
$params []= $handlerData['levels'];
|
|
$params []= Level::Emergency;
|
|
}
|
|
$params []= false;
|
|
$handlers []= new Handler\FilterHandler(...$params);
|
|
}
|
|
return $handlers;
|
|
}
|
|
|
|
protected function log(string|Throwable $message, array $context = []): void
|
|
{
|
|
try {
|
|
$dateTime = new DateTimeImmutable('now', new DateTimeZone($_ENV['TZ'] ?? 'America/Santiago'));
|
|
} catch (DateMalformedStringException | DateInvalidTimeZoneException $exception) {
|
|
$dateTime = new DateTimeImmutable();
|
|
}
|
|
if (is_a($message, Throwable::class)) {
|
|
$exception = $message;
|
|
$message = $exception->getMessage();
|
|
}
|
|
$context = json_encode($context, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
if ($context === false) {
|
|
$context = '[]';
|
|
}
|
|
$extra = [];
|
|
$extra['from'] = __FILE__;
|
|
if (isset($exception)) {
|
|
$extra['file'] = $exception->getFile();
|
|
$extra['line'] = $exception->getLine();
|
|
$extra['trace'] = $exception->getTrace();
|
|
}
|
|
$extra = json_encode($extra, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
$code = 0;
|
|
if (isset($exception)) {
|
|
$code = $exception->getCode();
|
|
}
|
|
if ($extra === false) {
|
|
$extra = '[]';
|
|
}
|
|
$output = "[{$dateTime->format('Y-m-d H:i:s P')}] [{$code}] {$message} {$context} {$extra}";
|
|
$filename = '/logs/error.json';
|
|
$fileContents = [];
|
|
if (file_exists($filename)) {
|
|
$fileContents = file_get_contents($filename);
|
|
$fileContents = json_decode($fileContents, true);
|
|
if ($fileContents === false) {
|
|
$fileContents = [];
|
|
}
|
|
}
|
|
$fileContents[$dateTime->getTimestamp()] = $output;
|
|
$fileContents = json_encode($fileContents, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
|
if ($fileContents === false) {
|
|
$fileContents = '[]';
|
|
}
|
|
file_put_contents($filename, $fileContents);
|
|
}
|
|
}
|