new: [extended logger] added
- Added more information about the request to the stack traces - logs user name / ID - logs request x-forwarded-forperm_community_admin
parent
799428d55b
commit
2655241265
|
@ -177,6 +177,7 @@ return [
|
||||||
* breathing room to complete logging or error handling.
|
* breathing room to complete logging or error handling.
|
||||||
*/
|
*/
|
||||||
'Error' => [
|
'Error' => [
|
||||||
|
'logger' => App\Error\ExtendedErrorLogger::class,
|
||||||
'errorLevel' => E_ALL,
|
'errorLevel' => E_ALL,
|
||||||
'skipLog' => [],
|
'skipLog' => [],
|
||||||
'log' => true,
|
'log' => true,
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue