使用Python将celery3.13日志写入syslog

时间:2014-08-11 11:29:08

标签: logging celery syslog

您可以帮我一个示例代码,帮助我使用Python将celery3.13日志写入syslog。

2 个答案:

答案 0 :(得分:2)

我知道这是一个老帖子 - 但我在研究同样的问题时偶然发现了它。我以为我会分享我们选择的方法。注意:我们改编了http://nanvel.name/weblog/celery-logging-syslog的材料。

将此代码放入app/__init__.py

import logging
from celery.signals import after_setup_logger, after_setup_task_logger

def setup_handler(sender=None, logger=None, loglevel=None,
       logfile=None, fmt=None, colorize=None, **kwds):
    r"""create a syslog handler"""
    handler = logging.handlers.SysLogHandler(address='/dev/log')
    handler.setFormatter(logging.Formatter(fmt))
    handler.setLevel(loglevel or logging.INFO)
    logger.addHandler(handler)

after_setup_logger.connect(setup_handler)
after_setup_task_logger.connect(setup_handler)

答案 1 :(得分:0)

请注意,芹菜4 fmt已重命名为格式。

import logging
from celery.signals import after_setup_logger, after_setup_task_logger

def setup_handler(sender=None, logger=None, loglevel=None,
   logfile=None, format=None, colorize=None, **kwds):
  r"""create a syslog handler"""
  handler = logging.handlers.SysLogHandler(address='/dev/log')
  handler.setFormatter(logging.Formatter(format))
  handler.setLevel(loglevel or logging.INFO)
  logger.addHandler(handler)

after_setup_logger.connect(setup_handler)
after_setup_task_logger.connect(setup_handler)