【发布时间】:2014-10-06 08:11:00
【问题描述】:
考虑这段代码:
ubuntu_logger = logging.getLogger('ubuntu-logger')
mail_handler = MailHandler()
mail_handler.setLevel(logging.INFO)
ubuntu_logger.addHandler(mail_handler)
filepath = "/home/ubuntu/logs/central.log"
formatter = logging.Formatter('[%(asctime)s - %(name)s - %(levelname)s]: %(message)s')
central_handler = logging.handlers.RotatingFileHandler(
filename=filepath,
mode="a+"
)
central_handler.setLevel(logging.DEBUG)
central_handler.setFormatter(formatter)
ubuntu_logger.addHandler(central_handler)
我在 serverutils.logutils 中创建了这个处理程序,这是一个自定义 python 模块。然后,我将它导入到我的守护进程服务脚本中,该脚本由root 用户运行:
from serverutils.logutils import ubuntu_logger as logger, DEFAULT_LOGGING_CONFIG
logger.info('pydaemons launching...')
使用上面的代码,ubuntu_logger 根本什么都不做。更改如下代码后,ubuntu_logger 可以正常工作,除了根记录器:
import logging
from serverutils.logutils import ubuntu_logger as logger, DEFAULT_LOGGING_CONFIG
config = DEFAULT_LOGGING_CONFIG # Fancy format, log level DEBUG
config.update(filename='/home/ubuntu/test.log')
logging.basicConfig(**config)
logging.error('omg, this works')
logger.info('pydaemons launching...')
我错过了什么?
【问题讨论】:
标签: python ubuntu logging module daemon