Logging Facility
Application Logging Abstraction Layer
$ yarn add logging-facility$ 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
const _loggingFacility = ; // use fancy colored cli output_loggingFacility;
2. Logging
Within each of your application files you can access the global named loggers
// create/get a logger named "mylogger"const _logger = ; // log something_logger;_logger;
3. Custom Logging Backend
const _loggingFacility = ; // only required for styling const _colors = ; // set the logging backend/upstream// every log message is passed to this function_loggingFacility;
Multiple Backends
const _loggingFacility = ; // add logging backend for fatal errors_loggingFacility; // add fancy console output_loggingFacility;
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:
// create/get a logger named "mylogger"const _logger = ; // log something_logger;_logger;
::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:
const _loggingFacility = ; // add logging backend for fatal errors_loggingFacility; // add fancy console output_loggingFacility; // add simple cli output_loggingFacility;
::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:
const _loggingFacility = ; // add fancy console output_loggingFacility; // add logging backend for fatal errors - WILL REMOVE the fancy-cli backend added previously!_loggingFacility;
::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:
const _loggingFacility = ; // get warning log-levelconsole;
::LOGGER
Description: Build-In logging backends
CLI - simple cli logging
FANCY - colorized cli output
DEFAULT - alias of CLI
Example:
const _loggingFacility = ; // add logging function with min-loglevel of 5_loggingFacility;
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!