How to capture stdout of remote celery worker?

2019-03-06 04:09发布

问题:

I have a setup with multiple remote Celery workers. In the main program which is starting tasks via apply_async I need continuous access to stdout of active remote workers.

Is it possible using only Celery API?

回答1:

I think you should set up your SysLogHandler. CELERY_REDIRECT_STDOUTS and CELERY_REDIRECT_STDOUTS_LEVEL are working by default with current logger.

You can try example so (on remote servert):

import logging
from logging.handlers import SysLogHandler

logger = logging.getLogger()
logger.setLevel(logging.INFO)
# host is your server that used for the log
syslog = SysLogHandler(address=(host, port))
formatter = logging.Formatter('%(name)s: %(levelname)s %(message)s')
syslog.setFormatter(formatter)
logger.addHandler(syslog)

# check it
logger.info("Test test")