0

I have a VM micro instance running on google compute cloud and I want to log an error message to stackdriver. This page https://cloud.google.com/logging/docs/agent/installation shows this example

logger "Some test message"

which works great for normal messages, but I want stackdriver to recognize some messages as errors, so that they would show up here https://console.cloud.google.com/errors, which would allow me to get email notifications.

I'm aware that the gcloud tool has a beta logging solution, but I'm hoping to avoid installing the extra components it requires.

Bijou Trouvaille
  • 8,794
  • 4
  • 39
  • 42

1 Answers1

0

You'll want to read over the docs about formatting at https://cloud.google.com/error-reporting/docs/formatting-error-messages

Something like:

  {
   "message": "Some test message",
   "context": {
     "reportLocation": {
       "functionName": "my_function"
    }
   },
   "serviceContext": {
     "service": "my service",
   }
  }

You'll need the message to be the jsonPayload of the log entry, not the textPayload. I believe the agent will automatically recognize JSON messages, but if there are also non-JSON messages it may fall back to using text in all cases. In that case, using a dedicated log for the errors should help.

You may also be interested in the docs on how messages are grouped together: https://cloud.google.com/error-reporting/docs/grouping

Kirk Kelsey
  • 4,259
  • 1
  • 23
  • 26
  • Thanks for the answer. I've tried your example with `logger "$json"` (in shell) and it shows up in stackdriver but not error reporter. I also tried the same thing but replacing new lines with `\n` like so `json='{\n "message": "Some test message",\n "context": {\n "reportLocation": {\n "functionName": "my_function"\n }\n },\n "serviceContext": {\n "service": "my service",\n }\n }'` – Bijou Trouvaille Aug 25 '17 at 14:22
  • When you expand the log entry in the cloud console logs viewer, what payload type is the content in (json, text, proto)? – Kirk Kelsey Sep 01 '17 at 13:29
  • I think it's just text, but here's the response https://gist.github.com/bijoutrouvaille/881d3b8780ba2e35e3e4dc3320172189 – Bijou Trouvaille Sep 07 '17 at 16:39
  • `textPayload` is the trick. It looks like the agent doesn't fully parse the syslog entry. If the message were e.g. in a file, I suspect it would work better. Are you planning to use `logger` to generate the messages long term? – Kirk Kelsey Sep 08 '17 at 13:13
  • I'd like to avoid, if possible, installing additional components, especially ones that require manual configuration. – Bijou Trouvaille Sep 08 '17 at 13:19
  • also systemd automatically pipes logs to syslog, I think. – Bijou Trouvaille Sep 08 '17 at 13:21
  • Here's a question about getting fluentd to convert text into json payloads that might help (but I think needs some more details): https://stackoverflow.com/questions/45933111/stackdriver-json-payload-in-logs-from-normal-text-payload-using-google-fluentd-a – Kirk Kelsey Sep 15 '17 at 14:08