gpt4 book ai didi

python - 有没有办法将 celery app.task 日志发送到 stackdriver?

转载 作者:行者123 更新时间:2023-12-02 04:24:16 26 4
gpt4 key购买 nike

我试图将默认登录 Python 模块创建的 celery 任务日志发送到 Stackdriver(谷歌云平台),但我遗漏了一些东西。

在当前状态下,所有日志都已推送,但我缺少在任务 Python 文件中创建的日志。

class Logger(object):
def __init__(self, log_level):
formatter = CustomJsonFormatter(
'(timestamp)(level)(name)(created)(process)(msecs)(message)'
)
formatter.json_ensure_ascii = False
logger = logging.getLogger()
logger.setLevel(log_level)
client = google.cloud.logging.Client.from_service_account_json(
'key.json')
# StreamHandler
# sh = logging.StreamHandler()
sh = CloudLoggingHandler(client)
sh.setLevel(log_level)
sh.setFormatter(formatter)
logger.addHandler(sh)


from app.utils.logger import Logger
from celery.signals import setup_logging
@setup_logging.connect
def initialize_logger(*args, **kwargs):
Logger(log_level=log_level)

最佳答案

试试这个:

class Logger(object):
def __init__(self, log_level):
formatter = CustomJsonFormatter(
'(timestamp)(level)(name)(created)(process)(msecs)(message)'
)
formatter.json_ensure_ascii = False
logger = logging.getLogger()
logger.setLevel(log_level)

client = google.cloud.logging.Client.from_service_account_json(
'key.json')
log_handler = client.get_default_handler()
cloud_logger = logging.getLogger("cloudLogger")
cloud_logger.setLevel(log_level)
cloud_logger.setFormatter(formatter)
cloud_logger.addHandler(log_handler)

关于python - 有没有办法将 celery app.task 日志发送到 stackdriver?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56357089/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com