Free Trial

Add logging to your apps with the new Sumo Logic Javascript Logging SDK

Many apps today are written in Javascript for both the front end UI and server and this new SDK, available on Github, is intended to make integrating Sumo Logic with your Javascript application easier and more dependable.

The SDK provides a convenient wrapper for sending log messages to HTTP sources in your account and includes both plain vanilla Javascript and Node.js module versions. The log messages may be text or structure JSON objects and are sent at batched intervals to minimize network traffic.

Two small demo apps are included, one for each version. The Node example is in the node-example directory and the browser app demo is in the example directory; both include an HTML page that let you send test messages to your own endpoint.

The Node.js Example Page

Prerequisites

Before we dive in, please note that you must have an HTTP source in your Sumo Logic account to use this SDK. To create one, log into Sumo Logic, go to the Collectors page and add a new HTTP source to a new or existing Hosted Collector. You’ll need the endpoint URL to configure the logger object.

If you don’t have a Sumo Logic account yes, you can quickly create one by going to www.sumologic.com and clicking the Free Trial button — no cost, just enter your email.

Please review the Security Note at the end of this article before planning your implementation.

Configuration options

The log object exposes a number of properties you may configure, and while most are useful only the HTTP source endpoint is required.

  • endpoint: HTTP source URL (required)
  • interval: an integer specifying the milliseconds between sending messages
  • sendErrors: true, defaults to FALSE, only pass this to NOT send all unhandled errors to Sumo Logic
    (Vanilla JS version only)
  • sessionKey: a GUID identifier
  • sourceName: metadata value for searches
  • sourceCategory: metadata value for searches
  • hostName: metadata value for searches
  • onSuccess: function() { … a callback to handle successful sends …. },
  • onError: function() { … a callback to handle handle errors …. }

Using the SDK

Other than configuring the logger, usage is straightforward: Simply call the logger’s log() function with the message or messages to be logged. The function accepts a string or a JSON object, or an array of strings or JSON objects. Messages are queued until the sync interval is reached and then send to the endpoint. You may supply success and error callback functions to be called in either circumstance.

The log function accepts a second parameter, an object, so you can configure a few values on a per-message basis:

  • sessionKey: Override the default sessionKey value supplied when the logger object was instantiated.
  • timestamp: Override the default timestamp for the message (otherwise new Date() is sent).
  • url: Page URL from which the log message was sent (Node.js version only)

There are two additional utility functions:

  • emptyLogQueue: Clears any unsent messages from the queue
  • flushLogs: Immediately sends any unsent messages, which should be called from your shutdown or unload handlers to ensure all messages are sent to Sumo Logic.

Calling the logger’s log function:

// Node.js (using Express middleware)
var sumoLogger = require('../src/sumoLogger.js');
var opts = {
  endpoint: '[Your HTTP source endpoint goes here]',
  clientUrl: 'https://myapp.example.com',
  interval: 10000,
  // generate a GUID or inject standard session key
  sessionKey: 'Abc32df34rfg54gui8j098dv13sq5re', 
  sourceName: 'My Custom App',
  sourceCategory: 'My Source Category',
  hostName: 'My Host Name',
  onSuccess: function () {
    console.log('Sumo Logic success callback executed');
  },
  onError: function () {
    console.log('Sumo Logic error callback executed');
  }
};

// ...
var logSumo = new sumoLogger(opts);

// ...
app.post('/sendLog', function (req, res) {
  var msg = req.body.msg;
  var opts = {};
  if (req.body.url) {
    opts.url = req.body.url;
  }
  logSumo.log(msg, opts);
  res.send(true);
});
// In-browser JS
<script src=”/path/to/sumologic.logger.js”></script>
// ...
<script>
var messageData = [
  {'testingKey': 'Testing from example page'},
  {'testingKey': 'Second test log line'}
];
SLLogger.log(messageData);
</script>

Please review the two demos’ source files for more detail.

Feedback and Issues

I’d love to hear from people who use the SDK, especially if you have ideas to make it better. Issues should be filed on the Github repo’s issues page.

Security Note

Sumo Logic is always concerned with security but some instances call for balancing risk with value of functionality. Using the vanilla JS version of this library is one such situation.

Hitting an HTTP source endpoint from code running in a web browser exposes the endpoint URL to anyone inspecting the code or running your app with the browser console open to the network tab. There is no means to obfuscate or hide this. The risk is some malicious individual will send additional traffic to the endpoint, potentially using up your ingest or polluting your searches.

If this is a concern for you, we recommend using the Node.js version of the lib so your endpoint URL is never exposed.

One method for minimizing the damage from some malicious users, should you choose to use this or other similar code in the browser, is adding an arbitrary string based on a regex to your log message and adding a processing rule to the HTTP source configuration that blocks incoming messages which lack a match for the regex.

Get Started Today!

Sign up for your FREE Sumo Logic Trial.

Free Trial
“Sumo Logic brings everything together into one interface 
where we Hudl can quickly scan across 1,000 servers across and gigabytes of logs and quickly identify problems. It’s awesome software 
and awesome support.”

Jon Dokuli,
VP of Engineering

Sign up for your 30 day free trial!*
Sign up for Sumo Logic Free
  • No credit card required to sign-up
  • Create your account in minutes
  • No expiration date*
  • *After 30 day trial period, reverts to Sumo Logic Free
    View All Pricing Options
    Already have an account? Login