:page_with_curl: Logging Abstraction Layer for easy backend switching
Application Logging Abstraction Layer
$ yarn add logging-facility
$ npm install logging-facility --save
This code sets up the logging-backend and should be executed within the application bootstrap
const _loggingFacility = require('logging-facility');
// use fancy colored cli output
_loggingFacility.addBackend('fancy-cli');
Within each of your application files you can access the global named loggers
// create/get a logger named "mylogger"
const _logger = require('logging-facility').getLogger('mylogger');
// log something
_logger.info('Initializing module X1', 'Additional Payload');
_logger.emergency('fatal error');
const _loggingFacility = require('logging-facility');
// only required for styling
const _colors = require('colors/safe');
// set the logging backend/upstream
// every log message is passed to this function
_loggingFacility.setBackend(function(facility, level, args){
// simple console output
// log, info, debug
if (level > 5){
console.log(_colors.grey(facility.trim() + '~'), args.join(' '));
// errors
}else{
console.log(_colors.red(facility.trim() + '~'), args.join(' '));
}
});
const _loggingFacility = require('logging-facility');
// add logging backend for fatal errors
_loggingFacility.addBackend(function(facility, level, args){
// send a e-mail on fatal errors occured
if (level > 3){
Mailer.notify(args[0]);
}
});
// add fancy console output
_loggingFacility.addBackend('fancy-cli');
Description: Create a new logger
Syntax: logger:object = getLogger(name)
Arguments:
Returns:
An object with the following logging-functions
Example:
// create/get a logger named "mylogger"
const _logger = require('logging-facility').getLogger('mylogger');
// log something
_logger.info('Initializing module X1', 'Additional Payload');
_logger.emergency('fatal error');
Description: Adds a new backend logger to the stack
Syntax: setBackend(backend:function|string, [minLogLevel:int=99])
Arguments:
fancy-cli
, cli
).Example:
const _loggingFacility = require('logging-facility');
// add logging backend for fatal errors
_loggingFacility.addBackend(function(facility, level, args){
// send a e-mail on fatal errors occured
if (level > 3){
Mailer.notify(args[0]);
}
});
// add fancy console output
_loggingFacility.addBackend('fancy-cli');
// add simple cli output
_loggingFacility.addBackend('cli');
DEPRECATED
Description: Removes all exsiting backends and add a new one to the stack
Syntax: setBackend(backend:function|string)
Arguments:
backend:(function|string) - a callback function which will receive all log messages. all default loggers can be accessed by name.
Example:
```js
const _loggingFacility = require(‘logging-facility’);
// add fancy console output
_loggingFacility.addBackend(‘fancy-cli’);
// add logging backend for fatal errors - WILL REMOVE the fancy-cli backend added previously!
_loggingFacility.setBackend(function(facility, level, args){
// send a e-mail on fatal errors occured
if (level > 3){
Mailer.notify(args[0]);
}
});
### ::LEVEL ###
**Description:** Constants used for different log-levels `@see lib/loglevel.js`
EMERGENCY: 0
ALERT: 1
CRITICAL: 2
ERROR: 3
WARNING: 4
NOTICE: 5
INFO: 6
DEBUG: 7
**Example:**
```js
const _loggingFacility = require('logging-facility');
// get warning log-level
console.log(_loggingFacility.LEVEL.WARNING);
Description: Build-In logging backends
CLI - simple cli logging
FANCY - colorized cli output
DEFAULT - alias of CLI
Example:
const _loggingFacility = require('logging-facility');
// add logging function with min-loglevel of 5
_loggingFacility.addBackend(_loggingFacility.LOGGER.CLI(_loggingFacility.LEVEL.NOTICE));
Please open a new issue on GitHub
Logging-Facility is OpenSource and licensed under the Terms of The MIT License (X11). You’re welcome to contribute!