Duplicate log entries with Google Cloud Stackdriver logging of Python code on Kubernetes Engine

前端 未结 2 1090
一生所求
一生所求 2020-12-31 07:42

I have a simple Python app running in a container on Google Kubernetes Engine. I am trying to connect the standard Python logging to Google Stackdriver logging using this gu

2条回答
  •  青春惊慌失措
    2020-12-31 08:37

    I solved this problem by overwriting the handlers property on my root logger immediately after calling the setup_logging method

    import logging
    from google.cloud import logging as gcp_logging
    from google.cloud.logging.handlers import CloudLoggingHandler, ContainerEngineHandler, AppEngineHandler
    
    logging_client = gcp_logging.Client()
    logging_client.setup_logging(log_level=logging.INFO)
    root_logger = logging.getLogger()
    # use the GCP handler ONLY in order to prevent logs from getting written to STDERR
    root_logger.handlers = [handler
                            for handler in root_logger.handlers
                            if isinstance(handler, (CloudLoggingHandler, ContainerEngineHandler, AppEngineHandler))]
    

    To elaborate on this a bit, the client.setup_logging method sets up 2 handlers, a normal logging.StreamHandler and also a GCP-specific handler. So, logs will go to both stderr and Cloud Logging. You need to remove the stream handler from the handlers list to prevent the duplication.

    EDIT: I have filed an issue with Google to add an argument to to make this less hacky.

提交回复
热议问题