Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Logging/LogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function write(array|LogRecord $record): void
$record = $record->toArray();
}

if (! Arr::get($record, 'context.exception')) {
if (! $error = Arr::get($record, 'context.exception')) {
$errors = collect([
'Request URL: '.request()->fullUrl(),
'Request data: '.json_encode(request()->input()),
Expand All @@ -48,23 +48,32 @@ public function write(array|LogRecord $record): void
front()
->post(
"/conversations/$conversation/comments",
$this->convertErrorToFrontMessage(Arr::get($record, 'context.exception'))
$this->convertErrorToFrontMessage($error, Arr::except(Arr::get($record, 'context', []), 'exception'))
)->throw();
}

private function convertErrorToFrontMessage(Throwable $error): array
private function convertErrorToFrontMessage(Throwable $error, array $context): array
{
return ['body' => $this->formatErrorLines($error)->implode(PHP_EOL)];
return ['body' => $this->formatErrorLines($error, $context)->implode(PHP_EOL)];
}

private function formatErrorLines(Throwable $error): Collection
private function formatContext(array $context): Collection
{
return collect($context)
->map(fn ($value, string $key) => '* '.$key.': '.(is_string($value) ? $value : json_encode($value)))
->values();
}

private function formatErrorLines(Throwable $error, array $context): Collection
{
return collect([
'Request URL: '.request()->fullUrl(),
'Request data: '.json_encode(request()->input()),
'**'.$error->getMessage().'**',
'* '.$error->getFile().' ('.$error->getLine().')',
])->merge($this->formatStackTrace($error));
])
->merge($this->formatContext($context))
->merge($this->formatStackTrace($error));
}

private function formatStackTrace(Throwable $error): Collection
Expand Down
51 changes: 51 additions & 0 deletions tests/Logging/LogHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use Illuminate\Support\Facades\Http;
use Monolog\Level;
use Monolog\LogRecord;
use TransformStudios\Front\Logging\LogHandler;

beforeEach(function () {
config()->set('front.logging.conversation_id', 'cnv_id');
Http::preventStrayRequests();
Http::fake(['https://api2.frontapp.com/conversations/cnv_id/comments' => Http::response([], 200)]);
});

it('adds the log context to the comment', function () {
(new LogHandler(['level' => 'error']))->write(record(context: [
'exception' => new Exception('boom'),
'redis' => ['loading' => 1, 'uptime_in_seconds' => 12],
'userId' => 7,
]));

Http::assertSent(function ($request) {
expect($request['body'])
->toContain('* redis: {"loading":1,"uptime_in_seconds":12}')
->toContain('* userId: 7');

return true;
});
});

it('leaves the exception out of the rendered context', function () {
(new LogHandler(['level' => 'error']))->write(record(context: ['exception' => new Exception('boom')]));

Http::assertSent(function ($request) {
expect($request['body'])
->toContain('**boom**')
->not->toContain('* exception:');

return true;
});
});

function record(array $context): LogRecord
{
return new LogRecord(
datetime: new DateTimeImmutable,
channel: 'testing',
level: Level::Error,
message: 'boom',
context: $context,
);
}
Loading