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

util.log

v0.0.45

Published

A simple logging utility module

Downloads

59

Readme

util.log

A simple logging utility module

build analysis code style: prettier testing NPM

A wrapper for output messages similar to log4j in java. This module is intended for node/electron environments as it will write to files in the local environment. It intercepts log messages and routes them to the console and to an output file. A special case exists for debug messages where it wraps the debug module.

Installation

This module uses yarn to manage dependencies and run scripts for development.

To install as an application dependency:

$ yarn add --dev util.log

To build the app and run all tests:

$ yarn run all

Usage

import logger from 'util.log';

const log = logger.instance();
log.info('This is a log message');

Uses the default options and logs an info level message to a file and to the console. The messages passed to each logging function can contain variables. They wrap the sprintf.js module and allow dynamic formatting of the message. See that module for formatting options.

import logger from 'util.log';

const log = logger.instance();

const x: number = 42;
log.info('This is a log message: %d', x);

This code writes the log message to info and take in the dynamic x parameter as part of the outut string. The next example shows how to override the default configuration:

import logger from 'util.log';

const log = logger.instance({
	directory: '/var/log/app',
	toConsole: false
});

log.info('This is a log message');

Similar to the first example. This overrides the default configuration and changes the output directory where the log files will be written. This will write the log file /var/log/app/messages.log. It also turns off console logging so the message is only written to the file.

import logger from 'util.log';

const log = logger.instance();
log.event('EVT_BUTTON_PRESSED', 'button onClick()');

Writes an event message to the ./logs/events.log file. The EVT_BUTTON_PRESSED is a string id associated with the event. It's just a string that can be used to represent the event name, but allows for routing/watching events segregated from the other messaging. Note that all messages are written to the main log file, but that events have a special designation.

All of the instances above are using the default (or root) logger. The configuration options are associated with this namespace. This behavior can be changed by using the namespace option when retrieving an instance.

import logger from 'util.log';

const log = logger.instance({
	namespace: 'something',
	directory: '/var/log/app'
});

log.info('This is a log message');

This logging instance is associated with the something configuration namespace. The default configuration still exists and an instance of it can be retrieved and used. This allows multiple logging configurations to coexist by namespace. If no namespace is given, then the value 'default' is used. If an undefined namespace is given, then a UUID is generated and assigned as the namespace.

Example Output

Example Output

Configuration

The .instance() method accepts the following parameters as an object to change the internal configuration:

  • colors - Uses the chalk library to add color to the logging level in the messages. This is on by default. It adds ASCII escape sequences around the level to provide color (see chalk module).
  • dateFormat - the timestamp format used in strftime. The default is %Y-%m-%d @ %H:%M:%S:%L.
  • debug - a boolean flag that turns the debug log messages on or off. If true, then the logger will produce debug messages. The default is false.
  • directory - the directory location for the messages and event log files. The default is the current directory with logs prepended to it.
  • enabled - a boolean flag that turns the logger on or off. If true, then the logger will produce messages, otherwise all messages are suppressed and no output is generated. The default is true.
  • eventFile - the output file name for all event messages. Any message sent to log.event() will be placed in this log file.
  • messageFile - the output file name for all debug|info|warning|error messages. The default is messages.log.
  • namespace - a unique name given to a logger instance. By default is is default if no value is given when the instance is requested.
  • nofile - if this is true, then no file output will be generated. Only console output. The module creates the files by default (set to false)
  • nsWidth - the output message prints the namespace value. It is truncated at a maxium length determined by this value. The default is 15 characters. If it is lesss than this size, then it is right padded with spaces. If the value is set to -1 then the namespace value is set to the width of the namespace (the default).
  • toConsole - a boolean flag that turns console logging on or off. If true, then the message written to the log is also written to console.log/error, otherwise it the message is suppressed from the console. The default is true.
  • useConsoleDebug - a boolean flag that routes debug messages to console instead of the debug package. This is false by default (using the debug module)

API

The module contains the following instance functions (after a successful call to .instance()):

  • .debug({string}[, args]) - writes a debug message to the log. This message is written in gray.
  • .error({string}[, args]) - prints an error message to the log. If the console logging is enabled, then it also writes to console.error. These are written in red when color is enabled.
  • .event(id, [{string}, args]) - writes an event message (these are typically for react/redux events). Used to track actions as they occur. These are written to both the messages and the events log. They are written in blue when color is enabled.
  • .info({string}[, args]) - writes an info message to the log. These are written in green when color is enabled.
  • .instance([{}]) - retrieves an instance of a logger. This takes the configuration for this instance as an optional parameter.
  • .warn({string}[, args]) or warning() - prints a warning message to the log. These are written in yellow when color is enabled.