简体   繁体   English

Python-将日志条目写入GCE VM实例

[英]Python - Write log entry to GCE VM instance

I got the same problem with this post . 这篇文章我也遇到了同样的问题。 All the logs are written as the global category. 所有日志都写为全局类别。 How can I write the logs as GCE VM Instance category? 如何将日志写为GCE VM实例类别? Thanks. 谢谢。 Below is what I have so far: 以下是我到目前为止的内容:

import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = os.path.abspath("") + "/server/keys/google/logging.json"
import logging
import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler

client = google.cloud.logging.Client()
handler = CloudLoggingHandler(client)

cloud_logger = logging.getLogger("cloudLogger")
cloud_logger.setLevel(logging.INFO)
cloud_logger.addHandler(handler)

cloud_logger.error("bad news error")
cloud_logger.warning("bad news warning")
cloud_logger.info("bad news info")

As stated in the documentation : 文档中所述:

You can write logs to Logging from Python applications by using the Python logging handler included with the Logging client library, or by using Stackdriver Logging API Cloud client library for Python directly. 您可以使用Logging客户端库附带的Python日志处理程序,或直接使用适用于Python的Stackdriver Logging API Cloud客户端库,将日志从Python应用程序写入Logging中。

I've also found this sample code on how to use the Google Cloud Client Library for Python. 我还找到了有关如何使用适用于Python的Google Cloud Client库的 示例代码 But as reported in similar issues ( issue 1 and issue 2 ) this is not working as intended, apparently is not possible to set Logging Severity. 但是,正如在类似问题( 第1期第2期 )中所报告的那样,此方法未按预期工作,显然无法设置日志记录严重性。 You can keep tracking both issues in order to track the progress made. 您可以继续跟踪两个问题,以跟踪进度。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM