How can Celery configure submodules' loggers?

30 views Asked by At

My understanding is that @after_setup_logger.connect is supposed to allow you to attach custom handlers. However, this does not work for me.

@after_setup_logger.connect
def setup_loggers(logger: logging.Logger, *args, **kwargs):
    h = LogCollector()
    logger.addHandler(h)  # or even logger.getLogger().addHandler(h)


@app.task()
def add(x: int, y: int):
    result = x + y
    logger.info(f"Add: {x} + {y} = {result}")
    return result

Inside the add function, logger does not have handlers. What am I doing wrong?

1

There are 1 answers

0
Zeke Marffy On

I must've had other incorrect code. What I wrote here is fine and exactly how configuring logging with Celery is works.