1

我正在尝试使用他们的 Logging API 将 celery 任务日志推送到 GCP。我也在使用google.cloud.logging.handlers.CloudLoggingHandler

这是我的做法:

设置.py

from google.cloud import logging as gcp_logging
gcp_client = gcp_logging.Client()
gcp_client.setup_logging()
LOGGING_FOLDER = ''
LOG_NAME = 'custom-logs'
LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        }
    },
    'handlers': {
        'gcp_log': {
            'level': 'DEBUG',
            'class': 'google.cloud.logging.handlers.CloudLoggingHandler',
            'client': gcp_client,
            'name': LOG_NAME,
            'formatter': 'standard',
        },
        'celery': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(LOGGING_FOLDER, 'celery.log'),
            'maxBytes': 1024 * 1024 * 100,  # 100 MB
            'backupCount': 15,
            'formatter': 'standard',
        }
    },
    'loggers': {
        'myapp.tasks':{
            'handlers': ['celery', 'gcp_log'],
            'level': 'DEBUG',
            'propagate': True
        },
        'celery.worker':{
            'handlers': ['celery', 'gcp_log'],
            'level': 'DEBUG',
            'propagate': False
        },
    },
}

我的应用程序/tasks.py

from celery.utils.log import get_task_logger
logger = get_task_logger(__name__)
@task
def add(x, y):
    print("Add - I am from Print ")
    logger.info("Add - I am from logger")
    return x + y

所有日志都出现在 celery 处理程序中,即 celery.log 文件中,但只有工作日志出现在 GCP 上。myapp.tasks日志不会上传到 GCP。

我的应用程序目前是基于 django 的,但我也在 vanilla Python 中尝试过。仍然是相同的行为。

我遵循了https://www.distributedpython.com/2018/08/28/celery-logging/中描述的方法 1, 但它也产生了相同的结果。我正在做任何配置失误吗?

4

1 回答 1

0

几周前我遇到了类似的问题,但记录到 splunk。这是我的答案:如果我使用 celery 作为我的任务调度程序,如何从我的 python 应用程序登录到 splunk?

我认为问题可能出在您设置的位置:

logger = get_task_logger(__name__)

这可能会生成 settings.py 文件中未提及的记录器。也许获取一个您在 settings.py 文件中专门设置的记录器及其处理程序。但是我个人只是设置了自己的记录器,就像您在上面的链接答案中看到的那样。我希望这可以帮助你一点...

于 2019-01-23T12:01:48.047 回答