Good news is, you can do this with a simple trick.
And no task id when validate_email is called from within Flask. What you do want is to get the Celery task id in the log message when validate_email is called from within a Celery task. You do not want to pollute it with a Celery-specific logger implementation. Your lower-level code should not care in which context it runs. info ( f'Validate email address: ' ) if value is not None : assert in value return value. String ( 256 ), unique = True, nullable = False ) validates ( 'email' ) def validate_email ( self, key, value ): logger. String ( 64 ), unique = False, nullable = True ) email = db. Column ( UUID ( as_uuid = True ), primary_key = True, server_default = text ( "uuid_generate_v4()" )) name = db. Import logging from passlib.hash import sha256_crypt from import UUID from sqlalchemy.orm import validates from sqlalchemy import text from. is an extension of logging.Formatter and gets a reference to the current Celery task at runtime (check out the source code if you want to take a deeper dive). There is one gotcha: In order to get access to task_id and task_name, you have to use instead of logging.Formatter. This is the signal we want to connect to in order to customise the log formatter. The after_setup_task_logger signal gets triggered as soon as Celery worker has set up the celery.task logger. How do you customise the celery.task log message format? Remember how you customise the Celery logger using the after_setup_logger signal? There is a similar signal for the celery.task logger.
#How to make a new file from a celery task how to#
How to customise the celery.task log format