CakePHP error log - can I exclude 404 errors?

cakephp, cakephp-2.0

Solution

Simply redirecting or removing these URLs is not going to cut it.

A busy site will get hit by hundreds of "random" 404s every day, most from far east countries checking for exploits or URLs such as "/wp-admin".

Logging these with a full stack trace is completely unnecessary

Solution

You can override the default error handler in CakePHP, and log to `app/tmp/logs/404.log` instead.

In your `app/Config/core.php` file, define the class you want to handle exceptions:

Configure::write('Error', array(
    'handler' => 'MyCustomErrorHandler::handleError',
    'level' => E_ALL & ~E_DEPRECATED,
    'trace' => true
));

Create the class within `app/Lib/Error` and include using `App::uses` in your `app/Config/bootstrap.php` file:

App::uses('MyCustomErrorHandler', 'Lib/Error');

Make an exact copy of the original ErrorHandler class, just change the class name, and somewhere within the handleException method check which exception is being thrown, and log somewhere else. It will look a bit like this;

App::uses('ErrorHandler', 'Error');

class MyCustomErrorHandler {

    public static function handleException(Exception $exception) {

         // some code...

         if (in_array(get_class($exception), array('MissingControllerException', 'MissingActionException', 'PrivateActionException', 'NotFoundException'))) {
             $log = '404';
             $message = sprintf("[%s]", get_class($exception));
         }

         // more code...
    }

}

Problem

The error log of my CakePHP app is full of 404 errors. Can I exclude these `MissingControllerException`s from appearing in the error log? Using Cake 2.3.

Original source