title: Celery logger configuration
body: <p>I'm using Django 1.10, python 3.5 and celery 4.1.0
  I'm trying to log celery tasks info into a file. So I tried as suggested in celery documentation -</p>
  
  <pre><code>from celery.utils.log import get_task_logger
  
  logger = get_task_logger(__name__)
  </code></pre>
  
  <p>and tried to log a message inside the task -</p>
  
  <pre><code>logger.info(message)
  </code></pre>
  
  <p>I expected it to log to my default logger. But it didn't. So I added to settings a dedicated logger named 'celery.task' (as I understand from documentation):</p>
  
  <pre><code>LOGGING = {
  'version': 1,
  'disable_existing_loggers': False,
  'filters': {
      'require_debug_false': {
          '()': 'django.utils.log.RequireDebugFalse',
      },
      'require_debug_true': {
          '()': 'django.utils.log.RequireDebugTrue',
      },
      'require_test_false': {
          '()': 'myapp.utils.classes.logging.RequireTestFalse',
      },
      'suppress_deprecated': {
          '()': 'myapp.utils.classes.logging.SuppressDeprecated'
      }
  },
  'handlers': {
      'console': {
          'level': 'INFO',
          'class': 'logging.StreamHandler',
          'formatter': 'json',
          'filters': ['suppress_deprecated']
      },
      'celery_file': {
          'level': 'INFO',
          'class': 'myapp.utils.classes.logging.SBRotatingFileHandler',
          'maxBytes': 1024 * 1024 * 200,  # 200 MB
          'backupCount': 10,
          'formatter': 'json',
          'filename': BASE_DIR + '/../log/celery.log',
      }
  },
  'loggers': {
      'django': {
          'handlers': ['console', 'file'],
          'level': LOG_LEVEL,
          'propagate': True,
      },
      'celery.task': {
          'handlers': ['console', 'celery_file'],
          'level': 'INFO',
          'propagate': True,
              },
  }
  </code></pre>
  
  <p>But I still don't see logs from celery task not in the celery.log file nor in the default log file.</p>
  
  <p>Only when starting celery worker with '-f'  - it writes logs to that file
  Any ideas?</p>
  
  <p>EDIT:
  I'm trying to use 'after_setup_task_logger' to update the celery.task logger handler to use a handler that exists in my logging.config (in settings) unsuccessfully.
  I've tried the following:</p>
  
  <pre><code> @celery.signals.after_setup_task_logger.connect
   def after_setup_logging(logger, **kwargs):
       logging_settings = settings.LOGGING
       celery_handler = logging_settings['handlers']['celery_file']
       logger.addHandler(celery_handler)
  </code></pre>
  
  <p>But that doesn't work. I'm getting </p>
  
  <pre><code>AttributeError: 'dict' object has no attribute 'createLock'
  </code></pre>
  
  <p>Which means the handler was not properly created. 
  So I tried getting the handler from 'logging' object. But I don't see my handler in both logging._handlers and logging._handlersList</p>
  
  <p>UPDATE:
  That's what finally worked for me:</p>
  
  <pre><code>def create_celery_logger_handler(logger, propagate):
      # 209715200 is 1024 * 1024 * 200 or 200 MB, same as in settings
      celery_handler = RotatingFileHandler(
          settings.CELERY_LOG_FILE,
          maxBytes=209715200,
          backupCount=10
      )
      celery_formatter = jsonlogger.JsonFormatter(settings.LOGGING['formatters']['json']['format'])
      celery_handler.setFormatter(celery_formatter)
  
      logger.addHandler(celery_handler)
      logger.logLevel = settings.LOG_LEVEL
      logger.propagate = propagate
  
  
  @celery.signals.after_setup_task_logger.connect
  def after_setup_celery_task_logger(logger, **kwargs):
      """ This function sets the 'celery.task' logger handler and formatter """
      create_celery_logger_handler(logger, True)
  
  
  @celery.signals.after_setup_logger.connect
  def after_setup_celery_logger(logger, **kwargs):
      """ This function sets the 'celery' logger handler and formatter """
      create_celery_logger_handler(logger, False)
  </code></pre>