从“cherrypy.access.173118160”中筛选日志记录,其中我需要使用“cherrypy.access.*”作为日志记录选项

2024-09-28 21:02:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如何配置Python3日志以使用通配符过滤所有内容?cherrypy.access为每个客户端连接使用随机值或线程id,如“cherrypy.access.173118160”。下面是我一直想弄清楚如何过滤“loggers”中的“cherrypy.access.*”,有人知道怎么做吗

import os
import logging
import logging.config

def getLogger(name):

    if loggers.get(name):
        return loggers.get(name)

    logging.config.dictConfig({
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': '%(asctime)s [%(levelname)-11.11s] [%(name)s] [%(threadName)-12.12s] [%(funcName)s:%(lineno)d] %(message)s'
            }
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'standard',
                'stream': 'ext://sys.stdout'
            },
            'rotateBytesFile': {
                'level': 'DEBUG',
                'class': 'logging.handlers.RotatingFileHandler',
                'formatter': 'standard',
                'filename': '%s/%s' % (os.getcwd(), '/logs/example.log'),
                'maxBytes': 10485760,
                'backupCount': 10
            }
        },
        'loggers': {
            '': {
                'level': 'DEBUG',
                'handlers': ['console', 'rotateBytesFile'],
                'propagate': False
            },
            'cherrypy.access': {
                'level': 'WARNING',
                'handlers': ['console', 'rotateBytesFile'],
                'propagate': False
            },
        }
    })

    return logging.getLogger(name)

Tags: namedebugimportconfigfalseaccessoslogging