@clipmx/node.logger

logger module for clip node applications.

Usage no npm install needed!

<script type="module">
  import clipmxNodeLogger from 'https://cdn.skypack.dev/@clipmx/node.logger';
</script>

README

node.loggly

Introduction

This is a nodejs loggly module for Clip's internal and external node application.

Install

npm install @clipmx/node.logger --save

Important Note: This module works with Node v10.13.0 and higher.

Compatible with 8.11.3 but no less because this is using some ES6 features so please upgrade.

v10.13.0 (npm v6.14.1)

Setup

const logger = require('./clip-logger');

logger.initializeLogger(options)

Please initialize loggly before any logging activity in the application, ideally during initializing middlewares.

Log Levels

https://github.com/ClipMX/the-dragons-lair/wiki/SDS-Logging-Levels

Logging options

When initializing a logger, you can set the following options:

  • name: Name of the logger. The name is logged in every line. (required)
  • folderName: Name of the log directory in the instance. (Default: logs).
  • folderPath: Absolute path directory of the application. (required)
  • defaultLogLevel: Log level. Options: trace, debug, info, warn, error, off. (Default: trace).
  • enableDailyRotateFile will enable the logger to save the logs into files. Boolean (Default: true).
  • loggly: Set loggly config. If not defined, loggly isn't initialized. If loggly attribute is defined, all child attributes { token: '', subdomain: '', tags: '', categoryName: '', level: '', json: true } are required.
  • The level passed in for the loggly.level is loggly's Log Level you can set separately from the other log transports.
  • logLevels: Set custom Log levels ( Default: { trace: 5, debug: 4, info: 3, warn: 2, error: 1, off: 0 } )
  • note if you put in your own custom log levels that are not the clip default you can use them like this:
    logger.mainLogger.customLevel('my log');
    logger.mainLogger.verbose('verbose log');
    logger.mainLogger.silly('silly log');
    
    // you can also hit the default logs the same way if you like
    // you just dont get the benefit of having the requestId, or the error stack strace, ie
    logger.mainLogger.info('info log');
    logger.mainLogger.error('error log');
    
    // the other loggers are available this way as well
    logger.healthLogger.info('info health');
    logger.accessLogger.error('error access');
    logger.idtLogger.debug('idt debug!');

  • colorize: turn on or off colorize ( Default: true )
  • colors: pass in custom colors ( Default: { trace: 'cyan', debug: 'blue', info: 'green', warn: 'yellow', error: 'red', off: 'grey' } ) note: for some reason these are not working right now
  • format: log message format. ( Default: [${moment().format('YYYY-MM-DD HH:mm:ss:SSS')}] [${options.level.toUpperCase()}] test-loggly - [UUID] ${options.message} )
  • idt: turn on/off idt logs ( Default: true )

Usage:

const logger = require('./logger');
const path = require('path');
const appDir = path.dirname(require.main.filename);

const options = {
  name: 'logger',
  folderName: 'logs',
  folderPath: appDir,
  defaultLogLevel: 'trace',
  enableDailyRotateFile: false,
  loggly: {
          token: 'token',
          subdomain: 'paycliptest',
          tags: ['Node-Logger-Test'],
          categoryName: 'test-test',
          level: 'warn',
          json: true,
      },
};

logger.initializeLogger(options);

Logging

Trace

logger.trace('UUID', stringified_data <, .. more stringified data>);

Example: logger.trace('UUID', 'This is test!', JSON.stringify({"test": "More test"}), 3);

Output:

[2017-04-10 11:07:27.859] [TRACE] test-loggly - [UUID]: This is test!
[2017-04-10 11:07:27.859] [TRACE] test-loggly - [UUID]: {"test":"More test"}
[2017-04-10 11:07:27.860] [TRACE] test-loggly - [UUID]: 3

Info

logger.info('UUID', stringified_data <, .. more stringified data>);

Example: logger.info('UUID', 'This is test!', JSON.stringify({"test": "More test"}), 3);

Output:

[2017-04-10 11:07:27.859] [INFO] test-loggly - [UUID]: This is test!
[2017-04-10 11:07:27.859] [INFO] test-loggly - [UUID]: {"test":"More test"}
[2017-04-10 11:07:27.860] [INFO] test-loggly - [UUID]: 3

Error

The Error Object will be parsed and display the file, function, and line number of error that was thrown.

logger.error('UUID', Error, stringified_data <, .. more stringified data>);

logger.error('UUID', Error);

Example:

logger.error('UUID', Error, 'This is test!', JSON.stringify({"test": "More test"}), 3);

logger.error('UUID', Error);

Output:

[2017-05-12 11:40:49:304] [ERROR] test-loggly - [uuid]: Error: Some Error Throw. file name: /Users/matthewsanders/CODE/node.logger/sample.js. method: errorTest. line: 80

Debug

logger.debug('UUID', stringified_data <, .. more stringified data>;

Example: logger.debug('UUID', 'This is test!', JSON.stringify({"test": "More test"}), 3);

Output:

[2017-04-10 11:07:27.859] [DEBUG] test-loggly - [UUID]: This is test!
[2017-04-10 11:07:27.859] [DEBUG] test-loggly - [UUID]: {"test":"More test"}
[2017-04-10 11:07:27.860] [DEBUG] test-loggly - [UUID]: 3

Warn

logger.warn('UUID', stringified_data <, .. more stringified data>;

Example: logger.warn('UUID', 'This is test!', JSON.stringify({"test": "More test"}), 3);

Output:

[2017-04-10 11:07:27.859] [WARN] test-loggly - [UUID]: This is test!
[2017-04-10 11:07:27.859] [WARN] test-loggly - [UUID]: {"test":"More test"}
[2017-04-10 11:07:27.860] [WARN] test-loggly - [UUID]: 3

Raw logging - Info

UUID is not required in rawInfo.

logger.rawInfo(stringified_data <, .. more stringified data>);

Example: logger.rawInfo('UUID', 'This is test!', JSON.stringify({"test": "More test"}), 3);

Output:

[2017-04-10 11:10:55.796] [INFO] test-loggly - Logger Initialized!
[2017-04-10 11:10:55.796] [INFO] test-loggly - {"test":"More test"}
[2017-04-10 11:10:55.796] [INFO] test-loggly - 3

Raw logging - Error

UUID is not required in rawError.

logger.rawError(stringified_data <, .. more stringified data>);

Example: logger.rawError(This is test!', JSON.stringify({"test": "More test"}), 3);

Output:

[2017-04-10 11:10:55.796] [ERROR] test-loggly - Logger Initialization Error!
[2017-04-10 11:10:55.796] [ERROR] test-loggly - {"test":"More test"}
[2017-04-10 11:10:55.796] [ERROR] test-loggly - 3

Raw logging - Debug

UUID is not required in rawDebug.

logger.rawError(stringified_data <, .. more stringified data>);

Example: logger.rawError(This is test!', JSON.stringify({"test": "More test"}), 3);

Output:

[2017-04-10 11:10:55.796] [DEBUG] test-loggly - Logger Initialized!
[2017-04-10 11:10:55.796] [DEBUG] test-loggly - {"test":"More test"}
[2017-04-10 11:10:55.796] [DEBUG] test-loggly - 3

Health Check logs

Success:

Log function for health check success. Logs only in the health file.

logger.healthOkay(req, <stringified_additional data, <.. more stringified data>>);

Example: ```logger.healthOkay(req);`

Outputs: [2017-04-10 12:52:49.888] [INFO] health-check - [] GET: /hostname/index Health is Okay!

Example: index.healthOkay(req, JSON.stringify({ status: true }));

Outputs: [2017-04-10 12:55:47.493] [INFO] health-check - [] GET: /hostname/index Health is Okay! {"status":true}

Failure:

Log function for health check failure. Logs in main and health files.

Example: logger.healthFail(req, error, JSON.stringify({ critial: true }))

[2017-05-18T16:39:55.860Z] [ERROR] - Health Check - []   GET : /hostname/index Error: Health Error, file name: sample.js, method: healthTest line: 51
2017-05-18T16:53:20.658Z] [ERROR] - Main - []   GET : /hostname/index ERROR HEALTH!
[2017-05-18T16:53:20.658Z] [ERROR] - Main - []   GET : /hostname/index {"critial":true}

Incoming Request Log:

Logs the incoming request information. This function is used for all the incoming request in the service.

logger.incomingRequestLog(req, opts)

Options for opts:

  • log: Log the body parameters. The param is false for end-points like registration, login where you want to mask the password. (Option: true or false) (Default: true)

Example: logger.incomingRequestLog(req);

Output:

[2017-04-10 13:09:10.282] [INFO] test-loggly - [] GET: /hostname/index
[2017-04-10 13:09:10.282] [INFO] test-loggly - [] :Query params: "{test: 123}"

Example: logger.incomingRequestLog(req, { log: false });

Output: [2017-04-10 13:15:29.636] [INFO] test-loggly - [] GET: /hostname/index

Dynamic Change Log Level

You can change the log level dynamically too if you would like. This will change the log level for the specific logger (main, idt, healthCheck, access) and all of its transports.

If you pass in 'all' this will change 'all' of the loggers within the module. (main, idt, healthCheck, access).

logger.changeLogLevel('all', 'error');
logger.changeLogLevel('main', 'trace');
logger.changeLogLevel('main', 'error');