Created
January 29, 2020 12:12
-
-
Save Zeitwaechter/55e3ca89de4d8190eaa6e5613c327637 to your computer and use it in GitHub Desktop.
Universal Laravel Error Logger Helper
This file contains 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
<?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