Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Celery: log each task run to it's own file?

I want each job running to log to it's own file in the logs/ directory where the filename is the taskid.

logger = get_task_logger(__name__)

@app.task(base=CallbackTask)
def calc(syntax):
    some_func()
    logger.info('started')

In my worker, I set the log file to output to by using the -f argument. I want to make sure that it outputs each task to it's own log file.

like image 243
user299709 Avatar asked Aug 13 '14 08:08

user299709


2 Answers

Seems like I am 3 years late. Nevertheless here's my solution inspired from @Mikko Ohtamaa idea. I just made it little different by using Celery Signals and python's inbuilt logging framework for preparing and cleaning logging handle.

from celery.signals import task_prerun, task_postrun
import logging

# to control the tasks that required logging mechanism
TASK_WITH_LOGGING = ['Proj.tasks.calc']
@task_prerun.connect(sender=TASK_WITH_LOGGING)
def prepare_logging(signal=None, sender=None, task_id=None, task=None, args=None, kwargs=None)
    logger = logging.getLogger(task_id)
    formatter = logging.Formatter('[%(asctime)s][%(levelname)s] %(message)s')
    # optionally logging on the Console as well as file
    stream_handler = logging.StreamHandler()
    stream_handler.setFormatter(formatter)
    stream_handler.setLevel(logging.INFO)
    # Adding File Handle with file path. Filename is task_id
    task_handler = logging.FileHandler(os.path.join('/tmp/', task_id+'.log'))
    task_handler.setFormatter(formatter)
    task_handler.setLevel(logging.INFO)
    logger.addHandler(stream_handler)
    logger.addHandler(task_handler)

@task_postrun.connect(sender=TASK_WITH_LOGGING)
def close_logging(signal=None, sender=None, task_id=None, task=None, args=None, kwargs=None, retval=None, state=None):
    # getting the same logger and closing all handles associated with it
    logger = logging.getLogger(task_id)
    for handler in logger.handlers:
        handler.flush()
        handler.close()
    logger.handlers = []

@app.task(base=CallbackTask, bind=True)
def calc(self, syntax):
    # getting logger with name Task ID. This is already
    # created and setup in prepare_logging
    logger = logging.getLogger(self.request.id)
    some_func()
    logger.info('started')

The bind=True is necessary here in order to have id available within task. This will create individual log file with <task_id>.log every time the task calc is executed.

like image 113
Saurabh Avatar answered Sep 30 '22 02:09

Saurabh


Below is my crude, written out-of-my-head, untested approach. Think it more as guidelining than production-grade code.

def get_or_create_task_logger(func):
    """ A helper function to create function specific logger lazily. """

    # https://docs.python.org/2/library/logging.html?highlight=logging#logging.getLogger
    # This will always result the same singleton logger
    # based on the task's function name (does not check cross-module name clash, 
    # for demo purposes only)
    logger = logging.getLogger(func.__name__)

    # Add our custom logging handler for this logger only
    # You could also peek into Celery task context variables here
    #  http://celery.readthedocs.org/en/latest/userguide/tasks.html#context
    if len(logger.handlers) == 0:
        # Log to output file based on the function name
        hdlr = logging.FileHandler('%s.log' % func.__name__)
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
        hdlr.setFormatter(formatter)
        logger.addHandler(hdlr) 
        logger.setLevel(logging.DEBUG)

    return logger

@app.task(base=CallbackTask)
def calc(syntax):
    logger = get_or_create_task_logger(calc)
    some_func()
    logger.info('started')
like image 40
Mikko Ohtamaa Avatar answered Sep 30 '22 02:09

Mikko Ohtamaa