简体   繁体   English

Django芹菜任务记录

[英]Django Celery Task Logging

I have setup Celery in a Django project on which I am working. 我在我正在工作的Django项目中设置了Celery。 I would like to separate the logging for celery tasks vs the remainder of celery logs (celerycam, celerybeat, etc). 我想将芹菜任务的记录与芹菜原木的剩余部分(celerycam,celerybeat等)分开。

Based on the Celery documentation ( http://docs.celeryproject.org/en/latest/userguide/tasks.html#logging ) it seems like I should just be able to define a Django logger for 'celery.task' which should do this. 基于Celery文档( http://docs.celeryproject.org/en/latest/userguide/tasks.html#logging ),似乎我应该能够为'celery.task'定义一个Django记录器,它应该做什么这个。 However, when I do this, nothing shows up in the logs. 但是,当我这样做时,日志中没有任何内容。 Everything does show up if I create a generic 'celery' logger, implying that this may be something to do with the name of the logger. 如果我创建一个通用的“芹菜”记录器,一切都会显示出来,暗示这可能与记录器的名称有关。

What am I missing here? 我在这里错过了什么? Is there a way to make this work, or must all celery logs go together? 有没有办法让这项工作,或所有芹菜日志一起去?

For what it's worth, I have set CELERYD_HIJACK_ROOT_LOGGER = False. 为了它的价值,我设置了CELERYD_HIJACK_ROOT_LOGGER = False。

My logging setup in settings.py: 我在settings.py中的日志记录设置:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'standard': {
            'format': '%(asctime)s %(levelname)s [%(name)s: %(lineno)s] -- %(message)s',
            'datefmt': '%m-%d-%Y %H:%M:%S'
        },
    },
    'handlers': {
        'logfile': {
            'level': 'INFO',
            'filters': None,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/vagrant/logs/logfile.log',
            'maxBytes': 1024*1024*5,
            'backupCount': 3,
            'formatter': 'standard'
        },
        'debug_logfile': {
            'level': 'DEBUG',
            'filters': None,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/vagrant/logs/debug_logfile.log',
            'maxBytes': 1024*1024*5,
            'backupCount': 5,
            'formatter': 'standard'
        },
        'default_logger': {
            'level': 'WARNING',
            'filters': None,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/vagrant/logs/default.log',
            'maxBytes': 1024*1024*5,
            'backupCount': 2,
            'formatter': 'standard'
        },
        'celery_logger': {
            'level': 'DEBUG',
            'filters': None,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/vagrant/logs/celery.log',
            'maxBytes': 1024*1024*5,
            'backupCount': 2,
            'formatter': 'standard'
        },
        'celery_task_logger': {
            'level': 'DEBUG',
            'filters': None,
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': '/vagrant/logs/celery_tasks.log',
            'maxBytes': 1024*1024*5,
            'backupCount': 2,
            'formatter': 'standard'
        },
    },
    'loggers': {
        '': {
            'handlers': ['default_logger'],
            'level': 'WARNING',
            'propagate': True,
        },
        'django': {
            'handlers': ['logfile'],
            'level': 'INFO',
            'propagate': True,
        },
        'feedmanager': {
            'handlers': ['logfile', 'debug_logfile'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'recipemanager': {
            'handlers': ['logfile', 'debug_logfile'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'menumanager': {
            'handlers': ['logfile', 'debug_logfile'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'celery.task': {
            'handlers': ['celery_task_logger'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'celery': {
            'handlers': ['celery_logger'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

i'm guessing in your tasks your doing this 我在你的任务中猜测你这样做

from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)

if your doing that then your logger name will be the module name, so if your task is in a tasks.py file in an app called MyApp your logger will be named 'MyApp.tasks' and you'd have to create the logger 'MyApp.tasks' in your settings. 如果您这样做,那么您的记录器名称将是模块名称,因此如果您的任务位于名为MyApp的应用程序中的tasks.py文件中,您的记录器将被命名为“MyApp.tasks”并且您必须创建记录器' MyApp.tasks'在您的设置中。

You can just put a different string in place of __name__ for all of the tasks to log to same logger if you have them all over place. 您可以为所有任务添加一个不同的字符串代替__name__,如果您将它们全部放在一起,则可以记录到同一个记录器。 ie: 'celery.task' 即:'celery.task'

oh and make sure your worker loglevel is to what you want it to be 哦,并确保你的工作人员loglevel是你想要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM