Reputation: 43
I am just beginning with Laravel and found that whenever we get any error / exception then Laravel itself appends stacktrace results in log file.
Is there any way to stop it as I don't need it at all and it is unnecessarily increasing the log file size on production server.
Please help me to get rid off from this issue. Thanks in advance !!
Upvotes: 3
Views: 6750
Reputation: 4074
Override report() in app/Exceptions/Handler.php
.
Here is what I use in my projects.
use \Illuminate\Support\Facades\Log;
public function report(Exception $exception)
{
// parent::report($exception);
if ($this->shouldntReport($exception)) {
return;
}
// Remove stack-trace when not debugging.
if (!config('app.debug')) {
Log::error(
sprintf(
"\n\r%s: %s in %s:%d\n\r",
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
)
);
// 'trace' => $exception->getTraceAsString(),
}
}
And I set APP_DEBUG=false
in production.
Upvotes: 4
Reputation: 399
Somebody might be strolling by and find this useful, so here is my contribution. This is how I handled this issue.
First, I created a helper function called logAnException
for whenever I want to handle an exception with try-catch
, e.g.
try {
// do some work
return redirect()->back();
}
catch (\Exception $exception) {
// USAGE EXAMPLE
logAnException($exception);
return redirect()->back()->withInput( $request->except('_token') );
}
logAnException
looks something like this...
function logAnException(\Exception $exception) {
$exceptionFormat = "\nAppName-EXCEPTION \nMESSAGE:: %s \nFILE:: %s \nLINE::%s \n\n";
\Illuminate\Support\Facades\Log::info(sprintf($exceptionFormat,
// some exceptions don't come with a message
!empty(trim($exception->getMessage()))
? $exception->getMessage()
: get_class($exception),
$exception->getFile(),
$exception->getLine()
));
}
Error Logging in Laravel can be overridden in the report
method of the App\Exceptions\Handler
class. So, I just replaced the legacy parent::report($exception);
with logAnException($exception);
like so
public function report(Exception $exception)
{
// parent::report($exception);
logAnException($exception);
}
With this, exceptions are now rendered in about just five line.
Upvotes: 0
Reputation: 60058
Yes - you can override the App::error()
filter with something like this:
App::error(function(Exception $exception, $code)
{
Log::error('This is the only error message that will appear in your logs');
if( ! (Config::get('app.debug')))
{
return Response::view('errors.error_page', array(), 500);
}
});
Upvotes: 4