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

colorized-logger

v1.0.3

Published

Simple instantiable logger that prepends a customizable colorized message to every log message per logger instance.

Downloads

11

Readme

colorized-logger

Simple instantiable logger that prepends a customizable colorized message to every log message per logger instance.

1. Installation

~$ npm install --save colorized-logger

2. Usage


// Step 1: import ColorizedLogger

const { ColorizedLogger } = require("colorized-logger");

// Step 2: instantiate a new ColorizedLogger

const debugLogger = new ColorizedLogger("[debug]", ["green", "bold", "underline", "bgBlack"], function(message) {
 return message.startsWith("[debug]");
});

// Step 3: use your new ColorizedLogger instance

if(debugLogger.log("Something")) {
 debugLogger.log("Always will return true because of the callback: the message will always start with '[debug]'.");
} else {
 debugLogger.log("Never executed");
}

3. API

ColorizedLogger = require("colorized-logger").ColorizedLogger

Type: {Class}

Description: Class used to instantiate easily and fast new loggers with colorized prepended message, and configurable returned callbacks.


new ColorizedLogger(message:String, styles:Array, callback:Function)

Type: {Constructor}

Parameter: {String} message. Message to be prepended in every ColorizedLogger#log(...) call.

Parameter: {Array<String>} styles. Styles (colors, background colors and modifiers) applied to the prepended message of this logger. The colorization is based on chalk library. Possibilities are listed here:

Modifiers

· reset

· bold

· dim

· italic (Not widely supported)

· underline

· inverse

· hidden

· strikethrough (Not widely supported)

· visible (Text is emitted only if enabled)

Colors

· black

· red

· green

· yellow

· blue (On Windows the bright version is used since normal blue is illegible)

· magenta

· cyan

· white

· gray ("bright black")

· redBright

· greenBright

· yellowBright

· blueBright

· magentaBright

· cyanBright

· whiteBright

Background colors

· bgBlack

· bgRed

· bgGreen

· bgYellow

· bgBlue

· bgMagenta

· bgCyan

· bgWhite

· bgBlackBright

· bgRedBright

· bgGreenBright

· bgYellowBright

· bgBlueBright

· bgMagentaBright

· bgCyanBright

· bgWhiteBright

Parameter: {Function} callback. Function to which the message is passed in plain format (once it is printed by console). This function is the last call of the ColorizedLogger#log(...) call as a return statement. This means that what is returned by this callback, is returned by the ColorizedLogger#log(...) call. By default: () => {}.

Description: Creates a new {ColorizedLogger} instance.


ColorizedLogger#message

Type: {String}

Description: Message to be prepended on every ColorizedLogger#log(...) message logged. It is defined in the constructor call (parameter 1).


ColorizedLogger#styles

Type: {Array<String>}

Description: Styles to be used on the prepended message by the ColorizedLogger#log(...) call. It is defined in the constructor call (parameter 2).


ColorizedLogger#callback(message:String, parameters:Array)

Type: {Function}

Parameter: {String} message. Plain text of the log.

Parameter: {Array<Any>} parameters. Data passed to the ColorizedLogger#log(...) call, without modifications.

Returns: {Any}

Description: Callback called as return statement by the ColorizedLogger#log(...) call. It is defined in the constructor call (parameter 3). The parameters will be injected when the ColorizedLogger#log(...) is called.


ColorizedLogger#log(...args:Any)

Type: {Method}

Parameter: {Any} ...args. Pass any type of data. Each item passed will be stringified (unless it is already a {String}) and concatenated in the same log message. Functions are also accepted, and their inner code will be printed.

Returns: `{Any}

Description: Prints the message defined by ColorizeLogger#message with the styles specified at ColorizeLogger#styles and the items stringified afterwords. Finally, it returns what ColorizeLogger#callback(fullLoggedMessageAsPlainText, passedArguments) call returns when it is called like this.


ColorizedLogger.create(message:String, styles:Array, callback:Function)

Type: {Static method}

Returns: {ColorizedLogger}

Description: It does the same as the ColorizedLogger constructor, but statically.


ColorizedLogger.stringify(data:Any, beautify:Boolean)

Type: {Static method}

Parameter: {String} data. The data to be stringified.

Parameter: {String} beautify. Whether to beautify the message, or not. By default: true.

Returns: {String}

Description: It returns any type of data, but stringified as JSON ({Function}s are allowed too).

4. Test, coverage and documentation

To generate the coverage:

~$ npm run coverage

To do the tests:

~$ npm run test

To generate the documentation:

~$ npm run docs

5. Conclusion

This is a small and minimalistic tool to have a visual logger implemented in your projects. Fully customizable, the functional approach lets you implement your own transporters, beside the colorized logging, which is quite comfortable, to be honest, compared with other logging frameworks. In fact, despite being so minimalistic, this is my favorite logging framework for Node.js.

Fully tested and covered, you can rely on this logging framework for your projects.