generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Fix | Replace Listener For Terminatable Middleware #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Sammyjo20
wants to merge
5
commits into
bilfeldt:main
from
PlannrCrm:fix/move-listener-code-to-terminatable-middleware
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
661a383
Fix | Replace Listener For Terminatable Middleware
Sammyjo20 11e0c52
Removed listener
Sammyjo20 5bc3634
Move terminating logic into invokable class
Sammyjo20 b7f8114
Introduce array logger and added basic feature end-to-end test
Sammyjo20 140655a
Removed use
Sammyjo20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ testbench.yaml | |
vendor | ||
node_modules | ||
.php-cs-fixer.cache | ||
.phpunit.cache/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Bilfeldt\RequestLogger; | ||
|
||
use Bilfeldt\RequestLogger\Contracts\RequestLoggerInterface; | ||
use Bilfeldt\RequestLogger\Traits\Loggable; | ||
use Illuminate\Http\Request; | ||
|
||
class ArrayLogger implements RequestLoggerInterface | ||
{ | ||
use Loggable; | ||
|
||
/** | ||
* Logs | ||
* | ||
* @var array<string, mixed> | ||
*/ | ||
protected array $logs = []; | ||
|
||
/** @inheritDoc */ | ||
public function log(Request $request, $response, ?int $duration = null, ?int $memory = null): void | ||
{ | ||
$this->logs[] = [ | ||
'uuid' => $request->getUniqueId(), | ||
'correlation_id' => $this->truncateToLength($request->getCorrelationId()), | ||
'client_request_id' => $this->truncateToLength($request->getClientRequestId()), | ||
'ip' => $request->ip(), | ||
'session' => $request->hasSession() ? $request->session()->getId() : null, | ||
'middleware' => array_values(optional($request->route())->gatherMiddleware() ?? []), | ||
'method' => $request->getMethod(), | ||
'route' => $this->truncateToLength(optional($request->route())->getName() ?? optional($request->route())->uri()), | ||
'path' => $this->truncateToLength($request->path()), | ||
'status' => $response->getStatusCode(), | ||
'headers' => $this->getFiltered($request->headers->all()) ?: null, | ||
'payload' => $this->getFiltered($request->input()) ?: null, | ||
'response_headers' => $this->getFiltered($response->headers->all()) ?: null, | ||
'response_body' => $this->getLoggableResponseContent($response), | ||
'duration' => $duration, | ||
'memory' => round($memory / 1024 / 1024, 2), | ||
]; | ||
} | ||
|
||
/** | ||
* Get logs | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function getLogs(): array | ||
{ | ||
return $this->logs; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -29,6 +29,11 @@ public function createNullDriver(): NullLogger | |||||||||
return new NullLogger(); | ||||||||||
} | ||||||||||
|
||||||||||
public function createArrayDriver(): ArrayLogger | ||||||||||
{ | ||||||||||
return app()->make(ArrayLogger::class); | ||||||||||
} | ||||||||||
|
||||||||||
Comment on lines
+32
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't really wanna have the burden of maintaining an Array logger when I think that is only relevant in the context of tests. |
||||||||||
public function createModelDriver(): RequestLoggerInterface | ||||||||||
{ | ||||||||||
$model = config('request-logger.drivers.model.class'); | ||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Bilfeldt\RequestLogger\Traits; | ||
|
||
use Bilfeldt\RequestLogger\RequestLoggerFacade; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Response; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Str; | ||
use Illuminate\View\View; | ||
|
||
trait Loggable | ||
{ | ||
protected function getLoggableResponseContent(\Symfony\Component\HttpFoundation\Response $response): array | ||
{ | ||
$content = $response->getContent(); | ||
|
||
if (is_string($content)) { | ||
if (is_array(json_decode($content, true)) && | ||
json_last_error() === JSON_ERROR_NONE) { | ||
return $this->contentWithinLimits($content) | ||
? $this->getFiltered(json_decode($content, true)) | ||
: ['Purged By bilfeldt/laravel-request-logger']; | ||
} | ||
|
||
if (Str::startsWith(strtolower($response->headers->get('Content-Type')), 'text/plain')) { | ||
return $this->contentWithinLimits($content) ? [$content] : ['purge' => 'bilfeldt/laravel-request-logger']; | ||
} | ||
} | ||
|
||
if ($response instanceof RedirectResponse) { | ||
return ['redirect' => $response->getTargetUrl()]; | ||
} | ||
|
||
if ($response instanceof Response && $response->getOriginalContent() instanceof View) { | ||
return [ | ||
'view' => $response->getOriginalContent()->getPath(), | ||
//'data' => $this->extractDataFromView($response->getOriginalContent()), | ||
]; | ||
} | ||
|
||
return ['html' => 'non-json']; | ||
} | ||
|
||
protected function contentWithinLimits(string $content): bool | ||
{ | ||
return intdiv(mb_strlen($content), 1000) <= 64; | ||
} | ||
|
||
protected function getFiltered(array $data) | ||
{ | ||
return $this->replaceParameters($data, RequestLoggerFacade::getFilters()); | ||
} | ||
|
||
protected function replaceParameters(array $array, array $hidden, string $value = '********'): array | ||
{ | ||
foreach ($hidden as $parameter) { | ||
if (Arr::get($array, $parameter)) { | ||
Arr::set($array, $parameter, '********'); | ||
} | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
protected function truncateToLength(?string $string, int $length = 255): ?string | ||
{ | ||
if (! $string) { | ||
return $string; | ||
} | ||
|
||
$truncator = '...'; | ||
|
||
if (mb_strwidth($string, 'UTF-8') <= $length) { | ||
return $string; | ||
} | ||
|
||
return Str::limit($string, $length - mb_strwidth($truncator, 'UTF-8'), $truncator); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.