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: '<<LOG-SHIPPING-TOKEN>>',
protocol: 'https',
host: '<<LISTENER-HOST>>',
port: '8071',
type: 'YourLogType'
});
Parameters
| Parameter | Description | Required/Default |
|---|---|---|
| token | Your Layerlog log shipping token securely directs the data to your Layerlog account. Replace <<LOG-SHIPPING-TOKEN>> with the token of the account you want to ship to. |
Required |
| protocol | http or https. The value of this parameter affects the default of the port parameter. |
http |
| host | Use the listener URL specific to the region where your Layerlog account is hosted. Click to look up your listener URL. Replace <<LISTENER-HOST>> with the host for your region. For example, listener.layerlog.com if your account is hosted on AWS US East, or listener-nl.layerlog.com if hosted on Azure West Europe. |
listener.layerlog.com |
| port | Destination port. The default port depends on the protocol parameter: 8070 (for HTTP) or 8071 (for HTTPS) |
8070 / 8071 |
| type | Declare your log type for parsing purposes. Layerlog applies default parsing pipelines to the following list of built-in log types. If you declare another type, contact support for assistance with custom parsing. Can’t contain spaces. | nodejs |
| sendIntervalMs | Time to wait between retry attempts, in milliseconds. | 2000 (2 seconds) |
| bufferSize | Maximum number of messages the logger accumulates before sending them all as a bulk. | 100 |
| numberOfRetries | Maximum number of retry attempts. | 3 |
| debug | Set to true to print debug messsages to the console. |
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. | -- |
| extraFields | JSON format. Adds your custom fields to each log. Format: extraFields : { field_1: "val_1", field_2: "val_2" , ... } |
-- |
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 at the end of the run if you’re using logzio-nodejs in a severless environment, such as AWS Lambda, Azure Functions, or Google Cloud Functions:
logger.sendAndClose();