防止 cherrypy 记录访问

Prevent cherrypy from logging access

我试图设置一个记录器来只捕获我的错误级别消息,但我的记录器似乎总是将 INFO:cherrypy.access 消息写入我的日志文件,这是我不想要的。我尝试设置 log.error_file 全局,并使用标准 python 日志记录模块 logging.basicConfig(filename='error.log', filemode='w', level=logging.ERROR),但即使我将阈值指定为 ERROR 级别消息,我仍然将那些 INFO 级别消息写入我的日志文件。有什么办法可以防止这种情况吗?

INFO 级别的日志似乎即将用于 cherrypy.access。根据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.

您还应该尝试设置 log.access_file(类似于 log.error_file)。或者您可以为此添加一个处理程序,例如 -

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)

我可以通过设置自己的方式来禁用访问消息:

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

然后设置:

cherrypy.log.access_log.propagate = False

我也有这个问题,我想通了。所以post就在这里,希望它能帮助有类似问题的人。

CherryPy 版本 14.2.0

要禁用 access 日志记录,您只需将空字符串保留为 '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
        }
    }

下面我 post 我的自定义日志记录配置 logging_configuration.py,我在设置中添加了自定义记录器:

# 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
        },
    }
}

如何使用它?

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)

要登录 cherrypy_custom.log,请使用:

logger = logging.getLogger(loggingconf.LOGGER_NAME)

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

要禁用访问日志,请将 _ACCESS_FILELOG = '' 设置为空字符串