TheoretiCAL
TheoretiCAL

Reputation: 20571

Prevent cherrypy from logging access

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?

Upvotes: 2

Views: 1333

Answers (3)

Haifeng Zhang
Haifeng Zhang

Reputation: 31895

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

Upvotes: 0

TheoretiCAL
TheoretiCAL

Reputation: 20571

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

Upvotes: 1

Anand S Kumar
Anand S Kumar

Reputation: 90889

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)

Upvotes: 0

Related Questions