CakePHP Error Log - Can I Eliminate 404 Errors?

The error log of my CakePHP application is filled with 404 errors. Can I exclude these MissingControllerExceptionfrom errors appearing in the log? Using the cake 2.3.

+4
source share
4 answers

Simply redirecting or removing these URLs will not be reduced.

A busy site hits hundreds of “random” 404s daily, most of the Far East countries check exploits or URLs such as “/ wp-admin”.

Writing this data with a full stack trace is completely unnecessary

Decision

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

app/Config/core.php , :

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

app/Lib/Error App::uses app/Config/bootstrap.php :

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

ErrorHandler, - handleException , , . :

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...
    }

}
+8

robmcvey, CakePHP 2.6.

app/Config/core.php :

Configure::write('Exception', array(
    'handler' => 'AppErrorHandler::handleException',
    'renderer' => 'ExceptionRenderer',
    'log' => true
));

app/Config/bootstrap.php CakeLog:

CakeLog::config('not_found', array(
    'engine' => 'FileLog',
    'types' => array('404'),
    'file' => '404',
));

type of 404 - , , - , . CakeLog::write('404', 'That was not found.');

ErrorHandler

app/Lib/Error/AppErrorHandler.php. Cake ErrorHandler, ; handleException(), _getMessage() _log().

<?php
class AppErrorHandler extends ErrorHandler {

/**
 * List of Cake Exception classes to record to specified log level.
 *
 * @var array
 */
    protected static $_exceptionClasses = array(
        'MissingControllerException' => '404',
        'MissingActionException' => '404',
        'PrivateActionException' => '404',
        'NotFoundException' => '404'
    );

    public static function handleException(Exception $exception) {
        $config = Configure::read('Exception');
        self::_log($exception, $config);

        $renderer = isset($config['renderer']) ? $config['renderer'] : 'ExceptionRenderer';
        if ($renderer !== 'ExceptionRenderer') {
            list($plugin, $renderer) = pluginSplit($renderer, true);
            App::uses($renderer, $plugin . 'Error');
        }
        try {
            $error = new $renderer($exception);
            $error->render();
        } catch (Exception $e) {
            set_error_handler(Configure::read('Error.handler')); // Should be using configured ErrorHandler
            Configure::write('Error.trace', false); // trace is useless here since it internal
            $message = sprintf("[%s] %s\n%s", // Keeping same message format
                get_class($e),
                $e->getMessage(),
                $e->getTraceAsString()
            );

            self::$_bailExceptionRendering = true;
            trigger_error($message, E_USER_ERROR);
        }
    }

/**
 * Generates a formatted error message
 *
 * @param Exception $exception Exception instance
 * @return string Formatted message
 */
    protected static function _getMessage($exception) {
        $message = '';
        if (php_sapi_name() !== 'cli') {
            $request = Router::getRequest();
            if ($request) {
                $message .= $request->here() . " Not Found";
            }
        }
        $message .= "\nStack Trace:\n" . $exception->getTraceAsString() . "\n";
        return $message;
    }

/**
 * Handles exception logging
 *
 * @param Exception $exception The exception to render.
 * @param array $config An array of configuration for logging.
 * @return bool
 */
    protected static function _log(Exception $exception, $config) {
        if (!empty(self::$_exceptionClasses)) {
            foreach ((array)self::$_exceptionClasses as $class => $level) {
                if ($exception instanceof $class) {
                    return CakeLog::write($level, self::_getMessage($exception));
                }
            }
        }
        return parent::_log();
    }
}

$_exceptionClasses, , , . _getMessage() .

URL-, /exploitable-plugin, tmp/logs/404.log.

2015-04-01 16:37:54 404: /exploitable-plugin Not Found
Stack Trace:
#0 /var/example.com/app/index.php(146): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#1 {main}
+6

2.5.6 (, , ) .

:

Configure::write('Exception', array(
    'handler' => 'ErrorHandler::handleException',
    'renderer' => 'ExceptionRenderer',
    'log' => true,
    'skipLog'=>array(
        'MissingControllerException'
    )
));

skipLog .

+5

, . - , - Google https://www.google.com/webmasters/tools/removals. . , .

, CakePHP: https://www.google.com/webmasters/tools/removals?pli=1

Edit:

, , . bootstrap.php:

$matches = array();
preg_match('/^\/(.+?)\//', $_SERVER['REQUEST_URI'],$matches);
if(!App::import('Controller', $matches[1])){
   die('404 Error![some_random_chars_so_you_can_easy_find_it_in_the_future]') ;
}
0

Source: https://habr.com/ru/post/1533142/


All Articles