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

custom-logger

v0.3.1

Published

Simple yet highly customizable console logger for node.js. Supports templating output, custom log levels, custom log types, and colored output.

Downloads

1,546

Readme

custom-logger npm version

custom-logger is a simple, highly customizable logging plugin for node.js.

To install this module simply run:

npm install custom-logger

I'm still very much still learning node.js, and this is my first "official" module. If you notice any unusual or depracated coding behaviors be sure to let me know, like I said, I'm still learning! (:

If you have any questions or feedback, or need any help using this module, please contact @felix_mc.

Basic Usage

var log = require('custom-logger').config({ level: 0 });

log.debug( 'hello?' );

log.info( 'hello world!' );

log.warn( 'carefule there, world!' );

log.error( 'WHOA WHOA WHOA world?!' );

log.config({
	messageFormatting: true
});

log.info("I see %d %s!", 3, "bananas");

The above code will render to:

basic output snapshot

custom-logger uses logging levels and different event types to determine what is being outputed at any given time. Below are the default events and their respective logging levels:

0 => debug
1 => info
2 => warn
3 => error

As you can see above, you can use the level parameter in the config method to set the current logging level. You can call on the config method on the log object at any time throughout your code to change the log level or other options.

custom-log will only show events whose level is equal to or higher than the one set. For example a level of 0 will show all event types, whereas a level of 2 will only show the event types warning and error. By default the logging level is 0.

You can pass any number of parameters to logging methods (like log.info() or log.error), regardless of whether they are a default method or one that you created (see below). custom-logger will join all parameters together in one string, converting JSON and other non-string parameters to strings automatically.

Customizing Events Types

One cool thing you can do with custom-logger is you easily create your own additional event types:

log.new({
	fatal: { level: 5, event: "fatal", color: "rainbow" }
});

log.fatal( "program is dead" ); //outputs "fatal:  program is dead"

In the parameters passed to the new method, the key (debugger) is the method to be added to the module, where as the event property is what is going to be displayed in the log as the event type. Don't the get two confused!

You can also overwrite the default events (info, warn, error) with your own if you'd like to:

log.new({
	info: { color: 'cyan', level: 0, event: 'info' },
	notice: { color: 'yellow', level: 1, event: 'notice' },
	warn: { color: 'yellow', level: 2, event: 'warning' },
	error: { color: 'red', level: 3, event: 'ERROR' }
});

However, if all you you want to do is change some of the properties, you can also do the following:

log.info().config({ color: 'cyan' });

log.info('Hello World!'); //output should be cyan now

Make sure not to pass anything to the info method when you are configuring it, otherwise you'll probably get an error. This is the only ugly part of the syntax (hopefully!), I will probably fix it at a later time.

Log Formatting

Template

Using custom-logger you can also configure how the console output will look like. It uses some simple templating patterns to do so:

%event%     => the type of event being logged, such as "info" or "warning"
%message%   => the message to be displayed, such as "server started!"
%timestamp% => customizable datetime (see below) for when the event occured
%padding%   => space characters that help align the current %event%. See details sections below for more

By default the format string is:

%timestamp% - %event%:%padding%  %message%

Which produces the output:

01:08:24 - debug:     hello?
01:08:24 - info:      hello world!
01:08:24 - warning:   carefule there, world!
01:08:24 - error:     WHOA WHOA WHOA world?!

You can specify the output format like so:

var log = require('custom-logger').config({ format: "%event% %padding%[%timestamp%]: %message%" });

Which would output to:

debug   [01:06:26]:  hello?
info    [01:06:26]:  hello world!
warning [01:06:26]:  carefule there, world!
error   [01:06:26]:  WHOA WHOA WHOA world?!

If you were to remove %padding% from the format string, the output would be:

debug [01:10:12]:  hello?
info [01:10:12]:  hello world!
warning [01:10:12]:  carefule there, world!
error [01:10:12]:  WHOA WHOA WHOA world?!

I hope this helps illustrate the purpose of %padding%.

Timestamp

You can also modify the format of the actual %timestamp%. By default it's HH:MM:ss. You can change it as follows:

var log = require('custom-logger').config({ timestamp: "h:MM:ss TT" });

Which should look familiar to those familiar with date formatting in other programming languages. For more information on formatting the timestamp, see this. Since this modules relies on the dateFormat library, any values valid for dateFormat should be valid here too.

Overwriting Global Config

You can also overwrite global settings for log template and timestamp format based on individual event types, either when you create them or by modifying existing ones:

log.new({
	alert: { level: 3, color: 'red', event: 'alert', format: '!!!!!%event% : %message% !!!!!' }
});

log.error().config({ timestamp: "HH:MM:ss" });

Further Details

Colors

If you'd like to further customize colors or formatting of your events, take a look at the colors modules that this module depends on. Any color/formatting value that's valid for that module, should be fine to pass to the color parameter when creating/configuring an event type.

Padding

Say you have three event types with the event properties as follows: info, warning, error.

The %padding% value is going to be different for each of the events when they are displayed. The way it's calculated is that it finds the string length of the longest event, and subtracts it from the string length of the current event, and then returns a string containg that many spaces.

For examples, the longest string of the above events is warning, with 7 characters. Here's how it's being worked out:

info.length = 4     ===>  7 - 4 = 3  ===> "   "
warning.length = 7  ===>  7 - 7 = 0  ===> ""
error.length = 5    ===>  7 - 5 = 2  ===> "  "

If this confuses you, don't worry much about it, you don't have to use it. I simply included it to make outputing to the console prettier.

Message Formatting

Messages can be formatted using util.format by setting the config option messageFormatting to true. This defaults to false to ensure backward compatibility with versions prior to 0.3.1.

For example:

log.info("I see", "3", "bananas!") // Outputs "I see 3 bananas!"

log.config({
	messageFormatting: true
});

log.info("I see %d %s!", 3, "bananas"); // Outputs "I see 3 bananas!"