Skip to content

Commit

Permalink
include context no matter if with or without exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 12, 2025
1 parent b9f8751 commit c8d9d91
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/Formatters/GithubIssueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Naoray\LaravelGithubMonolog\Formatters;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
Expand Down Expand Up @@ -54,7 +55,7 @@ public function formatBatch(array $records): array
private function generateSignature(LogRecord $record, ?Throwable $exception): string
{
if (! $exception) {
return md5($record->message.json_encode($record->context));
return md5($record->message . json_encode($record->context));
}

$trace = $exception->getTrace();
Expand All @@ -64,7 +65,7 @@ private function generateSignature(LogRecord $record, ?Throwable $exception): st
$exception::class,
$exception->getFile(),
$exception->getLine(),
$firstFrame ? ($firstFrame['file'] ?? '').':'.($firstFrame['line'] ?? '') : '',
$firstFrame ? ($firstFrame['file'] ?? '') . ':' . ($firstFrame['line'] ?? '') : '',
]));
}

Expand Down Expand Up @@ -110,7 +111,7 @@ private function formatTitle(LogRecord $record, ?Throwable $exception = null): s
private function formatContent(LogRecord $record, ?Throwable $exception): string
{
return Str::of('')
->when($record->message, fn ($str, $message) => $str->append("**Message:**\n{$message}\n\n"))
->when($record->message, fn($str, $message) => $str->append("**Message:**\n{$message}\n\n"))
->when(
$exception,
function (Stringable $str, Throwable $exception) {
Expand All @@ -120,8 +121,8 @@ function (Stringable $str, Throwable $exception) {
);
}
)
->when(! $exception && ! empty($record->context), fn ($str, $context) => $str->append("**Context:**\n```json\n".json_encode($record->context, JSON_PRETTY_PRINT)."\n```\n\n"))
->when(! empty($record->extra), fn ($str, $extra) => $str->append("**Extra Data:**\n```json\n".json_encode($record->extra, JSON_PRETTY_PRINT)."\n```\n"))
->when(! empty($record->context), fn($str, $context) => $str->append("**Context:**\n```json\n" . json_encode(Arr::except($record->context, ['exception']), JSON_PRETTY_PRINT) . "\n```\n\n"))
->when(! empty($record->extra), fn($str, $extra) => $str->append("**Extra Data:**\n```json\n" . json_encode($record->extra, JSON_PRETTY_PRINT) . "\n```\n"))
->toString();
}

Expand All @@ -141,7 +142,7 @@ private function formatBody(LogRecord $record, string $signature, ?Throwable $ex
private function cleanStackTrace(string $stackTrace): string
{
return collect(explode("\n", $stackTrace))
->filter(fn ($line) => ! empty(trim($line)))
->filter(fn($line) => ! empty(trim($line)))
->map(function ($line) {
if (trim($line) === '"}') {
return '';
Expand Down Expand Up @@ -217,8 +218,8 @@ private function formatExceptionDetails(Throwable $exception): array

return [
'message' => $exception->getMessage(),
'stack_trace' => $header."\n[stacktrace]\n".$this->cleanStackTrace($exception->getTraceAsString()),
'full_stack_trace' => $header."\n[stacktrace]\n".$exception->getTraceAsString(),
'stack_trace' => $header . "\n[stacktrace]\n" . $this->cleanStackTrace($exception->getTraceAsString()),
'full_stack_trace' => $header . "\n[stacktrace]\n" . $exception->getTraceAsString(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Formatters/GithubIssueFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
channel: 'test',
level: Level::Error,
message: 'Test message',
context: ['user_id' => 123, 'action' => 'login'],
context: ['user_id' => 123, 'action' => 'login', 'exception' => new RuntimeException('Test exception')],
extra: []
);

Expand Down

0 comments on commit c8d9d91

Please sign in to comment.