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

@pieropatron/tinylogger

v1.0.3

Published

tiny nodejs logger

Downloads

617

Readme

tinylogger

tiny logger for node js

NPM version NPM downloads

Well, this is the one of my "bicycles" projects, tiny logger, which writes logs to console in log4js like format: [${datetime}] [${level}] ${name} - ${msg0} ${msg1} ... ${msgN}.

Additionally to standard methods, like "info", "warn", "error", "fatal", "debug" also appended methods to create child logger and to log durations. Idea of this project is to have simple to use, extendable Logger, which will have only functionalities to get colorizeds log in console. I was very liked log4js logger (https://github.com/log4js-node/log4js-node), but it's really big, relatively complex and has too much redundant functionality for me.

Install

npm install @pieropatron/tinylogger

Create new logger

As logger in really tiny, no configuration required, only the name of Logger

const {Logger} = require('@pieropatron/tinylogger');
const logger = new Logger('Example logger');

Set level

In fact Logger supports only 2 levels: "info" and "debug" and following realization looks like not best choice, but it allows to be compatible with log4js

// to enable debug level messages
logger.level = "debug";
// to disable debug level messages
logger.level = "info";

Standard methods

logger.info('Information', 'messages', 'of', 'any', 'types');
logger.debug('Warning', 'messages', 'of', 'any', 'types');
logger.warn('Warning', 'messages', 'of', 'any', 'types');
logger.error('Error', 'messages', 'of', 'any', 'types', 'usually', new Error("Sample error"));
logger.fatal('Fatal', 'messages', 'of', 'any', 'types', 'usually', new Error("Sample error"));

Result in console: image

dashline

prints dashline in console of required length (default length is 100)

logger.dashline(250); // prints 250 dash symbols ("-") in console

Create "child" Logger

Sometimes usefull to create another Logger, which are inside of "parent" Logger and has same level. For instance, if you have Logger for script, called "Update db" and you wants to have another Logger for one part of it's processing, such as "Update table".

const logger_db = new Logger('Update db');
logger_db.level = "debug";

function update_child(){
	const logger_table = logger_db.child('Update table');
	logger_table.debug("Starting...");
}

update_child(); // write in console: [2023-02-04T12:39:11.272] [DEBUG] Update db.Update table - Starting...

Note: "child" Logger will not depend on "parent" Logger somehow after creation. Thus, if you change level for parent Logger, it will not changes for "children".

Log durations

For console in nodejs we have time and timeEnd methods for this goals. But it's not comfortable to use for me, as it displays duration in millis and requires to remember names of labels for correct results of timeEnd. Also, it is not configurable to set level of logging of durations, which is often required. Thus, method "time" of Logger will require 2 arguments:

  • name : string, name of time label, mandatory
  • level: "debug" or "info", level of logging, optional, default is "debug".

And it returns a function to call in required moment.

logger.level = 'debug';
const time = logger.time("Example duration");
setTimeout(()=>{
	time();
}, 1000);

Results in console:

[2023-02-04T13:03:38.594] [DEBUG] Example logger - Example duration started
[2023-02-04T13:03:39.604] [DEBUG] Example logger - Example duration finished 00:00:01.010

Thus, as you can see, Logger is really tiny, not configurable and so on, but... As it is extendable, you can always write your own Logger on its base.