Configuring centralized logging from JavaScript

Logging messages from a client browser, like from Backbone, Angular, Ember, or simple console.log-style statements? Papertrail has two uniquely flexible ways for aggregating JavaScript log messages. Use the one that works best for your environment.

Logging from a Node.js server rather than a client-side JavaScript app? See Node.js.

Create a logging “bridge” app

Consider this solution regardless of whether you use Heroku. It’s elegant and powerful.

Papertrail integrates with Heroku by automatically collecting all output from a Heroku app and from the Heroku platform. Heroku automatically logs each request.

This is a ready-made log bridge with almost zero effort.

A common way to make supplementary requests as GETs is with an inlined 1x1 image, like this:

<img src="" width=1 height=1 style="display: none" />

Heroku’s service is free at low volume, so the setup doesn’t add any cost. The JavaScript-to-syslog bridge can log to an existing standard Papertrail account or to an add-on account set up just for this. See Heroku setup.

What if I want to do more?

The no-op app is a great basic solution, but there are lots of other things you can do, including augment or format the logs and use asynchronous methods to send data back:

For example, Vimeo’s Tattletale.js logging library can send data to Papertrail:

var tattletale = new Tattletale('');
tattletale.log('“My name is Ozymandias, king of kings:');
tattletale.log('Look on my works, ye Mighty, and despair!”');

Since Tattletale uses async XHR, a bridge app accepting data from Tattletale requires Cross-origin resource sharing (CORS) to be configured on the app – no longer a no-op, but an easy step in many web frameworks. You can find an example Sinatra app supporting CORS and deployable to Heroku here.

Standalone app as bridge

The example above uses Heroku’s add-on log integration to send logs to Papertrail, but Heroku isn’t required. You can set up an app and run it anywhere. Since Papertrail accepts syslog (a simple text format for sending logs over the wire), the “bridge” app is a few lines in any common language.

Here’s an example using Papertrail’s Ruby remote_syslog_logger gem to generate the syslog message. Your JavaScript app hits this bridge app and the bridge outputs the log message to Papertrail.

Example app

Using Sinatra, implementing a route to process log messages would look something like this:

post '/message'
  logger ='', 11111, :local_hostname => 'an-existing-sender', :program => 'js-bridge')"Whatever you want, like: #{params[:order_id]} from #{request.remote_ip}")

Using a POST route as shown in this example would require CORS support (see example app).

Using an existing app

If the client-side app is associated with an existing server-side app, adding a route to that app to process client log messages (similar to the above example) may be easier than setting up a new app, especially if the server app already logs to Papertrail.

Still not sure?

If we can help set up or deploy any of this, or if you’re using another language and would like an example, please contact us.