Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Universal Laravel Error Logger Helper
<?php
if ( !function_exists('error_logger')) {
/**
* error_logger
*
* Returns an expanded Error Log as JSON and Logs it into the Laravel Error Logs
*
* @param \Exception $exception
* @param null|string $message
*
* @return string
*
* @author https://github.com/Zeitwaechter
*/
function error_logger(\Exception $exception, $message = null): string
{
if (null === $message) {
$message = '-';
}
$error_log = collect([
'message' => [
'custom' => $message,
'exception' => $exception->getMessage(),
],
'request' => [
request()->input(),
],
'routing' => [
null !== request()
->route()
? request()
->route()
->getName()
: request()->path(),
],
'stack_trace' => [
$exception->getTraceAsString()
],
'user' => [
'entity' => null !== auth()->user()
? auth()->user()->uuid
: auth()->user(),
'roles' => 'User',
],
'defined_paths' => [
'app' => app_path(),
'base' => base_path(),
'config' => config_path(),
'database' => database_path(),
'public' => public_path(),
'resource' => resource_path(),
'storage' => storage_path(),
],
]);
$error_log_json = $error_log->toJson();
\Log::error($error_log_json);
return $error_log_json;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.