Support Logging setup

Node.js Logging

Node.js Logging

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 node logging has been tested with node versions 6.10.1 and 7.9.0. 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');
require('winston-loggly-bulk');
 
 winston.add(winston.transports.Loggly, {
    inputToken: "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

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 3.x Support

With winston’s latest 3.x release, we have updated our library to be compatible with it and provided initial support. We created a different 3.x branch and are managing all our latest 3.x compatible code there. We are currently on our winston-loggly-bulk-3.0.0-rc1 package and encouraging our customers to use this latest package and provide feedback so we can make it more stable. You can set up the library with winston 3.x compatibility by running the command below.

npm install https://github.com/loggly/winston-loggly-bulk.git#3.x --save

There are some syntax changes as well 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.

Note: Since winston has increased the minimum nodejs version support to V6.0.0, this library also requires minimum nodejs V6.0.0 to run smoothly. Please note that this minimum nodejs version condition exists only with 3.x setup not with winston 2.x setup.

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.

  • Loggly Libraries Catalog – New libraries are added to our catalog
  • 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?

  • Search or post your own questions on Node.js logging levels, file transport, log messages, and more in the community forum.
Thanks for the feedback! We'll use it to improve our support documentation.