Configuring centralized logging from JavaScript

Logging messages from a client browser, like from Backbone, Angular, Ember, or simple console.log-style statements? Papertrail has 2 uniquely flexible ways for aggregating the 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.

1-line “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.

Here’s how it works. Because Papertrail automatically captures all output, you’ve got a ready-made log bridge with zero effort. Create a 1-line app or even a 0-line app on Heroku. The app does nothing except return HTTP 200. Use any language which Heroku supports.

While this technique works well with any JS framework or none at all, here’s an example with Vimeo’s Tattletail.js logging library sending to Papertrail:

var tattletale = new Tattletale('https://probat-arabica-29.herokuapp.com/log');
tattletale.log('“My name is Ozymandias, king of kings:');
tattletale.log('Look on my works, ye Mighty, and despair!”');
tattletale.log(42);
tattletale.send();

Why is this neat?

For a few minutes of effort, this bridge app is an incredibly powerful logging handler with nearly zero maintenance. It has. Here’s why it’s far more elegant than logging to Papertrail directly:

Finally, Heroku’s service is free at low volume.

This JavaScript-to-syslog bridge can log to an existing standard Papertrail account or to an add-on specific account setup just for this. See Heroku.

Standalone app as bridge

The example above uses Heroku’s add-on log integration to send logs to Papertrail. If you don’t want to run the logging bridge on Heroku, you can setup an app and run it anywhere. Since Papertrail accepts syslog (a very simple text format for sending logs on the wire), the “bridge” app is a few lines in any common language.

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

Example app

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

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