Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I log to Google Cloud Logging from an AngularJS application?

I'd like to be able to view client side errors in Google Cloud Log viewer. Is it possible to create log entries from an AngularJS application and if so how?

like image 462
Jonas K Avatar asked Aug 12 '16 19:08

Jonas K


2 Answers

Stackdriver Error Reporting is specifically designed for application errors, I encourage you to use it rather than Stackdriver Logging to report errors.

For client-side error reporting, you will need to use the HTTP report API with an API key.

I wrote an JavaScript library to help you call this endpoint from a client: https://github.com/GoogleCloudPlatform/stackdriver-errors-js

I did not test it yet with an Angular app (but there is a tracking issue for this). It is flagged as experimental, but should already be quite stable. Your contributions are welcome if you encounter issues or would like to help

like image 170
Steren Avatar answered Sep 27 '22 02:09

Steren


If you really want to see the errors in the Logs Viewer, the way to do this on GCE or AWS EC2 is to install the Stackdriver Logging agent and either

  • Write your log messages as JSON to port 24224 (see the in_forward documentation)

or

  • Write your logs to disk and tell the agent to watch those files (see the in_tail documentation).

The forwarding port is already pre-configured in the agent. If you choose to go with log files, you would have to write your own configuration pointing the agent at your log files.

Both of the above are server-side, so your client code would need to transfer the errors to the server somehow. It's also possible to expose the forwarding port to the client, but see below.

Another alternative is to use the Stackdriver Error Reporting API and post there either from the server or directly from your client code. That way the errors will appear in the Stackdriver Error Reporting console rather than the Logs Viewer.

Keep in mind that having the client write directly to either the agent port or the Error Reporting API is inherently insecure, as the relevant port/API key would have to be exposed to the internet (so entries can be spoofed and an attacker can, e.g., use up your quota).

Thank you for using Google Stackdriver.

like image 27
Igor Peshansky Avatar answered Sep 27 '22 02:09

Igor Peshansky