I want to append to each error log, the url of the page the user requested before the error occured. The logs already give me where the error occurs, but it's valuable for me know the url.
I saw there is a Handler.php file but how can I append the url there?
A log file is a file that keeps a registry or records of events, processes, messages that occur in the operating system or any software runs. -Laravel provides the robust logging services that allow the user to log messages to file. -Laravel uses the Monolog library, which is an existing standard logging library for PHP.
We can set a different location for error log via directive ‘ErrorLog’. there. After that, we have to set the Log Severity Levels. In laravel, there are different severity or security levels. By default, Laravel writes all the log levels to its storage. By default, Laravel comes with level ‘debug‘, which stores all messages.
Laravel supports different logging modes like single, daily, syslog, and errorlog modes. You can set these modes in config/app.php file. You can see the generated log entries in storage/logs/laravel.log file.
Note − After changing the APP_DEBUG variable, you should restart the Laravel server. Logging is an important mechanism by which system can log errors that are generated. It is useful to improve the reliability of the system.
Even better way of doing this:
In App\Exceptions\Handler extend Laravel's base context() function:
use Throwable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
/**
* Get the default context variables for logging.
*
* @return array
*/
protected function context()
{
try {
return array_filter([
'url' => Request::fullUrl(),
'input' => Request::except(['password', 'password_confirmation']),
'userId' => Auth::id(),
'email' => Auth::user() ? Auth::user()->email : null,
]);
} catch (Throwable $e) {
return [];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With