node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org »

@h2w/h2logger

Synopsis

H2Logger is a log wrapper that can write to multiple logging services. Currently, Winston and SumoLogic are supported.

Motivation

To centralize logging in a single npm registry/node package

Installation

The H2Logger is stored in a repo on npmjs.org.

To install:

npm install @h2w/h2logger --save

Configuration - Environment Variables:

The H2Logger expects certain environment variables to be present in order to be operational. If they environment variables are not set, the H2Logger will not load.

The expected environment variables are:

  • LOG_LEVEL
  • SUMO_LOGIC_ENDPOINT
  • SUMO_LOGIC_COLLECTOR_CODE

The following sections decribe the details of each of the environment variables listed above.

LOG_LEVEL

Log levels supported inclusively according to the following list, as borrowed from winston:

  • silly
  • verbose
  • debug
  • info
  • warn
  • error

Thus, if one declares a log level of silly, the levels error, warn, info, debug, and verbose are reported too.

|Example to set the environment varible LOG_LEVEL| |---|: |export LOG_LEVEL=error|

SUMO_LOGIC_ENDPOINT

The endpoint defined in SumoLogic to where log information will be sent. See the section, Finding SumoLogic endpoint and collector code, below to find the value to assign to this environment variable.

Example to set the environment varible SUMO_LOGIC_ENDPOINT
export SUMO_LOGIC_ENDPOINT=https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/

SUMO_LOGIC_COLLECTOR_CODE

The collector code as defined in SumoLogic. The Collector Code is the Base64 string that appears after the last slash in the URL defined in the SumoLogic Control Panel.

Example to set the environment varible SUMO_LOGIC_COLLECTOR_CODE
export SUMO_LOGIC_COLLECTOR_CODE=SSBhbSBzb21lIGJhc2U2NCBlbmNvZGluZw==

To learn more about setting this environment variable, see the section, Finding SumoLogic endpoint and collector code, below.

Finding SumoLogic endpoint and collector code

To find the values that you will apply the environment variables, SUMO_LOGIC_ENDPOINT and SUMO_LOGIC_COLLECTOR_CODE, in the SumoLogic Control Panel, goto: Manage > Collection, and get the source category

Screen Shot 2016-10-10 at 2.03.31 PM.png

Screen Shot 2016-10-10 at 2.03.57 PM.png

Screen Shot 2016-10-10 at 2.04.18 PM.png

The endpoint is the part of the url that ends with a slash. i.e. https://endpoint1.collection.us2.sumologic.com/receiver/v1/http/

The collector code is the Base64 encrypted part of the URL that follows the last slash in the url.

Sample Use

The intent of this package is to support all the behavior common to console.log while also including support of multiple arguements and all data types.

Formatting console logs is left to winston, except that some stackTrace info is appended to each line.

Formatting of SumoLogic logs is handled by this module in the foloowing ways:

  • strings from multiple parameters are join with a space
  • if multiple objects are passed in, their keys and values are combined, which makes data more searchable on SumoLogic
  • structued stackTrace info is added to every log.

If a function is provided as the last argument, it will be used as a callback. Both SumoLogic and winton return callbacks, so the results will be combined into one object. Winston resolves with the log level. Sumo resolves with an empty string. example:

javascript
{ winston: 'debug',
  sumoLogic: ''
}

Logging Examples

const logger = require('@h2w/h2logger');
 
logger.log(debug''this is a debug statement using log');
logger.debug({ message: 'this is a debug statement using an object'});
logger.error('this is an error statement');
logger.error(new Error('this is an error statement'));
logger.warn('this is a warning statement', { meta: 'data' });
logger.info('this is an info statement', { meta: 'data' });
logger.verbose('this is a verbose statement');
logger.silly('this is a silly statement o_O');
logger.debug('this is a debug statement with 2 params', { meta: 'data' });
logger.debug('debug''this is a debug with 4 params', { meta: 'data' }, (err, data) => {
    if (err) doSomething(err);
});

You can also send SumoLogic metadata as a third parameter (optional, advanced) in the log method call. There is also a fourth parameter which is a callback to detect failures to sumo (optional).
By default, these are logged to console. The log() method is also supported, and adds a level parameter in position 1.

Searching and tailing

Tailing

Trailing allows you to scroll through log output in real time. You can trail log data in SumoLogic.

To trail in SumoLogic go to Search > Live Tail in the SumoLogic user interface and enter sourceCategory=<source category> in the search bar, where <source category> is the log you want to trail. Then click, Run

Example
sourceCategory=local/node/challengeAPI/logs

Stack Trace

All logging to error() are added the stack trace. All other log levels will include a line number and file name.

License

The package is unlicensed and private