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

@isoss/logger.js

v1.0.0

Published

A lightweight and simple logging library

Downloads

2

Readme

@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
}