Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 10, 2025
1 parent 951503d commit f9539b2
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions src/GithubIssueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ private function generateSignature(LogRecord $record, ?Throwable $exception): st
get_class($exception),
$exception->getFile(),
$exception->getLine(),
$firstFrame ? ($firstFrame['file'] ?? '').':'.($firstFrame['line'] ?? '') : '',
$firstFrame ? ($firstFrame['file'] ?? '') . ':' . ($firstFrame['line'] ?? '') : '',
]));
}

return md5($record->message.json_encode($record->context));
return md5($record->message . json_encode($record->context));
}

/**
Expand All @@ -90,7 +90,7 @@ private function formatTitle(LogRecord $record, ?Throwable $exception = null): s
{
if ($exception) {
$exceptionClass = (new ReflectionClass($exception))->getShortName();
$file = Str::replace(base_path().'/', '', $exception->getFile());
$file = Str::replace(base_path() . '/', '', $exception->getFile());

return Str::of('[{level}] {class} in {file}:{line} - {message}')
->replace('{level}', $record->level->getName())
Expand Down Expand Up @@ -121,11 +121,11 @@ private function formatContent(LogRecord $record, ?Throwable $exception): string
$previousExceptions = $this->formatPreviousExceptions($exception);
$body .= $this->renderPreviousExceptions($previousExceptions);
} elseif (! empty($record->context)) {
$body .= "**Context:**\n```json\n".json_encode($record->context, JSON_PRETTY_PRINT)."\n```\n\n";
$body .= "**Context:**\n```json\n" . json_encode($record->context, JSON_PRETTY_PRINT) . "\n```\n\n";
}

if (! empty($record->extra)) {
$body .= "**Extra Data:**\n```json\n".json_encode($record->extra, JSON_PRETTY_PRINT)."\n```\n";
$body .= "**Extra Data:**\n```json\n" . json_encode($record->extra, JSON_PRETTY_PRINT) . "\n```\n";
}

return $body;
Expand All @@ -144,35 +144,29 @@ private function formatBody(LogRecord $record, string $signature, ?Throwable $ex
private function cleanStackTrace(string $stackTrace): string
{
$frames = collect(explode("\n", $stackTrace))
->filter(fn ($line) => ! empty(trim($line)))
->filter(fn($line) => ! empty(trim($line)))
->map(function ($line) {
// Extract frame number and content
if (! Str::match('/^#\d+\s+/', $line)) {
// Not a stack frame line, return as is
if (! Str::isMatch('/#[0-9]+ /', $line)) {
return $line;
}

$frameNumber = Str::match('/^(#\d+)/', $line);
$frame = Str::match('/^#\d+\s+(.+?)(?:\(\d+\))?$/', $line);

if (empty($frame)) {
return $line;
}

// Replace base path with relative path
$frame = Str::replace(base_path(), '', $frame);

return $frameNumber.' '.$frame;
});
// Make the line shorter by removing the base path
return str_replace(base_path(), '', $line);
})
->values();

$vendorFrames = collect();
$result = collect();
$vendorFrames = collect();

foreach ($frames as $frame) {
if (Str::contains($frame, '/vendor/')) {
$isVendorFrame = Str::contains($frame, '/vendor/') && ! Str::isMatch("/BoundMethod\.php\([0-9]+\):/", $frame);

if ($isVendorFrame) {
$vendorFrames->push($frame);
} else {
if ($vendorFrames->isNotEmpty()) {
$indentedFrames = $vendorFrames->map(fn ($frame) => " $frame")->implode("\n");
$indentedFrames = $vendorFrames->map(fn($frame) => " $frame")->implode("\n");
$result->push(Str::replace('{frames}', $indentedFrames, self::VENDOR_FRAME_PLACEHOLDER));
$vendorFrames = collect();
}
Expand All @@ -182,7 +176,7 @@ private function cleanStackTrace(string $stackTrace): string

// Add any remaining vendor frames
if ($vendorFrames->isNotEmpty()) {
$indentedFrames = $vendorFrames->map(fn ($frame) => " $frame")->implode("\n");
$indentedFrames = $vendorFrames->map(fn($frame) => " $frame")->implode("\n");
$result->push(Str::replace('{frames}', $indentedFrames, self::VENDOR_FRAME_PLACEHOLDER));
}

Expand Down Expand Up @@ -227,8 +221,8 @@ private function formatPreviousExceptions(Throwable $exception): array

private function renderExceptionDetails(array $details): string
{
$content = "**Message:**\n```\n{$details['message']}\n```\n\n";
$content .= "**Stack Trace:**\n```php\n{$details['stack_trace']}\n```\n\n";
$content = sprintf("**Message:**\n```\n%s\n```\n\n", $details['message']);
$content .= sprintf("**Stack Trace:**\n%s\n\n", $details['stack_trace']);

return $content;
}
Expand Down

0 comments on commit f9539b2

Please sign in to comment.