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

@daldalso/logger

v0.9.4-0

Published

Fancy logging library using tagged template literal

Downloads

80

Readme

@daldalso/logger

Fancy logging library using tagged template literal

Getting Started

  1. yarn add @daldalso/logger
  2. import { log } from "@daldalso/logger"
  3. log("Hello, World!")

Full examples are shown in example.ts.

Usage

Log levels

There are 5 built-in log levels below, and you can directly call them to log something.

  • log
  • info
  • success
  • warning
  • error

Example of log levels

import { log, info, success, warning, error } from "@daldalso/logger";

log("Log");
info("Info");
success("Success");
warning("Warning");
error("Error");

Colors

There are 36 styles of decorating text and you can call col to use them.

Example of col

import { col, log } from "@daldalso/logger";

log(col.red`Red`);
log(col.bgBlue`Blue`);

You can make a call chain to apply multiple styles.

Example of col chain

import { col, log } from "@daldalso/logger";

log(col.italic.green`Italic and green`);
log("🚗💨 " + col.yellow.bgLBlack.underline`─────`);

Tagged template literal

The five functions can also be used as tagged template literals. In this case, expressions are handled depending on their type. For example, log(`Data: ${{ foo: 1 }}`); converts { foo: 1 } to a string and prints "Data: [object Object]", but log`Data: ${{ foo: 1 }}`; treats it as an object and prints:

Example of tagged template literal for an object

When you want to print just an object without any template literals, call log(object) and it would be handled as its type.

Values

Most of object types are handled for better appearance.

Example of values

import { log } from "@daldalso/logger";

log([ 1, 2, 3 ]);
log(new Map([ [ "foo", /bar/g ], [ log, true ] ]));
log(Promise.resolve("foo"));

[!IMPORTANT]

In a general way, retrieval of the state of Promises can't be synchronous. To bypass the limit, Logger calls Promise.race with Promise.resolve() to check the state, which means logging a Promise is asynchronous.

In the above example, switching the execution order of log(new Map(...)) and log(Promise.resolve("foo")) won't change the result.

Object with circular references is marked as below:

Example of circular reference

import { log } from "@daldalso/logger";

const circularObject = { foo: 1 };
circularObject.this = circularObject;
log(circularObject);

const circularArray = [ "bar" ];
circularArray.push(circularObject, circularArray);
log(circularArray);

Labeling

When you call Logger with multiple arguments, it labels each argument with numbers like below:

Example of labeling

import { log } from "@daldalso/logger";

log("foo", "bar", "baz");

You can set the name of labels by calling any properties of its return value.

Example of setting label name

import { warning } from "@daldalso/logger";

warning("Loading is too long!").Task("Deleting redundant files")['⏱️']("100 seconds");

Styling

Logger's behavior can be configured by Logger.instance.setOptions. Actually, the above screenshots are not from the result of its default options, because the default options makes Logger print timestamps which are distracting for demonstration.

Example of styling

import { Logger, info, log } from "@daldalso/logger";

Logger.instance.setOptions({
  headings: {
    [LogLevel.VERBOSE]: col.black.bgLBlack`(LOG)`,
    [LogLevel.INFO]: col.black.bgCyan`(INF)`
  },
  headerFormat: col.lMagenta`[main]` + " $T $H "
});

log("Hello, World!");
info("How are you?");

Subscription

Logger calls console.log by default, but you can change this behavior with its subscription methods.

import { Logger, log } from "@daldalso/logger";

Logger.instance.removeSubscriber(1); // Removes default subscriber
Logger.instance.addSubscriber(
  (_, value) => console.error(value),
  { colored: true }
);
log("Hello, World!"); // This will be printed to stderr.

There are some functions that return a subscriber.

import { Logger, log, createDirectorySubscriber } from "@daldalso/logger";

Logger.instance.addSubscriber(
  createDirectorySubscriber("logs", { type: "time", interval: "daily" }),
  { colored: false } // Removes color data
);
log("Hello, World!"); // This will be printed to both stdout (by default) and a log file.

Instantiation

You can instantiate Logger to apply different configuration. Note that the instantiated Logger does not have any subscribers, which means you have to add one before logging with it.

import { Logger, createFileSubscriber } from "@daldalso/logger";

const fileLogger = new Logger({
  headerFormat: "$H "
});
fileLogger.addSubscriber(
  createFileSubscriber("example.log"),
  { colored: false }
);
fileLogger.verbose("Hello, World!"); // This won't be printed to stdout.