Prevent cherrypy from logging access

cherrypy logging
cherrypy engine
cherrypy example
cherrypy expose
cherrypy github
cherrypy stop server
cherrypy example project
cherrypy routes

I'm trying to set up a logger to only catch my ERROR level messages, but my logger always seems to write INFO:cherrypy.access messages to my log file, which I dont want. I tried setting the log.error_file global, and using the standard python logging module logging.basicConfig(filename='error.log', filemode='w', level=logging.ERROR), but even though I specify the threshold to be ERROR level messages, I still get those INFO level messages written to my log file. Is there any way to prevent this?

Seems like the INFO level logs are coming for cherrypy.access . According to documentation -

You should set these at either the global level or per application (see next), but generally not both.

log.screen: Set this to True to have both "error" and "access" messages printed to stdout. log.access_file: Set this to an absolute filename where you want "access" messages written. log.error_file: Set this to an absolute filename where you want "error" messages written.

You should also try setting the log.access_file (similar to log.error_file) . Or you can add an handler for that, example -

from logging import handlers
fname = getattr(log, "rot_access_file", "access.log") #The log for access messsages.
h = handlers.TimedRotatingFileHandler(fname, when='midnight')
h.setLevel(logging.DEBUG)
h.setFormatter(_cplogging.logfmt)
log.access_file = ""
log.access_log.addHandler(h)

Basics, Logging. Disable logging. Play along with your other loggers. Configuring A web application needs an HTTP server to be accessed to. CherryPy provides its  Disabling File Logging in CherryPy : To disable file logging in CherryPy, simply set an empty string to the log.access_file or log.error_file keys in your global configuration. cherrypy.config.up

I can disable access messages by setting up my own:

logging.basicConfig(filename='error.log', filemode='w', level=logging.ERROR)

and then setting:

cherrypy.log.access_log.propagate = False

Log is redirecting to console even when cherrypy.log.screen is False , _cplogging.logfmt) # Add access log file to cherrypy configuration That is - can cherrypy code be changed to disable propagation in the  Since version 18.0.0 CherryPy has dropped support for Python 2 , but there's still LTS branch for v17 supporting hybrid Python 2 and 3 code, which will get bugfixes and security updates.

I had this issue as well and I figured it out. So post it here and hope it can help people had similar questions.

CherryPy version 14.2.0

To disable access logging, you can simply leave an empty string as 'log.access_file': ''

if __name__ == '__main__':
    cherrypy.config.update(
        {
            'server.socket_host': settings.HOST,
            'server.socket_port': settings.PORT,
            'log.screen': True,
            'log.access_file': '',
            'log.error_file': ''
        }
    )

    config = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.on': True,
            'tools.staticdir.dir': settings.PROJECT_ROOT
        }
    }

Below I post my customized logging configurations logging_configuration.py, I added custom logger into the settings:

# logger settings
LOGGER_NAME = 'custom'
_LOGGER_HANDLER = 'cherrypy_' + LOGGER_NAME
_MULTIMEDIA_LOGFILE = LOGGER_NAME + '.log'
_ACCESS_LOGFILE = 'access.log'
_ERRORS_LOGFILE = 'errors.log'

LOG_CONF = {
    'version': 1,
    'formatters': {
        'void': {
            'format': ''
        },
        'standard': {
            'format': '%(asctime)s (%(module)15s:%(lineno)2s) [%(levelname)s] %(message)s'
        },
    },
    'handlers': {
        _LOGGER_HANDLER: {
            'level':'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'standard',
            'filename': _MULTIMEDIA_LOGFILE,
            'maxBytes': 10485760,
            'backupCount': 20,
            'encoding': 'utf8'
        },
        'cherrypy_access': {
            'level':'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'void',
            'filename': _ACCESS_LOGFILE,
            'maxBytes': 10485760,
            'backupCount': 20,
            'encoding': 'utf8'
        },
        'cherrypy_error': {
            'level':'INFO',
            'class': 'logging.handlers.RotatingFileHandler',
            'formatter': 'void',
            'filename': _ERRORS_LOGFILE,
            'maxBytes': 10485760,
            'backupCount': 20,
            'encoding': 'utf8'
        },
    },
    'loggers': {
        LOGGER_NAME: {
            'handlers': [_LOGGER_HANDLER],
            'level': 'INFO',
            'propagate': False
        },
        'cherrypy.access': {
            'handlers': ['cherrypy_access'],
            'level': 'INFO',
            'propagate': False
        },
        'cherrypy.error': {
            'handlers': ['cherrypy_console', 'cherrypy_error'],
            'level': 'INFO',
            'propagate': False
        },
    }
}

How I use it?

import cherrypy
from docs import logging_configuration as loggingconf

if __name__ == '__main__':
    cherrypy.config.update(
        {
            'server.socket_host': settings.HOST,
            'server.socket_port': settings.PORT,
            'log.screen': True,
        }
    )

    cherrypy.engine.unsubscribe('graceful', cherrypy.log.reopen_files)
    logging.config.dictConfig(loggingconf.LOG_CONF)  # add it here

    config = {
        '/': {
            'tools.sessions.on': True,
            'tools.staticdir.on': True,
            'tools.staticdir.dir': settings.PROJECT_ROOT
        }
    }
    cherrypy.quickstart(CampPage(), '/', config)

To log into cherrypy_custom.log, use:

logger = logging.getLogger(loggingconf.LOGGER_NAME)

logger.info('Hello There...')  # this line will be written into cherrypy_custom.log

To disable access log, set _ACCESS_FILELOG = '' to empty string

cherrypy.log Python Example, This page provides Python code examples for cherrypy.log. HTTPError( 401, "​You are not authorized to access that resource") these hooks is # to raise SystemExit and stop the whole server. if exc is None or hook.failsafe: try: hook() except  .. seealso:: Refer to the :mod:`cherrypy._cplogging` module for more details about CherryPy's logging architecture. Disable logging. You may be interested in disabling either logs. To disable file logging, simply set a en empty string to the log.access_file or log.error_file keys in your :ref:`global configuration <globalsettings>`.

Prevent cherrypy from logging access - python, I'm trying to set up a logger to only catch my ERROR level messages, but my logger always seems to write INFO:cherrypy.access messages to my log file, which  The cherrypy.process.plugins.SimplePlugin is a helper class provided by CherryPy that will automatically subscribe your start and stop methods to the related channels. When the start and stop channels are published on, those methods are called accordingly.

[PDF] CherryPy Documentation, That log is harmless and will not prevent CherryPy from working. You can refer the top-level cherrypy.access logger to log requests into a file. CherryPy is a small framework that focuses on one single task: take a HTTP request and locate the most appropriate Python function or method that match the request’s URL. Unlike other well-known frameworks, CherryPy does not provide a built-in support for database access, HTML templating or any other middleware nifty features.

cherrypy._cplogging, "Simple" logging means that you can log to the screen (i.e. console/stdout) or to a file, and that you can easily have separate error and access log files. Here are  Now, however, I was forced to recognize that the CherryPy try…except exception catcher and its logging handlers must only get involved when invoking a controller inside of a real CherryPy app. If an HTTP request is instead being handed off to a WSGI application of my own devising, then CherryPy took no further responsibility for what happened

Comments
  • It's not very practical to configure CherryPy logs with bare logging. Make sure you understand CherryPy configuration and its environments (see this question). If you persist, this article has good explanation of CherryPy logging tree and ways to extend it.