【问题标题】:How to use the Google api-client python library for Google Logging如何使用 Google api-client python 库进行 Google Logging
【发布时间】:2016-03-11 00:22:38
【问题描述】:

我一直在 python 中将 Google apiclient 库用于各种 Google Cloud API(主要用于 Google Compute),并取得了巨大的成功。

我想开始使用该库来创建和控制 Google Cloud Platform 提供的 Google Logging 机制

但是,这是一个测试版,我找不到任何关于如何使用日志记录 API 的真实文档或示例。

我能找到的只是高级描述,例如: https://developers.google.com/apis-explorer/#p/logging/v1beta3/

谁能提供一个关于如何使用 apiclient 进行日志记录的简单示例? 例如创建一个新的日志条目...

感谢您的帮助

沙哈

【问题讨论】:

    标签: google-compute-engine google-cloud-platform google-api-client google-api-python-client


    【解决方案1】:

    我找到了这个页面: https://developers.google.com/api-client-library/python/guide/logging

    您可以执行以下操作来设置日志级别:

    import logging
    
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    

    但是它似乎对输出没有任何影响,这对我来说始终是 INFO。

    我还尝试将 httplib2 设置为调试级别 4:

    import httplib2
    httplib2.debuglevel = 4
    

    但我在日志中没有看到任何 HTTP 标头:/

    【讨论】:

      【解决方案2】:

      我知道这个问题很老,但它正在引起一些关注,所以我想它可能值得回答,以防其他人来这里。

      用于 Google Cloud Platform 的Stackdriver Logging Client Libraries 不再处于测试阶段,因为它们在不久前就已正式发布。我分享的链接包含与安装和使用它们最相关的文档。

      运行pip install --upgrade google-cloud-logging 命令后,您将能够使用您的 GCP 帐户进行身份验证,并使用客户端库。

      使用它们就像使用 from google.cloud import logging 之类的命令导入库一样简单,然后实例化一个新客户端(您可以默认使用它,甚至可以传递 项目 ID凭据显式)并最终根据需要使用日志。

      您可能还想访问official library documentation,在那里您将找到有关如何使用该库、可用的方法和类以及如何做大部分事情的所有详细信息,其中有很多不言自明的示例,甚至是关于如何与 Stackdriver Logging 交互的不同备选方案之间的比较。

      作为一个小例子,我也分享一下如何检索状态比“警告”更严重的五个最新日志的sn-p:

      # Import the Google Cloud Python client library
      from google.cloud import logging
      from google.cloud.logging import DESCENDING
      
      # Instantiate a client
      logging_client = logging.Client(project = <PROJECT_ID>)
      
      # Set the filter to apply to the logs, this one retrieves GAE logs from the default service with a severity higher than "warning"
      FILTER = 'resource.type:gae_app and resource.labels.module_id:default and severity>=WARNING'
      
      i = 0
      # List the entries in DESCENDING order and applying the FILTER
      for entry in logging_client.list_entries(order_by=DESCENDING, filter_=FILTER):  # API call
          print('{} - Severity: {}'.format(entry.timestamp, entry.severity))
          if (i >= 5):
              break
          i += 1
      

      请记住,这只是一个简单的示例,使用 Logging Client Library 可以实现很多事情,因此您应该参考我分享的官方文档页面,以便更深入地了解一切有效。

      【讨论】:

        【解决方案3】:

        但是它似乎对输出没有任何影响 总是为我提供信息。

        添加一个日志处理程序,例如:

        formatter = logging.Formatter('%(asctime)s %(process)d %(levelname)s:  %(message)s')                                     
        consoleHandler = logging.StreamHandler()                                                                                 
        consoleHandler.setLevel(logging.DEBUG)                                                                                       
        consoleHandler.setFormatter(formatter)                                                                                   
        logger.addHandler(consoleHandler)      
        

        【讨论】:

          最近更新 更多