简体   繁体   English

无法将Celery任务日志推送到Google Cloud(堆栈驱动程序)

[英]Unable to push Celery task logs to Google Cloud (stackdriver)

I'm trying to push celery task logs to GCP using their Logging API. 我正在尝试使用其Logging API将celery任务日志推送到GCP。 I'm using google.cloud.logging.handlers.CloudLoggingHandler for the same. 我正在使用google.cloud.logging.handlers.CloudLoggingHandler相同。

Here is how I'm doing it: 这是我的做法:

settings.py settings.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
        },
    },
}

myapp/tasks.py myapp / 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

All the logs, appear in celery handler ie celery.log file but only worker logs appear on GCP. 所有日志都出现在celery处理程序中,即celery.log文件中,但只有工作日志出现在GCP上。 myapp.tasks logs doesn't get uploaded to GCP. myapp.tasks日志不会上传到GCP。

My app is currently django based but I've also tried it in vanilla Python. 我的应用程序当前基于django,但我也在香草Python中尝试过。 Still the same behaviour. 仍然是相同的行为。

I followed the method 1 described in https://www.distributedpython.com/2018/08/28/celery-logging/ but it also yielded same results. 我遵循https://www.distributedpython.com/2018/08/28/celery-logging/中描述的方法1,但它也产生了相同的结果。 Is there any configuration miss I'm doing? 我正在做任何配置小姐吗?

I encountered a similar problem a few weeks ago but with logging to splunk. 几周前,我遇到了类似的问题,但日志记录不全。 Here is my answer: How can I log from my python application to splunk, if I use celery as my task scheduler? 这是我的答案: 如果我将celery用作任务计划程序,如何从python应用程序登录到splunk?

I think that the problem could be where you set: 我认为问题可能出在您的位置:

logger = get_task_logger(__name__)

This might generate a logger that is not mentioned in the settings.py file. 这可能会生成settings.py文件中未提及的记录器。 Maybe grab a logger that you specificaly set up in your settings.py file with its handlers. 也许抓取一个您专门在settings.py文件中设置的记录程序及其处理程序。 But I personnaly just set up my own logger like you can see in my linked answer from above. 但是我个人而言只是建立了自己的记录器,就像您可以从上方的链接答案中看到的那样。 I hope this might help yuo a bit... 我希望这可以对您有所帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以将celery app.task日志发送到stackdriver中? - Is there any way to send celery app.task logs into stackdriver? Google Cloud Stackdriver 忽略 Python 日志中的格式 - Google Cloud Stackdriver ignores formatting in Python logs 将 python 中使用的库的日志发送到谷歌云堆栈驱动程序日志记录 - Send logs of libraries used in python to google cloud stackdriver logging Python-无法将文件行号和文件路径推送到Google Cloud StackDriver - Python - not able to push file line no and file path to Google Cloud StackDriver 使用堆栈驱动程序日志计算 Cloud Composer(Airflow 任务)的任务持续时间的脚本 - Script to calculate task duration for Cloud Composer (Airflow tasks) using stackdriver logs Google Cloud:自定义stackdriver指标 - Google Cloud: custom stackdriver metrics 在Celery任务中调用Google Cloud API永远不会返回 - Call to Google Cloud API in Celery task never returns 使用 google-cloud-logging 的 Celery 任务日志 - Celery task log using google-cloud-logging Google StackDriver 将日志与父请求 python 3 相关联 - Google StackDriver correlating logs with parent request python 3 无法通过芹菜信号连接到芹菜任务? - Unable to connect to celery task from a celery signal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM