Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
iglocska committed Jun 18, 2024
2 parents fd9da66 + 2655241 commit 52a1635
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
* breathing room to complete logging or error handling.
*/
'Error' => [
'logger' => App\Error\ExtendedErrorLogger::class,
'errorLevel' => E_ALL,
'skipLog' => [],
'log' => true,
Expand Down
62 changes: 62 additions & 0 deletions src/Error/ExtendedErrorLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);

namespace App\Error;

use Cake\Error\ErrorLogger;
use Cake\Error\Debugger;
use Cake\Error\PhpError;
use Cake\Core\Configure;
use Cake\Core\Exception\CakeException;
use Cake\Core\InstanceConfigTrait;
use Cake\Log\Log;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;


/**
* Log errors and unhandled exceptions to `Cake\Log\Log`
*/
class ExtendedErrorLogger extends ErrorLogger
{

/**
* Get the request context for an error/exception trace.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request to read from.
* @return string
*/
public function getRequestContext(ServerRequestInterface $request): string
{
$message = "\nRequest URL: " . $request->getRequestTarget();

$referer = $request->getHeaderLine('Referer');
if ($referer) {
$message .= "\nReferer URL: " . $referer;
}


if (method_exists($request, 'clientIp')) {
$clientIp = $request->clientIp();
if ($clientIp && $clientIp !== '::1') {
$message .= "\nClient IP: " . $clientIp;
}
}

if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$message .= sprintf(
"\nClient X_FORWARDED_FOR: %s",
$_SERVER['HTTP_X_FORWARDED_FOR']
);
}

if (!empty($request->getSession()->read('authUser'))) {
$message .= sprintf(
"\nClient user: #%s :: %s",
$request->getSession()->read('authUser.id'),
$request->getSession()->read('authUser.username')
);
}
return $message;
}
}

0 comments on commit 52a1635

Please sign in to comment.