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

duplogger

v1.0.2

Published

Small logger with colors for Node.JS

Downloads

6

Readme

Duplogger

A minimalist logger for Node.JS with color schemes.
Execute node ./node_modules/duplogger/test.js to run the demo:

screenshot

Yet another logger

The search "logger" yields more than 5000 results on NPM so it's likely there are similar modules. It is a set of features I find useful. In order to maintain and deploy the module, I decided to publish it on NPM.

Installation

$ npm install duplogger

How to use

The following approach is my preferred way of calling the API. If you don't like it, you may skip to Configuring.

I don't like loggers with logging levels like:

otherLogger.log(3, 'Dump users: ' + users.dumpUserList()); // 3 is the log level

I find it to be flawed because users.dumpUserList() will execute regardless of the logging level. If the logging level equals or less than 2, the call will simply waste the resources and produce no results.
Instead, I advice to check the logging level beforehand:

if(debug >= 3)
    logger.log('Dump users: ' + users.dumpUserList());

That way the call users.dumpUserList() will only trigger if the log level equals or higher than 3.
Check test.js for example code.
Also you may want to check the duplogger.js where the source code is located. It's a small file.

Configuring

const Duplogger = require('duplogger');

After that you can change some variables. Once you change something it will be used for all instances of logger in all files.

  • Duplogger.colors - colors that available for log.colorize and rotation array. You can delete/add/modify colors. Check duplogger.js
  • Duplogger.colors_rotate - array of color names for rotation. Every instance of Duplogger will use new color from list.
  • Duplogger.stdout - function(text) - you can create your own function that will receive stdout so you can save/send it somewhere.
  • Duplogger.stderr - same as Duplogger.stdout but for stderr
  • Duplogger.getNewColor(id) - function that rotates color. You can replace it with your own to generate colors based on id. Return color name.
  • Duplogger.in_tty - true/false - enable/disable colors. Check more detailed explanation below.
Duplogger.in_tty

When you require('duplogger') for first time it tries to detect are you running in tty (terminal/console) or you pipe output somewhere using Node.JS's isatty like node app.js > file.log
If duplogger detects that you are not tty then colors will be disabled and all errors will be duplicated to stdout. So running node app.js > file.log will log errors both to strout and stderr.
You can force enable/disable colors using Duplogger.in_tty = true or false;
Duplogger without colors looks like this:

screenshot

Logger API

After you did const Duplogger = require('duplogger') you can create loggers like let log = new Duplogger(id).
id can be any string you want. Like Webserver or User(somename) or what you think is best to debug your app.

  • log.id - string - id of current logger. You can change it any time
  • log.color_name - string - name of color that Duplogger.getNewColor generated for us. You can change it any time
  • log.info(text) - log text to stdout
  • log.warn(text) - log text to stderr with yellow WARNING inverted text
  • log.error - log error
    • log.error(text) - log text to stderr with red ERROR inverted text and stack trace
    • log.error(e) - log text to stderr with red ERROR inverted text and stack trace of e where e is instance of Error
  • log.colorize - add colors to text
    • log.colorize(text) - change color of text to curent color of instance of duplogger
    • log.colorize(color, text) - change color of text to selected color like red/green/redLight/greenLight. Check duplogger.js for list of colors of add yours modifying Duplogger.colors
    • log.colorize(opt, text) - apply object of options to text
      • opt.color - name of color to paint text (by default current color of instance of duplogger)
      • opt.light - true/false - light version of color (work only for dark colors like red, green but not redLight or greenLight since they already lighted red and green)
      • opt.invert - true/false - invert colors around text (create colored box around text)
      • opt.underline - true/false - underline text. (May not work. Depends on fonts that you use in your terminal. I never use it anyway)
      • opt.blink - true/false - blinking text. (May not work. Depends on fonts that you use in your terminal. I never use it anyway)
  • log.light - change color of text to light version of color
    • log.light(text) - alias for log.colorize({light: true}, text) (Change color of text to light version of current color of current instance of duplogger)
    • log.light(color, text) - alias for log.colorize({color: color, light: true}, text) or log.colorize('colornameLight', text)
  • log.invert - invert color of text (create colored box around text)
    • log.invert(text) - alias for log.colorize({invert: true}, text) (Create colored box around text using color of current instance of duplogger)
    • log.invert(color, text) - alias for log.colorize({color: color, invert: true}, text)
  • log.underline - underline text. (May not work. Depends on fonts that you use in your terminal. I never use it anyway)
    • log.underline(text) - alias for log.colorize({underline: true}, text)
    • log.underline(color, text) - alias for log.colorize({color: color, underline: true}, text)
  • log.blink - blinking text. (May not work. Depends on fonts that you use in your terminal. I never use it anyway)
    • log.blink(text) - alias for log.colorize({blink: true}, text)
    • log.blink(color, text) - alias for log.colorize({color: color, blink: true}, text)
  • log.child(id) - creates new instance from current instance. Will add parent id. If we have log.id = 'Server' then child('IncommingConnection') will create new logger with log.id = 'Server>IncommingConnection'.
    Run node ./node_modules/duplogger/test.js to see how it works.

Good luck!

And I recommend to look at source code of test.js and duplogger.js to see how I use duplogger and source of it to check color names and other stuff.