This enables JSON logging for Celery workers if the LOG_JSON conig value is set. It uses the same JsonFormatter class used by the Flask applications. That class has been updated in two ways: - It takes a `source` kwarg to define the log source for the formatter. - The `msg` attribute of the log record is formatted with any arguments that may have been passed. This is necessary for Celery to render task type, completion time, etc. into the log output.
20 lines
509 B
Python
20 lines
509 B
Python
#!/usr/bin/env python
|
|
import logging
|
|
|
|
from atst.app import celery, make_app, make_config
|
|
from celery.signals import after_setup_task_logger
|
|
|
|
from atst.utils.logging import JsonFormatter
|
|
|
|
config = make_config()
|
|
app = make_app(config)
|
|
app.app_context().push()
|
|
|
|
|
|
@after_setup_task_logger.connect
|
|
def setup_task_logger(*args, **kwargs):
|
|
if app.config.get("LOG_JSON"):
|
|
logger = logging.getLogger()
|
|
for handler in logger.handlers:
|
|
handler.setFormatter(JsonFormatter(source="queue"))
|