有什么方法可以将celery app.task日志发送到stackdriver中?

时间:2019-05-29 09:17:17

标签: python logging google-cloud-platform celery stackdriver

我试图发送芹菜任务日志,该日志是默认情况下创建的,将Python模块记录到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)

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

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)