Files
oficial/app/common/Implement/Log/Formatter/PDO.php

49 lines
1.6 KiB
PHP
Raw Normal View History

2024-04-02 13:50:08 -03:00
<?php
2025-06-03 11:37:14 -04:00
namespace Incoviba\Common\Implement\Log\Formatter;
2024-04-02 13:50:08 -03:00
2025-06-06 17:55:58 -04:00
use Throwable;
2024-04-02 13:50:08 -03:00
use Monolog\Formatter\JsonFormatter;
use Monolog\LogRecord;
2025-06-03 11:37:14 -04:00
class PDO extends JsonFormatter
2024-04-02 13:50:08 -03:00
{
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
{
2025-06-06 17:55:58 -04:00
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;
}
2025-04-22 15:31:55 -04:00
$normalized = $this->normalize($record, $this->maxNormalizeDepth);
2024-04-02 13:50:08 -03:00
return $normalized['message'];
}
2025-06-06 17:55:58 -04:00
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;
}
2024-04-02 13:50:08 -03:00
}