Reputation: 12888
So I am doing some simple logging to GCP stackdriver using v1 of the python google cloud logging library (see below). I'm not clear on how to set the "severity" level for the log entry. The severity currently shows up in Stackdriver as "default" when I log as below. How do I set the severity? It isn't clear to me from the documentation.
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()
Upvotes: 0
Views: 2103
Reputation: 8074
You can set the severity in the log entry :
Python Client for Stackdriver Logging¶
severity enum (LogSeverity)
Optional. The severity of the log entry. The default value is 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)
Another example of Writing log 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))
Upvotes: 1