P
P
Pavel2018-01-14 09:47:58
Flask
Pavel, 2018-01-14 09:47:58

Flask, Celery and RotatingFileHandler - how to make friends?

The same code is called to initialize App and Celery:

# -*- coding: utf-8 -*-
"""Create an application instance."""

from flask.helpers import get_debug_flag

from appdata.app import create_app
from appdata.settings import DevConfig, ProdConfig
from appdata.extensions import celery
from appdata.utils import init_celery

CONFIG = DevConfig if get_debug_flag() else ProdConfig

app = create_app(CONFIG)
init_celery(app, celery)

Docker
for App
command: /usr/local/bin/gunicorn -w 1 -b :5000 autoapp:app --reload --log-level=info
for Celery
command: celery worker -n worker_order --app=autoapp.celery --loglevel=info
But App has logger initialization:
def register_logger(app):
    """Register application logger."""
    import os.path
    if not os.path.exists("logs/"):
        os.makedirs("logs/")

    handler = RotatingFileHandler('logs/main.log', maxBytes=10000000, backupCount=5)
    handler.setFormatter(
        logging.Formatter('%(asctime)s - %(name)s:{%(pathname)s:%(lineno)d} - %(levelname)s - %(message)s')
    )
    app.logger.addHandler(handler)

    app.logger.setLevel(logging.INFO)

And this logger is first initialized for App, and then it tries for Celery and a natural bummer occurs.
How to make a separate initialization of the logger for Celery and write to the same handler?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question