Django Celery Logging Best Practice

前端 未结 4 1176
暗喜
暗喜 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:24

    When your logger initialized in the beginning of "another module" it links to another logger. Which handle your messages. It can be root logger, or usually I see in Django projects - logger with name ''.

    Best way here, is overriding your logging config:

    LOGGING = {
        'version': 1,
        'disable_existing_loggers': True,
        'formatters': {
            'simple': {
                'format': '%(levelname)s %(message)s',
                 'datefmt': '%y %b %d, %H:%M:%S',
                },
            },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'simple'
            },
            'celery': {
                'level': 'DEBUG',
                'class': 'logging.handlers.RotatingFileHandler',
                'filename': 'celery.log',
                'formatter': 'simple',
                'maxBytes': 1024 * 1024 * 100,  # 100 mb
            },
        },
        'loggers': {
            'celery': {
                'handlers': ['celery', 'console'],
                'level': 'DEBUG',
            },
        }
    }
    
    from logging.config import dictConfig
    dictConfig(LOGGING)
    

    In this case I suppose it should work as you assume.

    P.S. dictConfig added in Python2.7+.

提交回复
热议问题