[Live Webinar] Next-Level O11y: Why Every DevOps Team Needs a RUM Strategy Register today!

Back to All Integrations

Node.js Bunyan Node.js Bunyan

Last Updated: Jun. 29, 2023

Create a Coralogix logger instance and Bunyan stream to set up and use Bunyan, a Node.js module for logging in JSON.

Create a Coralogix Logger Instance

STEP 1. To create a Coralogix logger instance, input the following variables:

VariableDescription
Private KeyCoralogix Send-Your-Data API key, a unique ID which represents your company
Application NameCoralogix application name
For example, a company named “SuperData” might insert the “SuperData” string parameter. If it desires to debug its test environment, it might insert “SuperData –Test”.
SubSystem NameCoralogix subsystem name, essential parameter for organization of your data

STEP 2. Configure the CORALOGIX_URL by inputting your Coralogix domain into the ingress endpoint:
https://ingress.<cx_domain>/api/v1/logs.

STEP 3. Install a Coralogix Bunyan Stream.

For more information on how to set up and use Bunyan, head over to the Bunyan repository.

Installation

Install Bunyan as follows:

npm install --save coralogix-logger-bunyan

Bunyan Usage Example

var bunyan = require("bunyan");
var CoralogixBunyan = require("coralogix-logger-bunyan");
// global configuration for coralogix 
var config = {
    privateKey: "your-private-key",
    applicationName: "YOUR APP NAME",
    subsystemName: "YOUR SUBSYSTEM",
};
CoralogixBunyan.CoralogixStream.configure(config);
// configure bunyan to user coralogix stram 
var logger = bunyan.createLogger({
    name: 'BUNYAN_ROOT',
    streams: [
        {
            level: 'info',
            stream: new CoralogixBunyan.CoralogixStream({category:"YOUR CATEGORY"}),
            type: 'raw'
        }
    ]
});
// use bunyan 
logger.info('hello bunyan');
// use bunyan child loggers and assign category to them 
var childLogger = logger.child({category:"CHILD CATEGORY"});
childLogger.error("Child logger bunyan");
//Below example code added to address missing logs due to termination of the process prior
//to the Bunyan stream being cleared. https://github.com/trentm/node-bunyan/issues/37
// Delayed shutdown time in milliseconds (adjust as necessary)
var shutdownDelay = 5000;
//Delayed shutdown function
//Allows the logger to send all pending messages before shutdown
function delayedShutdown(exitCode) {
    setTimeout(() => {
        process.exit(exitCode)
    }, shutdownDelay);
}
//You can change the exitCode thrown by different signals as desired.
process.on('SIGHUP', () => delayedShutdown(1));
process.on('SIGINT', () => delayedShutdown(1));
process.on('SIGTERM', () => delayedShutdown(1));
process.on('uncaughtException', () => delayedShutdown(1));
//When manually stoping your process, use the process.exit() wrapper instead
delayedShutdown(0)

Notes:

  • Bunyan log messages are scheduled asynchronously. If your process is manually terminated or terminates as a result of an error, log messages may be lost.
  • An example delayed shutdown implementation has been provided in the example code above to demonstrate how you can catch different OS signals and trigger a delayed shutdown while allowing your Bunyan log stream time to clear.

Support

Need help?

Our world-class customer success team is available 24/7 to walk you through your setup and answer any questions that may come up.

Feel free to reach out to us via our in-app chat or by sending us an email at [email protected].

On this page

Live Webinar
Next-Level O11y: Why Every DevOps Team Needs a RUM Strategy
April 30th at 12pm ET | 6pm CET
Save my Seat