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

@gitul/konsole

v1.1.1

Published

A console extender to prefix datetime and so on

Downloads

6

Readme

Change your console object a little bit useful

MAIN FUNCTION

  • You can add date/time stamp before every log line.
  • All console methods(info/error/warn/debug/log) are changed to prefix datetime stamp.
  • One time use of 'require' changes global console object.
$ cat your.js

require('@gitul/konsole');
console.info("Hello, world!");

$ node your.js
2020-04-27 13:27:36.110 Hello, world?

That's it!

CUSTOMIZATION

You can customize konsole with 'set' method of konsole

var konsole = require('@gitul/konsole');

the konsole object is global.console itself, and which is added some more functions. so you can tweak these features with console.set method, but it is not recommend for compatibilities.

Change date/time format

We use a simple strftime function which implements %-format date/time value replacer. The default format of konsole is '%F %T.%f' (which means '%Y-%m-%d %H:%M:%S.%f'; e.g 2000-01-23 12:34:56.789). You can customize with konsole.set method.

var konsole = require('@gitul/konsole');
konsole.set('date-format', '%H:%M:%S');

console.debug("Hello world?");

-- run --

13:27:36 Hello, world?

Above example produces just only time related field (%H:%M:%S).

Add log caller location

Log locations sometimes help your debugging works, for e.g.,

function func1() {
    console.debug('CHECK 1');
    if (blah) {
        console.debug('CHECK 2');
        do_something();
    } else {
        console.debug('CHECK 3');
        do_anything();
    }
    for (var i=0; i<the.length; i++) {
        console.debug('CHECK 4', i);
        do_looping_works();
    }
    console.debug('CHECK 5');
}

func1();

Tracing with 'CHECK number' are good for debugging in runtime. Those lines are simply used for flow checking and deleted after debugging. The different numbers are needed for distinguishing source locations. To solve like this problem, just use as this.

var konsole = require('@gitul/konsole');
konsole.set('log-caller', true);

var blah=true;
function do_somthing(){};
function do_anything(){};
function do_looping_works(){};

function func1() {
    console.debug('CHECK');
    if (blah) {
        console.debug('CHECK');
        do_something();
    } else {
        console.debug('CHECK');
        do_anything();
    }
    for (var i=0; i<the.length; i++) {
        console.debug('CHECK', i);
        do_looping_works();
    }
    console.debug('CHECK');
}

func1();

Only we need is the reapeated line console.debug('CHECK');. This makes your debugging simple.

Let's test!

2020-04-28 18:45:37.817 CHECK at func1 (/home/user1/work/konsole/a.js:11:13)
2020-04-28 18:45:37.824 CHECK at func1 (/home/user1/work/konsole/a.js:13:17)
2020-04-28 18:45:37.824 CHECK 0 at func1 (/home/user1/work/konsole/a.js:20:17)
2020-04-28 18:45:37.825 CHECK 1 at func1 (/home/user1/work/konsole/a.js:20:17)
2020-04-28 18:45:37.825 CHECK 2 at func1 (/home/user1/work/konsole/a.js:20:17)
2020-04-28 18:45:37.825 CHECK at func1 (/home/user1/work/konsole/a.js:23:13)

Strip caller's common path

As you see above example, the project root path is not necessary, so we can specify with strip-prefix to strip out the paths.

var konsole = require('@gitul/konsole');
konsole.set('log-caller', true);
konsole.set('strip-prefix', __dirname );

... (same with above example) ...

Run again!

2020-04-28 20:13:40.269 CHECK at func1 (a.js:12:13)
2020-04-28 20:13:40.275 CHECK at func1 (a.js:14:17)
2020-04-28 20:13:40.276 CHECK 0 at func1 (a.js:21:17)
2020-04-28 20:13:40.276 CHECK 1 at func1 (a.js:21:17)
2020-04-28 20:13:40.277 CHECK 2 at func1 (a.js:21:17)
2020-04-28 20:13:40.277 CHECK at func1 (a.js:24:13)

This is simpler and clear.


Call stack level

If you need more than one frame, you can set calling log level.

var konsole = require('@gitul/konsole');
konsole.set('log-caller', true);
konsole.set('log-caller-frames', 2);
konsole.set('strip-prefix', __dirname );

This will log top most two levels from call stack.

NOTICE: We use 'new Error().stack' mechanism for checking call stack, and which is expensive way, so it is recommended to be removed in production mode.