Is there a way to write structured logs from Cloud Functions such that the message field is automatically displayed as the primary message in GCP Log Viewer?
Using this test, I've found that a field named message is sometimes used. For example, given these logs:
{"severity":"trace","time":"2020-03-09T12:21:13.660044125-07:00","message":"Trace Basic"}
{"severity":"debug","time":"2020-03-09T12:21:13.660047625-07:00","message":"Debug Basic"}
{"severity":"info","time":"2020-03-09T12:21:13.660049425-07:00","message":"Info Basic"}
{"severity":"warn","time":"2020-03-09T12:21:13.660051425-07:00","message":"Warn Basic"}
{"severity":"error","time":"2020-03-09T12:21:13.660053225-07:00","message":"Error Basic"}
{"severity":"info","time":"2020-03-09T12:21:13.660055125-07:00","message":"One line message"}
{"severity":"info","time":"2020-03-09T12:21:13.660057025-07:00","message":"Line 1 of 2\nLine 2 of 2 for zlog.Info"}
{"severity":"info","myIntField":532,"myStringField":"howdy","myMultilineStringField":"Line 1 of 2\nLine 2 of 2 for zlog.Info with fields","time":"2020-03-09T12:21:13.660059925-07:00","message":"With Fields Example"}
The GCP Log Viewer will display something like this:
Notice the final entry, which should have a message With Fields Example instead has a top level message { "fields": { ... } }.
Cloud Run has a document describing special structured logging fields (i.e. severity and message) that Stackdriver logging will automatically pick up and use to populate the DEBUG/WARN/INFO/ERROR icon and top level message for the log entry in Stackdriver console.
Special JSON fields in messages
When you provide a structured log as a JSON dictionary, some special fields are stripped from the jsonPayload and are written to the corresponding field in the generated LogEntry as described in the documentation for special fields.
For example, if your JSON includes a severity property, it is removed from the jsonPayload and appears instead as the log entry's severity. The message property is used as the main display text of the log entry if present. For more on special properties read the Logging Resource section below.
The corresponding document for Cloud Functions has no information about special fields.
This was a bug in Google Cloud Platform logging that was fixed.