Create a Coralogix logger instance and Bunyan stream to set up and use Bunyan, a Node.js module for logging in JSON.
STEP 1. To create a Coralogix logger instance, input the following variables:
Variable | Description |
Private Key | Coralogix Send-Your-Data API key, a unique ID which represents your company |
Application Name | Coralogix 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 Name | Coralogix 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.
Install Bunyan as follows:
npm install --save coralogix-logger-bunyan
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:
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].