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

justo-logger

v0.9.1

Published

A simple logger for Node.js.

Downloads

29

Readme

Build Status

A simple logger for Node.js.

Proudly made with ♥ in Valencia, Spain, EU.

Usage

npm install justo-logger

By convention, the package should be imported as log:

const log = require("justo-logger");

Table of contents

  1. Architecture
  2. Log levels
  3. Format patterns
  4. Console loggers
  5. Colored console loggers
  6. Loggers

Architecture

We have a unique component, the logger. A logger represents an object to write log entries. The logger is the component to receive the log messages. When the user registers a log message, the logger creates a log entry and writes it if the entry level is greater than or equal to the minimum level configured.

A log entry contains:

  • The source logger.
  • The level.
  • The timestamp, when the entry was created.
  • The message.

Here's an illustrative example:

//(1) create logger and writer
var logger = new ConsoleLogger("app", {minLevel: Level.INFO, maxLevel: Level.FATAL});

//(2) write log entries
logger.debug("My debug message");
logger.info("My info message");
logger.warn("My warn message");
logger.error("My error message");
logger.fatal("My fatal message");

The logger receives the entries DEBUG, INFO, WARN, ERROR and FATAL, but it doesn't write DEBUG, because we have configured it to write only the entries with level greater than or equal to INFO.

Levels

The log level indicates the importance of the entry, from minor to major:

  • DEBUG. Debug Message. Only used when development.
  • INFO. Informational message.
  • WARN. Warning message. For example, the use of something deprecated.
  • ERROR. Error message. Something has gone wrong, but the app or component can continue working.
  • FATAL. Fatal error message. Something has gone wrong and the app or component can't continue working.

By type of level, we must use the following logger methods:

logger.debug(msg)
logger.info(msg)
logger.warn(msg)
logger.error(msg)
logger.fatal(msg)

The methods can accept several arguments. Their concatenation is the message. Example:

logger.debug("This", "is", "the", "message");
//similar to
logger.debug("This is the message");

Format patterns

A format pattern is a string that describe the entry syntax.

Wildcards:

  • %s. The source logger name.
  • %l. The level. DEBUG, INFO, WARN, etc.
  • %t. The timestamp: yyyy-mm-dd hh:mm:ss.
  • %m. The message.

The default pattern is %l [%t]: %m.

Console loggers

A console logger writes to the console. It writes the DEBUG and INFO entries using console.log(), while the other levels with console.error().

Constructors:

constructor()
constructor(name : string)
constructor(opts : object)

The logger options are:

  • name (string). The logger name. Default: logger.
  • enabled (boolean). Must it write the entries? true, yep; false, nope. Default: true.
  • minLevel (Level or string). The minimum level to write.
  • maxLevel (Level or string). The maximum level to write.
  • pattern (string). The format pattern for all entries.
  • patterns (object). The format patterns for specified entries: debug (string), info (string), warn (string), error (string) and/or fatal (string).

Example:

const log = require("justo-logger");
const ConsoleLogger = log.logger.ConsoleLogger;
const Level = log.Level;

var logger = new ConsoleLogger({
  name: "app",
  minLevel: Level.INFO,
  maxLevel: Level.FATAL,
  pattern: "%l: %m",
  patterns: {
    info: "%m"
  }
});

The default format pattern is %l: %m, except for INFO that uses %m. The DEBUG entries won't be written, because the minimum level is INFO.

Colored console loggers

A colored console logger is similar to ConsoleLogger, but always uses console.log() and it writes the level name with colors.

Constructors:

constructor()
constructor(name : string)
constructor(opts : object)

The logger options are:

  • name (string). The logger name. Default: logger.
  • enabled (boolean). Must it write the entries? true, yep; false, nope. Default: true.
  • minLevel (Level or string). The minimum level to write.
  • maxLevel (Level or string). The maximum level to write.
  • pattern (string). The format pattern for all entries.
  • patterns (object). The format patterns for specified entries: debug (string), info (string), warn (string), error (string) and/or fatal (string).
  • theme (object). The color theme.

When a colored console logger is used, we have to create themes, objects with the colors for each level. Right now, we can use the following colors:

  • black
  • blue
  • cyan
  • gray
  • green
  • grey
  • magenta
  • red
  • white
  • yellow

Example:

const ColoredConsoleLogger = require("justo-logger").logger.ColoredConsoleLogger;
const Level = require("justo-logger").Level;

var logger = new ColoredConsoleLogger({
  name: "app",
  minLevel: Level.INFO,
  maxLevel: Level.FATAL,
  pattern: "%l: %m",
  patterns: {
    info: "%m"
  },
  theme: {
    debug: "gray",
    info: "white",
    warn: "yellow",
    error: "red",
    fatal: "red"
  }
});

Loggers

The Loggers class is an array of loggers. This array contains the following methods:

//add a logger
add(logger)

//invoke the *() method of its loggers
debug(msg)
info(msg)
warn(msg)
error(msg)
fatal(msg)

Example:

const log = require("justo-logger");
const Loggers = log.Loggers;
const ConsoleLogger = log.logger.ConsoleLogger;
const Level = log.Level;

var loggers = new Loggers();
logger.add(new ConsoleLogger({minLogger: Level: INFO}));
logger.add(new ColoredConsoleLogger({minLogger: Level: DEBUG}));

//entry written by the 2nd logger
loggers.debug("the message");

//entry written by all loggers
loggers.info("the message");