0
考虑以下代码:Python记录奇怪的行为
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...')
我缺少什么?
我会更新这个问题,看起来我在错误的方向看 – Mazyod 2014-10-06 08:43:07
@Mazyod:如果你这样做,请删除*编辑*标题;我们都可以看到编辑历史,无需在问题本身中对其进行评论。 :-) – 2014-10-06 08:44:02
好的,谢谢。没有标题更新。 – Mazyod 2014-10-06 08:46:31