npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

logging-facility

v2.0.1

Published

Logging Abstraction Layer for easy backend switching

Downloads

94

Readme

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 = require('logging-facility');

// use fancy colored cli output
_loggingFacility.addBackend('fancy-cli');

2. Logging

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');

3. Custom Logging Backend

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(' '));
    }
});

Multiple Backends

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');

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 = require('logging-facility').getLogger('mylogger');

// log something
_logger.info('Initializing module X1', 'Additional Payload');
_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:

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');

::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 = 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:

const _loggingFacility = require('logging-facility');

// get warning log-level
console.log(_loggingFacility.LEVEL.WARNING);

::LOGGER

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

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!