I’m wondering how to setup a more specific logging system. All my tasks use
logger = logging.getLogger(__name__)
as a module-wide logger.
I want celery to log to “celeryd.log” and my tasks to “tasks.log” but I got no idea how to get this working. Using CELERYD_LOG_FILE from django-celery I can route all celeryd related log messages to celeryd.log but there is no trace of the log messages created in my tasks.
Note: This answer is outdated as of Celery 3.0, where you now use
get_task_logger()to get your per-task logger set up. Please see the Logging section of the What’s new in Celery 3.0 document for details.Celery has dedicated support for logging, per task. See the Task documentation on the subject:
Under the hood this is all still the standard python logging module. You can set the
CELERYD_HIJACK_ROOT_LOGGERoption to False to allow your own logging setup to work, otherwise Celery will configure the handling for you.However, for tasks, the
.get_logger()call does allow you to set up a separate log file per individual task. Simply pass in alogfileargument and it’ll route log messages to that separate file:Last but not least, you can just configure your top-level package in the python logging module and give it a file handler of it’s own. I’d set this up using the
celery.signals.after_setup_task_loggersignal; here I assume all your modules live in a package calledfoo.tasks(as infoo.tasks.emailandfoo.tasks.scaling):Now any logger whose name starts with
foo.taskswill have all it’s messages sent totasks.loginstead of to the root logger (which doesn’t see any of these messages because.propagateis False).