I'm running a Celery task that executes a function. This function generates some logging information. Using the get_task_logger logger, I am able print the logging information to the Celery stdout.
from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
def my_func_called_inside_a_task():
logger.debug("SOME OUTPUT HERE")
However, I also want to import this function as a normal python script (not using Celery) and log to, for example, stdout. Normally, I might do something like the following:
import logging
logger = logging.getLogger(__name__)
def my_func_called_inside_a_task():
logger.debug("SOME OUTPUT HERE")
How do I combine both approaches so I don't have to do something redundant like the following?
import logging
from celery.utils.log import get_task_logger
logger = logging.getLogger(__name__)
logger_celery = get_task_logger(__name__)
def my_func_called_inside_a_task():
logger.debug("SOME OUTPUT HERE")
logger_celery.debug("SOME OUTPUT HERE")
Summary: If I call the function from a celery task, I'd like it to log to the celery worker stdout. If I call the function from a normal Python prompt, it would use the normal Python logger. Any help is much appreciated.
You can pass an optional argument to that function.
import logging
from celery.utils.log import get_task_logger
def my_func_called_inside_a_task(use_celery_logger=None):
if use_celery_logger:
logger = get_task_logger(__name__)
else:
logger = logging.getLogger(__name__)
logger.debug("SOME OUTPUT HERE")
and in Your celery task call it as
my_func_called_inside_a_task(use_celery_logger=True)
and for normal logging you can call it as it is
my_func_called_inside_a_task()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With