我在 wsgi (web2py) 应用程序的每个请求上运行以下代码:
import logging, logging.handlers
from logging import StreamHandler, Formatter
def get_configured_logger(name):
logger = logging.getLogger(name)
if (len(logger.handlers) == 0):
# This logger has no handlers, so we can assume it hasn't yet been configured (Django uses similiar trick)
# === Configure logger ===
# Create Formatted StreamHandler:
FORMAT = "%(process)s %(thread)s: %(message)s"
formatter = logging.Formatter(fmt=FORMAT)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.debug('CONFIGURING LOGGER')
return logger
# Get app specific logger:
logger = get_configured_logger(request.application)
logger.debug("TEST")
这意味着使用我想要的格式化处理程序配置一次记录器。它有效,除了我的标准输出中有双重条目:
81893 4329050112: CONFIGURING LOGGER
DEBUG:dummy:CONFIGURING LOGGER
81893 4329050112: TEST
DEBUG:dummy:TEST
如何使用我的新格式化处理程序并摆脱/隐藏默认处理程序?