feature/cierres (#25)
Varios cambios Co-authored-by: Juan Pablo Vial <jpvialb@incoviba.cl> Reviewed-on: #25
This commit is contained in:
48
app/common/Implement/Log/Formatter/PDO.php
Normal file
48
app/common/Implement/Log/Formatter/PDO.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
namespace Incoviba\Common\Implement\Log\Formatter;
|
||||
|
||||
use Throwable;
|
||||
use Monolog\Formatter\JsonFormatter;
|
||||
use Monolog\LogRecord;
|
||||
|
||||
class PDO extends JsonFormatter
|
||||
{
|
||||
public function __construct(int $batchMode = self::BATCH_MODE_JSON, bool $appendNewline = false, bool $ignoreEmptyContextAndExtra = false, bool $includeStacktraces = true)
|
||||
{
|
||||
parent::__construct($batchMode, $appendNewline, $ignoreEmptyContextAndExtra, $includeStacktraces);
|
||||
}
|
||||
|
||||
public function format(LogRecord $record): string
|
||||
{
|
||||
if (is_a($record->message, Throwable::class)) {
|
||||
$exception = $record->message;
|
||||
$message = $this->normalizeException($exception);
|
||||
$context = $record->context;
|
||||
$context['exception'] = $exception;
|
||||
if ($exception->getPrevious()) {
|
||||
$context['previous'] = $this->walkException($exception);
|
||||
}
|
||||
$new_record = new LogRecord(
|
||||
$record->datetime,
|
||||
$record->channel,
|
||||
$record->level,
|
||||
json_encode($message),
|
||||
$context,
|
||||
$record->extra
|
||||
);
|
||||
$record = $new_record;
|
||||
}
|
||||
$normalized = $this->normalize($record, $this->maxNormalizeDepth);
|
||||
return $normalized['message'];
|
||||
}
|
||||
|
||||
protected function walkException(Throwable $exception, int $depth = 0): array
|
||||
{
|
||||
$output = [];
|
||||
$currentDepth = $depth;
|
||||
while ($previous = $exception->getPrevious() and $currentDepth < $this->maxNormalizeDepth) {
|
||||
$output []= $this->normalizeException($previous);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user