簡體   English   中英

如何在 python 中設置 stackdriver 日志條目的嚴重性?

[英]How do I set the severity of a stackdriver log entry in python?

因此,我正在使用 python 谷歌雲日志庫的 v1 對 GCP stackdriver 進行一些簡單的日志記錄(見下文)。 我不清楚如何為日志條目設置“嚴重性”級別。 當我按如下方式登錄時,嚴重性當前在 Stackdriver 中顯示為“默認”。 如何設置嚴重性? 從文檔中我不清楚。

import google.cloud.logging as glog1

def do_v1_log():
    # Instantiates a client
    logging_client = glog1.Client()

    # The name of the log to write to
    log_name = 'gregs-log'
    # Selects the log to write to
    logger = logging_client.logger(log_name)
    
    # Writes the log entry
    logger.log_struct({'name': 'Greg', 'phone': '619-555-1809'})


# Main bootstrapping routine
if __name__ == "__main__":
    # main()
    do_v1_log()

您可以在日志條目中設置嚴重性:

日志條目

Python Stackdriver 日志記錄客戶端¶

嚴重性枚舉 (LogSeverity)

選修的。 日志條目的嚴重性。 默認值為 LogSeverity.DEFAULT。


from google.cloud import logging_v2
client = logging_v2.LoggingServiceV2Client()

resource = {
    "type": "global",
    "labels": {
        "project_id": "[PROJECT_ID]"
    }
}


e = logging_v2.types.LogEntry(
    log_name="projects/[PROJECT_ID]/logs/test-logging", # optional
    resource=resource, # optional
    text_payload="this is a log statement",
    severity="WARNING")

entries = [e]
response = client.write_log_entries(entries)

另一個 寫日志條目的例子

def write_entry(logger_name):
    """Writes log entries to the given logger."""
    logging_client = logging.Client()

    # This log can be found in the Cloud Logging console under 'Custom Logs'.
    logger = logging_client.logger(logger_name)

    # Make a simple text log
    logger.log_text('Hello, world!')

    # Simple text log with severity.
    logger.log_text('Goodbye, world!', severity='ERROR')

    # Struct log. The struct can be any JSON-serializable dictionary.
    logger.log_struct({
        'name': 'King Arthur',
        'quest': 'Find the Holy Grail',
        'favorite_color': 'Blue'
    })

    print('Wrote logs to {}.'.format(logger.name))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM