logzio-nodejs setup

logzio-nodejs collects log messages in an array, which is sent asynchronously when it reaches its size limit or time limit (100 messages or 10 seconds), whichever comes first. It contains a simple retry mechanism which upon connection reset or client timeout, tries to send a waiting bulk (2 seconds default).

It’s asynchronous, so it doesn’t block other messages from being collected and sent. The interval increases by a factor of 2 between each retry until it reaches the maximum allowed attempts (3).

By default, any error is logged to the console. You can change this by using a callback function.

Configure logzio-nodejs

Add the dependency to your project

Navigate to your project’s folder in the command line, and run this command to install the dependency.

npm install logzio-nodejs
Configure logzio-nodejs

Use the samples in the code block below as a starting point, and replace the sample with a configuration that matches your needs.

For a complete list of options, see the configuration parameters below the code block.👇

// Replace these parameters with your configuration
var logger = require('logzio-nodejs').createLogger({
  token: '<<SHIPPING-TOKEN>>',
  protocol: 'https',
  host: '<<LISTENER-HOST>>',
  port: '8071',
  type: 'YourLogType'
});
Parameters
Parameter Description
token Your Logz.io account token.
Replace <<SHIPPING-TOKEN>> with the token of the account you want to ship to.
protocol http http or https. The value here affects the default of the port parameter.
host listener.logz.io Listener host. Replace <<LISTENER-HOST>> with your region’s listener host (for example, listener.logz.io). For more information on finding your account’s region, see Account region.
port 8070 (for HTTP) or 8071 (for HTTPS) Destination port. Default port depends on the protocol parameter.
type nodejs The log type, shipped as type field. Used by Logz.io for consistent parsing. Can’t contain spaces.
sendIntervalMs 2000 (2 seconds) Time to wait between retry attempts, in milliseconds.
bufferSize 100 Maximum number of messages the logger will accumulate before sending them all as a bulk.
numberOfRetries 3 Maximum number of retry attempts.
debug false To print debug messsages to the console, true. Otherwise, false.
callback A callback function to call when the logger encounters an unrecoverable error. The function API is function(err), where err is the Error object.
timeout Read/write/connection timeout, in milliseconds.
addTimestampWithNanoSecs false Boolean. Adds @timestamp_nano field, which is a timestamp that includes nanoseconds. To add this field, true. Otherwise, false.
If you’re sending multiple logs per second, we recommend setting to true to preserve the log sequence.
Code sample

You can send log lines as a raw string or as an object. For more consistent and reliable parsing, we recommend sending logs as objects.

To send an object (recommended):

  var obj = {
      message: 'Some log message',
      param1: 'val1',
      param2: 'val2'
  };
  logger.log(obj);

To send raw text:

  logger.log('This is a log message');

Include this line if you’re using logzio-nodejs in a severless environment, such as AWS Lambda, Azure Functions, or Google Cloud Functions:

  logger.sendAndClose();

winston-logzio setup

winston-logzio is a winston plugin and wrapper for the logzio-nodejs appender. With winston-logzio, you can take advantage of the winston logger framework with your Node.js app.

Configure winston-logzio

Add the dependency to your project

Navigate to your project’s folder in the command line, and run this command to install the dependency.

npm install winston-logzio --save
Configure winston-logzio

Use the samples in the code block below as a starting point, and replace the sample with a configuration that matches your needs.

For a complete list of options, see the configuration parameters below the code block.👇

var winston = require('winston');
var logzioWinstonTransport = require('winston-logzio');

// Replace these parameters with your configuration
var loggerOptions = {
    token: '<<SHIPPING-TOKEN>>',
    protocol: 'https',
    host: '<<LISTENER-HOST>>',
    port: '8071',
    type: 'YourLogType'
};

winston.add(logzioWinstonTransport, loggerOptions);
Parameters
Parameter Description
token Your Logz.io account token.
Replace <<SHIPPING-TOKEN>> with the token of the account you want to ship to.
protocol http http or https. The value here affects the default of the port parameter.
host listener.logz.io Listener host. Replace <<LISTENER-HOST>> with your region’s listener host (for example, listener.logz.io). For more information on finding your account’s region, see Account region.
port 8070 (for HTTP) or 8071 (for HTTPS) Destination port. Default port depends on the protocol parameter.
type nodejs The log type, shipped as type field. Used by Logz.io for consistent parsing. Can’t contain spaces.
sendIntervalMs 2000 (2 seconds) Time to wait between retry attempts, in milliseconds.
bufferSize 100 Maximum number of messages the logger will accumulate before sending them all as a bulk.
numberOfRetries 3 Maximum number of retry attempts.
debug false To print debug messsages to the console, true. Otherwise, false.
callback A callback function to call when the logger encounters an unrecoverable error. The function API is function(err), where err is the Error object.
timeout Read/write/connection timeout, in milliseconds.
addTimestampWithNanoSecs false Boolean. Adds @timestamp_nano field, which is a timestamp that includes nanoseconds. To add this field, true. Otherwise, false.
If you’re sending multiple logs per second, we recommend setting to true to preserve the log sequence.
Code samples

To send a log line:

winston.log('info', 'winston logger configured with logzio transport');

winston sends logs to the console by default. To disable this behavior:

winston.remove(winston.transports.Console);

To log the last UncaughtException before Node exits:

var logzIOTransport = new (winstonLogzIO)(loggerOptions);
var logger = new(winston.Logger)({
  transports: [
    logzIOTransport
  ],
  exceptionHandlers: [
    logzIOTransport
  ],
  exitOnError: true    // set this to true
});

process.on('uncaughtException', function (err) {
  logger.error("UncaughtException processing: %s", err);
  logzIOTransport.flush( function(callback) {
    process.exit(1);
  });
});