项目作者: AndiDittrich

项目描述 :
:page_with_curl: Logging Abstraction Layer for easy backend switching
高级语言: JavaScript
项目地址: git://github.com/AndiDittrich/Node.Logging-Facility.git
创建时间: 2016-05-30T07:14:51Z
项目社区:https://github.com/AndiDittrich/Node.Logging-Facility

开源协议:MIT License

下载


Logging Facility

Application Logging Abstraction Layer

  1. $ yarn add logging-facility
  2. $ npm install logging-facility --save

Features

  • Abstraction Layer/Wrapper to easily exchange your logging backend without breaking your code
  • Common used syslog levels are exposed as functions (emergency, alert, critical, error, warning, notice, info, debug)
  • Multiple Loggers identified by name
  • Ability to use multiple Backends
  • All Logging functions are aggregated into a single callback

Usage

1. Application Startup/Configuration

This code sets up the logging-backend and should be executed within the application bootstrap

  1. const _loggingFacility = require('logging-facility');
  2. // use fancy colored cli output
  3. _loggingFacility.addBackend('fancy-cli');

2. Logging

Within each of your application files you can access the global named loggers

  1. // create/get a logger named "mylogger"
  2. const _logger = require('logging-facility').getLogger('mylogger');
  3. // log something
  4. _logger.info('Initializing module X1', 'Additional Payload');
  5. _logger.emergency('fatal error');

3. Custom Logging Backend

  1. const _loggingFacility = require('logging-facility');
  2. // only required for styling
  3. const _colors = require('colors/safe');
  4. // set the logging backend/upstream
  5. // every log message is passed to this function
  6. _loggingFacility.setBackend(function(facility, level, args){
  7. // simple console output
  8. // log, info, debug
  9. if (level > 5){
  10. console.log(_colors.grey(facility.trim() + '~'), args.join(' '));
  11. // errors
  12. }else{
  13. console.log(_colors.red(facility.trim() + '~'), args.join(' '));
  14. }
  15. });

Multiple Backends

  1. const _loggingFacility = require('logging-facility');
  2. // add logging backend for fatal errors
  3. _loggingFacility.addBackend(function(facility, level, args){
  4. // send a e-mail on fatal errors occured
  5. if (level > 3){
  6. Mailer.notify(args[0]);
  7. }
  8. });
  9. // add fancy console output
  10. _loggingFacility.addBackend('fancy-cli');

Methods/Syntax

::getLogger()

Description: Create a new logger

Syntax: logger:object = getLogger(name)

Arguments:

  • name:string - the instance name of the logger, passed as first argument to the backend function

Returns:

An object with the following logging-functions

  • emerg(…messages),
  • emergency(…messages),
  • alert(…messages),
  • crit(…messages),
  • critical(…messages),
  • error(…messages),
  • err(…messages),
  • warning(…messages),
  • warn(…messages),
  • notice(…messages),
  • log(…messages),
  • info(…messages),
  • debug(…messages),

Example:

  1. // create/get a logger named "mylogger"
  2. const _logger = require('logging-facility').getLogger('mylogger');
  3. // log something
  4. _logger.info('Initializing module X1', 'Additional Payload');
  5. _logger.emergency('fatal error');

::addBackend()

Description: Adds a new backend logger to the stack

Syntax: setBackend(backend:function|string, [minLogLevel:int=99])

Arguments:

  • backend:(function|string) - a callback function which will receive all log messages. all default loggers can be accessed by name (allowed values: fancy-cli, cli).
  • minLogLevel:int=99 (optional) - the minimum log-level of the backend in case it’s initialized by name not function

Example:

  1. const _loggingFacility = require('logging-facility');
  2. // add logging backend for fatal errors
  3. _loggingFacility.addBackend(function(facility, level, args){
  4. // send a e-mail on fatal errors occured
  5. if (level > 3){
  6. Mailer.notify(args[0]);
  7. }
  8. });
  9. // add fancy console output
  10. _loggingFacility.addBackend('fancy-cli');
  11. // add simple cli output
  12. _loggingFacility.addBackend('cli');

::setBackend()

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]);
}
});

  1. ### ::LEVEL ###
  2. **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

  1. **Example:**
  2. ```js
  3. const _loggingFacility = require('logging-facility');
  4. // get warning log-level
  5. console.log(_loggingFacility.LEVEL.WARNING);

::LOGGER

Description: Build-In logging backends

  1. CLI - simple cli logging
  2. FANCY - colorized cli output
  3. DEFAULT - alias of CLI

Example:

  1. const _loggingFacility = require('logging-facility');
  2. // add logging function with min-loglevel of 5
  3. _loggingFacility.addBackend(_loggingFacility.LOGGER.CLI(_loggingFacility.LEVEL.NOTICE));

Any Questions ? Report a Bug ? Enhancements ?

Please open a new issue on GitHub

License

Logging-Facility is OpenSource and licensed under the Terms of The MIT License (X11). You’re welcome to contribute!