Skip to content

Latest commit

 

History

History
68 lines (46 loc) · 2.11 KB

File metadata and controls

68 lines (46 loc) · 2.11 KB

Logger

Features

The logger requires to be configured once prior to first instance being created. Chaining supported.

The wrapper example that could be used in application.

Logger creation with adding context and tags that could be used when filtering log messages later:

const Logger = require('./logger');

const log = new Logger('ctrl.get')
    .context({ requestId: 'arbitrary-string-as-reqId' })
    .tag([ 'crud', 'user' ]);

log.debug('test debugging message');
// 23.06.2018 23:06:17.352 arbitrary-string-as-reqId - debug [logger::ctrl.get] test debugging message {"ctx":{"requestId":"arbitrary-string-as-reqId"},"tags":["crud","user"]}

The child logger creation with its own tag being added:

const Logger = require('./logger');

const log = new Logger('ctrl.get')
    .tag('example', 'parent')
    .child('child')
    .tag('child', 'smth else');

log.info('test info message', { some: 'meta' });
// 23.06.2018 23:06:17.357  - info [logger::ctrl.get.child] test info message {"some":"meta","tags":["example","parent","child","smth else"]}
log.debug('test debugging message');
// 23.06.2018 23:06:17.357  - debug [logger::ctrl.get.child] test debugging message {"tags":["example","parent","child","smth else"]}

Log an error and throw on:

const Logger = require('./logger');

const log = new Logger('ctrl');
const typeError = new TypeError('smth bad happened');

log.throw(typeError);
// 30.06.2018 10:26:48.713  - error [logger::ctrl] smth bad happened {"error":{"name":"TypeError","message":"smth bad happened","stack":"TypeError: smth bad happened\n    at Object.<anonymous> (<...>/logger/tools/app.js:8:19)\n    at Module._compile (module.js:652:30)\n<...>"}}

Installation

npm install @alefi/logger --save

Configuration

The logger has preconfigured default values and do not requires any mandatory settings to be provided while configured. However, any of these values could be overridden. For details see config/default.json.

Test

npm install
npm test

Todo