Git Product home page Git Product logo

logger.js's Introduction

@isoss/logger.js

npm-version dependencies license

A lightweight and simple logging library
inspired by jonnyreeves' work

Table of contents

Install

$ npm install @isoss/logger.js

Usage

Using the constructor

import { Logger } from '@isoss/logger.js';
// or const { Logger } = require('@isoss/logger.js');

const logger = new Logger({
    context: {
        name: 'MyLogger'
    }
});

Note: a logger handles by default only fatal, error, warn and info levels. See Levels below.

Using Logger.get

import { Logger } from '@isoss/logger.js';
// or const { Logger } = require('@isoss/logger.js');

const logger = Logger.get('MyLogger');

Note: The Logger.get method retrives a logger already instantiated or creates a new one.

Logging

A logger has 6 different levels of logging:
FATAL, ERROR, WARN, INFO, DEBUG, TRACE
Each of these logging levels has its own method on the logging interface.

logger.fatal('Whoops ! A fatal error occurs.');
// => [hh:mm:ss] [MyLogger] [Fatal] Whoops ! A fatal error occurs.
// [Program crash...]

logger.error('This is pretty embarassing...');
// => [hh:mm:ss] [MyLogger] [Error] This is pretty embarassing...

logger.warn('Something goes wrong but we can continue.');
// => [hh:mm:ss] [MyLogger] [Warn] Something goes wrong but we can continue.

logger.info('This is a neat info !');
// => [hh:mm:ss] [MyLogger] [Info] This is a neat info !

logger.debug('AAAAA');
// => [hh:mm:ss] [MyLogger] [Debug] AAAAA

logger.trace('Very verbose logging !');
// => [hh:mm:ss] [MyLogger] [Trace] Very verbose logging ! 

Levels

Logging levels are represented by a bitfield. Only FATAL, ERROR and INFO are enabled by default. A level which isn't enabled will not be handled by the logging handler (see Handler below).

import { Level } from '@isoss/logger.js';
// or const { Level } = require('@isoss/logger.js');

Combining

As a level is represented by a flag in a bitfield, you can combine multiple levels easily using the bitwise operator |.

Level.FATAL | Level.ERROR | Level.INFO // FATAL, ERROR and INFO levels

Enabling

Use Logger#enable to enable a logging level (or more).

logger.enable(Level.WARN); // Enables the WARN level

You can check if a level is enabled using Logger#enabledFor

if(logger.enabledFor(Level.TRACE)) {
  // Do something
}

Disabling

Use Logger#disable to enable a logging level (or more).

logger.disable(Level.DEBUG); // Disables the DEBUG level

Handler

All log messages are routed through a handler functions which redirects them somewhere. You can configure it using Logge#setHandler. The supplied function expects three arguments; the first being the log messages, the second being the level key which represents the handled level (i.e. 'FATAL', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'TRACE') and the third being the context (name and levels that the logger handles) to handle.

logger.setHandler((messages, level, context) => {
    // Redirect messages somewhere
});

Default Handler

logger.js provides a default logging handler which writes to the console object using the appropriate logging function (i.e. logger.info => console.info).

Use Logger.createDefaultHandler to return a new logging handler.

let handler = Logger.createDefaultHandler({
    formatter: (messages, level, context) => {
        // Prefix each log message by a timestamp
        messages.unshift(new Date().toLocaleTimeString());
    }
})

Context

A context object contains the logger's name and filter level (the levels which are enabled). You can get it using Logger#getContext and set it via Logger#setContext.

{
    filterLevel: 0, // The bitfield representing the filter level
    name: 'Logger' // The logger's name
}

logger.js's People

Contributors

is0ss avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.