Django Celery Logging Best Practice

前端 未结 4 1178
暗喜
暗喜 2020-12-02 09:33

I\'m trying to get Celery logging working with Django. I have logging set-up in settings.py to go to console (that works fine as I\'m hosting on

4条回答
  •  日久生厌
    2020-12-02 10:25

    To fix duplicate logging issue, what worked for me is to set the propagate setting to false when declaring my settings.LOGGING dict

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose'
            },
        },
        'formatters': {
            'verbose': {
                'format': '%(asctime)s %(levelname)s module=%(module)s, '
                'process_id=%(process)d, %(message)s'
            }
        },
        'loggers': {
            'my_app1': {
                'handlers': ['console'],
                'level': 'DEBUG',
                'propagate': False #this will do the trick
            },
            'celery': {
                'handlers': ['console'],
                'level': 'DEBUG',
                'propagate': True
            },
        }
    }
    

    lets say your django project layout looks like:
    my_project/
    - tasks.py
    - email.py

    and lets say one of your tasks makes a call to some function in email.py; the logging will happen in email.py and then that logging will get propagated to the 'parent' which in this case happens to be your celery task. Thus double logging. But setting propagate to False for a particular logger means that for that logger/app, its logs wont get propagated to the parent, hence their will be no 'double' logging. By default 'propagate' is set to True

    Here's a link to the django docs section about that parent/children loggers stuff

提交回复
热议问题