I'm trying to write a JSON format data log to Cloud logging from Google cloud run. but when I check my log in Cloud logging, my data log is in the message field in jsonPayload, as a string type.
like,
jsonPayload: {
message: "{"test": "testing"}"
python_logger: "cloudLogger"
}
but, what I expected,
jsonPayload: {
test: "testing"
}
This is the code I tried,
client = google.cloud.logging.Client()
handler = CloudLoggingHandler(client, name="test")
cloud_logger = logging.getLogger('cloudLogger')
cloud_logger.setLvel(logging.INFO)
cloud_logger.addHandler(handler)
cloud_logger.info(json.dumps({"test": "testing"})
For the change the format for the jsonPayload, I think I might need to set a formatter, and I tried some samples from the web, but no luck.
How can I make it the jsonPayload shows exactly my JSON formatted data?
CodePudding user response:
Had a similar problem with cloud functions. I was using python3.7
, used multiple client logging libraries and stackdriver was still showing it as text as the python was printing it in string format.
Ended up upgrading python to v3.8
, this solved the issue.
CodePudding user response:
In Cloud Logging, structured logs we can refer to log entries that use the jsonPayload field to add structure to their payloads.
We have various methods to convert your payloads to JSON format:
- When you use the Cloud Logging agent to get your log entries, you can specify that the Logging agent converts your payloads to JSON format.
We should install the logging agent,to enable structured logging, you must change the default configuration of the Logging agent when installing or reinstalling it. Enabling structured logging replaces the previously listed configuration files. There is no change to the operation of the agent itself.
- You can use the Cloud Logging API or the gcloud command-line tool, you can control the structure of your payloads. See Writing structured logs for examples.
You can write structured logs to Logging in the following ways:
- Send the full LogEntry structure with a jsonPayload to the Cloud Logging API
- Supply serialized JSON objects to the Logging agent
When you enable structured logging, the listed logs are converted to log entries with different formats than they had before you enabled structured logs.
Please check Structured logging for more details.
- You can use the BindPlane service to ingest logs, then your payloads are in JSON format and are structured according to the source system.
Also you can check Formatting Python Logs for Stackdriver.