FIX: circular dependency
This commit is contained in:
@ -1,23 +1,23 @@
|
||||
<?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 Psr\Log\LoggerInterface;
|
||||
use Monolog\Formatter;
|
||||
use Monolog\Handler;
|
||||
use Monolog\Level;
|
||||
use Predis;
|
||||
use Incoviba;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Throwable;
|
||||
|
||||
class ArrayBuilder extends Ideal\Service
|
||||
class ArrayBuilder
|
||||
{
|
||||
public function __construct(LoggerInterface $logger, protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct($logger);
|
||||
}
|
||||
public function __construct(protected ContainerInterface $container) {}
|
||||
|
||||
public function build(array $data): array
|
||||
{
|
||||
@ -38,7 +38,7 @@ class ArrayBuilder extends Ideal\Service
|
||||
$handler = new $handlerData['handler'](...$params)
|
||||
->setFormatter($this->container->get($formatter));
|
||||
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
||||
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'handlerData' => $handlerData]);
|
||||
$this->log($exception, ['handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
} elseif ($handlerData['handler'] === Incoviba\Common\Implement\Log\Handler\MySQL::class) {
|
||||
@ -53,7 +53,7 @@ class ArrayBuilder extends Ideal\Service
|
||||
$handler = new $handlerData['handler'](...$params)
|
||||
->setFormatter($this->container->get($formatter));
|
||||
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
||||
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'handlerData' => $handlerData]);
|
||||
$this->log($exception, ['handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
} elseif ($handlerData['handler'] === Handler\RedisHandler::class) {
|
||||
@ -63,13 +63,13 @@ class ArrayBuilder extends Ideal\Service
|
||||
"logs:{$handlerData['name']}"
|
||||
];
|
||||
} catch (NotFoundExceptionInterface | ContainerExceptionInterface $exception) {
|
||||
$this->logger->error($exception->getMessage(), ['exception' => $exception, 'handlerData' => $handlerData]);
|
||||
$this->log($exception, ['handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
$handler = new $handlerData['handler'](...$params);
|
||||
}
|
||||
if (!isset($handler)) {
|
||||
$this->logger->error("Invalid handler", ['handlerData' => $handlerData]);
|
||||
$this->log("Invalid handler", ['handlerData' => $handlerData]);
|
||||
continue;
|
||||
}
|
||||
$params = [
|
||||
@ -88,4 +88,52 @@ class ArrayBuilder extends Ideal\Service
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user