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

@tapraise/logger

v0.1.4

Published

Logging utilities

Downloads

153

Readme

@tapraise/logger

Advanced logging with:

Installation

npm install --save @tapraise/logger

Examples

Namespace prefixes

When there is a lot being logged to the console, having namespace prefixes allows you to easier scan for specific subjects.

Example:

import { logger } from '@tapraise/logger';

const logger = new Logger({
    namespace: 'security',
});

logger.debug('debugging!'); // will log '[security] debugging!'
logger.warn('look out!'); // will log '[security] look out!'

Also able to apply or update after construction:

import { logger } from '@tapraise/logger';

const logger = new Logger();
logger.setNamespace('security');

Queue-ing

Allows you to queue logs and flush to console only when needed. For instance when you only want to log the output when an error occurs, to provide the context needed to fix it.

Example:

import { logger } from '@tapraise/logger';

const logger = new Logger({
    isQueued: true,
});

try {
    const someInput = {
        // ...
    };
    logger.debug('input:', someInput); // does not log to console yet

    executeTaskThatMightThrow(someInput);

    // resets the logger and with it the queue
    logger.reset();
} catch (error) {
    logger.error('Something went wrong: ', error);
    logger.info('context:');
    logger.flush(); // flushes all queued logs to the console
}

Also able to start queue-ing later on:

import { logger } from '@tapraise/logger';

const logger = new Logger();

logger.debug('Something'); // will be logged to the console right away
logger.queue();
logger.debug('This line is queued');
logger.flush(); // now the previous line is also logged to the console

Custom formatters

To make the output in your console scannable, so you can find the output you need easily, use formatters.

import { logger, SectionFormatter } from '@tapraise/logger';

const logger = new Logger({
    formatters: {
        // Register the formatter in the registry with an identifier
        section: new SectionFormatter(),
    },
});

// now use formatter. Make sure the first argument you log is the formatter identifier, otherwise
// it will not be applied
logger.info('section', 'Some section title');

It is also possible to add formatters on the fly:

import { Logger, SectionLogger } from '@tapraise/logger';

const logger = new Logger();
logger.setFormatter('section', new SectionLogger());

It is also possible to add your own, custom formatters:

import { Logger, FormattedArgs, FormatterInterface } from '@tapraise/logger';

class CustomFormatter extends FormatterInterface {
    format(args: any[]): FormattedArgs {
        // return an array to represent each line, and within that an array for arguments to log on that line, like:

        return [['.....'], args, ['.....']];
    }
}

const logger = new Logger({
    formatters: {
        custom: new CustomFormatter(),
    },
});

Centralized enabling and disabling

To not have to check your environment throughout your code, when logging, you can now centralize checking if logging should actually be done:

const logger = new Logger({
    enabled: process.env.production === false,
});

Build in formatters:

  • SectionFormatter → formats section header
  • ErrorFormatter → formats error messages nice and red!
  • NoteFormatter → formats notes in muted color
  • JsonFormatter → formats JSON strings to be better read- and scan-able, by using indenting
  • SuccessFormatter → formats all in green as success message(s)

Todo

  • [ ] Instead of just enable or not, add more fine grained solution where you can define which levels should be logged
  • [ ] Add listener system that allows you to hook into the system on specific level logs
  • [ ] Some more custom formatters, based upon what we did in other projects
  • [ ] Apply color support for browser. Perhaps using CSS styling?
  • [ ] Be able to apply a bunch of formatters at once, like a plugin?