1.1.10 • Published 5 years ago

@mtel/logger v1.1.10

Weekly downloads
17
License
ISC
Repository
-
Last release
5 years ago

@mtel/logger

Simple logging solution for Node.js AWS Lambda projects.

Usage

Levels

This has a progressive level of logging that allows you to change the verbosity of your application depending on the current runtime needs. For exmaple, you set your logging level to 'error' nothing below that point will be reported to the console.

Each logging statement matches console.log, in that you can pass in a message and an object. In fact, under the covers, it is only using console.log or console.error, just with a bit of formatting help and stops to help with verbosity.

  1. trace() - Intimate application behavior for a specific module.
  2. debug() - Detailed 'general' application behavior.
  3. info() - Configuration information.
  4. log() - Synonym for info, to be consistent with console.
  5. warn() - Problematic behavior that isn't application fatal.
  6. error() - Broken or failed behavior.
const LoggerService = require('@mtel/logger');
const logger = new LoggerService({name:'Parent', level:'trace'});

/*
 * Changes the logging level.
 */
logger.setLevel('warn');

logger.trace(`My message ${variable} value.`);
logger.trace('My message', variable);

logger.debug(`My message ${variable} value.`);
logger.debug('My message', variable);

logger.info(`My message ${variable} value.`);
logger.info('My message', variable);

logger.log(`My message ${variable} value.`);
logger.log('My message', variable);

logger.warn(`My message ${variable} value.`);
logger.warn('My message', variable);

logger.error(`My message ${variable} value.`);
logger.error('My message', variable);

logger.trace('a', { data1: '1', data2: '2' })
logger.debug('a', { data1: '1', data2: '2' })
logger.log('a', { data1: '1', data2: '2' })
logger.info('a', { data1: '1', data2: '2' })
logger.warn('a', { data1: '1', data2: '2' })
logger.error(new Error('errorrrr'), { data1: '1', data2: '2' })

Child

Gets a child logger that will contain the parent history for each logging statement of that child. This allows you to track the specific path of execution that resulted in the logging statement.

Timer

To measure the amount of time elapsed between to points, first call const myTimer = logger.timer('nameOfTimer) then when you are ready to print out the time to console simply pass the timer as an argument to any of the logging methods such as debug logger.debug('My Timer',myTimer).

Allows for easy reporting on the duration of execution of code. Each timing is a point in time, so it can be safely passed into paralelle or promise chains to be referenced at multiple points in time.

const Logger = require('lamlog');
const logger = new Logger({name:'Parent',level:'trace'});

function SuperSpecial(_logger) {
  /*
   * Will take on the logging level of its parent logger, and each logging
   *  statement will reflect the object chain.
   */
  const logger = _logger.child({name:'SuperSpecial'});
  
  /*
   * Records the current time, so it can be reported in a logging statement later.
   */
  const timing = logger.timer('NameOfTimer');
  
  /*
   * A timer can be reported directly in a logging statment and
   *  reported appropriately.
   */
  logger.info(timing);
  //2017-03-18T07:47:03.512Z [info]	Parent.SuperSpecial	NameOfTimer took 0.195009ms
}

// OR

const timing = logger.timer('NameOfTimer');

setTimeout(() => {
  logger.info(timing);
  // 2017-03-18T07:47:03.512Z [info] Parent NameOfTimer took 5.195009ms
}, 5000)

Tracking

// env
TRACKING_S3_REGION='ap-southeast-1'
TRACKING_S3_ACCESS_KEY_ID=''
TRACKING_S3_SECRET_ACCESS_KEY=''
TRACKING_S3_BUCKET_NAME='major-tracking'

logger.tracking('member', {
  action: 'member_movie_viewed',
  member_id: '5cef90037072dea59a618b60',
  movie_id: 'HO00003649',
  datetime: '2019-07-31 03:16:42.012Z'
});

Forked

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago