Documentation forLoggly

Node.js Logging

Loggly provides the infrastructure to aggregate and normalize log events so they are available to explore interactively, build visualizations, or create threshold-based alerting. In general, any method to send logs from a system or application to an external source can be adapted to send logs to Loggly. The following instructions provide one scenario for sending logs to Loggly.

You can send your Node.js logs using our winston-loggly-bulk package. It sends logs directly from your application to Loggly using the HTTP/S Event API. The minimum supported version of node.js is V6.0.0. If you need to use an older version of node.js, please refer to the section Winston 2.x Support below. For alternatives, please see the Advanced Options section.

Node.js Logging Setup

1. Install the winston-loggly-bulk package

Install the winston-loggly-bulk package using NPM or from Github

If you are running npm version 3 or higher then run the below command to setup the logging-

npm install winston-loggly-bulk 

If you are running npm version 2 or lower then run the below command to setup the logging-

npm install winston-loggly-bulk winston 

Note: To check the currrent npm version run the below command-

npm -v 

2. Configure loggly-winston-bulk

Paste the following code to create a Loggly client.

var winston  = require('winston');
var {Loggly} = require('winston-loggly-bulk');
winston.add(new Loggly({
    token: "TOKEN",
    subdomain: "SUBDOMAIN",
    tags: ["Winston-NodeJS"],
    json: true
}));
winston.log('info', "Hello World from Node.js!");

Replace:

  • TOKEN: your customer token from the source setup page
  • SUBDOMAIN: the subdomain of your Loggly account

You can find more parameters on Github[https://github.com/loggly/winston-loggly-bulk].

3. Send Test Events

Run your Node.js application to send test events to Loggly.

4. Verify Events

Search Loggly for events with the Winston-NodeJS tag over the past 20 minutes. It may take a few minutes to index the event. If it doesn’t work, see the troubleshooting section below.

tag:Winston-NodeJS 

Click on one of the logs to show a list of JSON fields (see screenshot below). If you don’t see them, please check that you are using one of our automatically parsed formats.

Node.js Logging Example

Winston 2.x Support

If you are using a node.js version V6.0.0 or older, you should use Winston 2.x. Winston 2.x is supported by winston-loggly-bulk version 2.0.3. You can install it by running the following command.

npm install winston-loggly-bulk@2.0.3 

There are some syntax changes to create your Loggly logger in your main app.js file so please go through the README.md file and see the sample code snippet here.

Advanced Node.js Logging Options

  • Package Docs – Describes how to send JSON objects, arrays, use tags, and more
  • node-Loggly-bulk – Use the node-loggly-bulk library to send node.js logs
  • bunyan-loggly – Use the bunyan library which has a wrapper for node-loggly
  • log4js – Use the log4js library which has a wrapper for node-loggly
  • HTTP/S Bulk Endpoint – Send up to 5MB batches of logs over HTTP/S for higher performance
  • Buffer Options – This library includes a buffer to save logs during a temporary network outage. By default it saves 500 events and retries after 30 seconds. If you wish to override the default settings, you can add a bufferOptions object as shown below.
    bufferOptions: {
      size: 1000,
      retriesInMilliSeconds: 60 * 1000
    } 
    
  • Flush logs and exit – Our library uses ajax requests to send logs to Loggly, and as ajax requests take time to complete, logs can be lost when process.exit() is called because it forces an immediate exit. To exit gracefully and ensure that the last logs get to Loggly, we created a function called flushLogsAndExit(). It waits for 10 seconds and then calls process.exit() itself. This allows enough time for the logs to be sent to Loggly. Here is an example of how to use the method in your app.js file:
    var winston = require('winston'),
    winlog = require('winston-loggly-bulk');
    winston.log("info", "hello World");
    winlog.flushLogsAndExit();
    
  • If you are instantiating the Loggly transporter using the new keyword and assigning it into any variable, then you have to add that Loggly transporter into winston by passing 3 parameters like below.
    winston.add(transportsObject, null, true);
    winston.remove(transportsObject); 

    As you can see in the above code snippet, we have passed true as a third parameter which tells winston that we have already created an instance of Loggly transporter. Also, if you want to remove that transporter later then you should call the winston.remove() method.

  • Search or post your own Node.js logging or custom logging questions in the community forum.

Troubleshooting Node.js Logs

If you don’t see any data show up in the verification step, then check for these common problems.

Check winston-loggly-bulk:

  • Wait a few minutes in case indexing needs to catch up
  • See our HTTP Troubleshooting Guide to verify HTTP events are being sent to Loggly.
  • If you see the error TypeError: transport is not a function and you are instantiating the Loggly transporter using the new keyword as shown in the Advanced Logging Options of this document, then make sure you are passing true as a third parameter in the winston.add() method.

Still Not Working?

The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.