Skip to content

Instantly share code, notes, and snippets.

@Zeitwaechter
Created January 29, 2020 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zeitwaechter/55e3ca89de4d8190eaa6e5613c327637 to your computer and use it in GitHub Desktop.
Save Zeitwaechter/55e3ca89de4d8190eaa6e5613c327637 to your computer and use it in GitHub Desktop.
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