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

clog-utils

v2.0.1

Published

Enhance Node.js console logging with clog-utils. Customize log presets, style messages, and reduce clutter.

Downloads

11

Readme

Clog Utils - Custom Logging Library for Node.js

CodeQL GitHub

Clog Utils is a versatile logging library designed for Node.js applications. It empowers developers to create and apply log presets, change log styles, and customize log messages while still utilizing the familiar console.log functionality.

Features 🚀

  • Lightweight: Clog Utils is a lightweight logging library, ensuring minimal impact on your Node.js applications.
  • Zero Dependencies: Designed with zero external dependencies, keeping your project's dependencies clean and simple.
  • Optimized for Speed: Built for super-fast performance to minimize any logging overhead in your application.
  • Simple to Use: Easy-to-use API allows you to quickly customize log messages while still using console.log.
  • Compatible with Chalk and Similar Modules: Clog Utils seamlessly integrates with libraries like Chalk, enhancing your log messages with colorful styling.

Installation 📦

You can install the library via npm:

npm install clog-utils

Getting Started 🚀

To get started, follow these simple steps:

  1. Import the library based on your project's module system (ES/Typescript or CommonJS):
// ES/Typescript
import { clogUtils } from 'clog-utils';

// CommonJS
const { clogUtils } = require("clog-utils");
  1. Initialize the logger and configure presets:
const logger = new clogUtils({
  // Configuration options (see below for details)
  presets: {
    error: {
      prefix: '[error]',
      prefixcolor: 'red',
    },
    info: {
      prefix: '[info]',
      prefixcolor: '#a8c9e3',
    },
    updatedInfo: function() {
      returns {
        prefix: `[info - Date: ${Date.now()}]`,
        prefixcolor: '#a8c9e3',
      }
    }
  }
});

Now, you can use console.log as usual, and it will apply your custom presets and styles to your log messages.

console.log('This is a basic log message.');
console.log('This is an informational message.', 'info');
// or
console.log('This is an informational message.', { preset: "info" });
console.log('This is a warning message.', { preset: "Warning" });
// or you can set temporary presets
console.log('This is a warning message.',  { preset: { prefix: 'Warning', prefixcolor: 'Red' }});
console.log('This is a warning message.',  { preset: function() { returns { prefix: 'Warning', prefixcolor: 'Red' } } });
 
// You can also use logger.log instead of console.log if you prefer.

Anti-Spam Feature 🚫

Clog Utils includes an anti-spam feature that tracks duplicate console messages. You can experiment with this functionality using setInterval:

setInterval(() => {
 console.log('This is a counting message.', 'info'); // Output: [Info] This is a counting message. (number of duplicates)
}, 1000)

You can disable the Anti-Spam feature by adding the disableAntiSpam option to presets:

const logger = new clogUtils({
  presets: {
    error: {
      prefix: '[error]',
      prefixcolor: 'red',
      disableAntiSpam: true, // Default: false
    }
  }
});

Progress bar

The Progress Bar is a tool that helps you monitor the progress of your projects. It's especially useful when you want to visualize the completion status of a task or process.

// ES/Typescript
import { ProgressBar, progressbarPresets } from 'clog-utils';

// CommonJS
const { ProgressBar, progressbarPresets } = require("clog-utils");

const bar = new ProgressBar({
  maxValue: 100,
  minValue: 0,
  barDesign: { // Bar design is Optional
      progressBarChar: "█",
      emptyProgressBarChar: " ",
      progressBarCharSuffix: ">"
  }
  // Or you can use Progress bar Presets: 
  // barDesign: progressbarPresets.modern - (Others: modern2, legacy, legacy2, shades, emoji)
});

bar.start();

bar.update(50);
bar.update(70);
bar.update(100, "Its Finished!"); // this will write the text to the right of the Progress bar.

bar.end();

The bar.update() method also has an auto-stop feature, so using bar.stop() to stop the Progress Bar is not always necessary.

Progress Bar Preset Designs

You can choose from a variety of preset designs for your Progress Bar by specifying the barDesign property:

  • progressbarPresets.modern
  • progressbarPresets.modern2
  • progressbarPresets.legacy
  • progressbarPresets.legacy2
  • And more

Select a design that best suits your project's style.

Enjoy tracking your progress with the Progress Bar!

Usages 💡

Warn and Error Functions ⚠️❌

You can send warnings and errors with customized messages:

logger.warn("This is a warning");

logger.error("This is an error");
// Or for error objects
logger.error(new Error("This is an error"));

Restoring Original Logging 🔄

You can restore the original console logging behavior:

logger.restore();

You can also retrieve the original console.log implementation:

logger.setup();

Managing Presets 🎨

You can manage presets using the following methods:

Applying Presets ✔️

Presets define log prefixes and styles. You can apply presets like this:

logger.addPreset({ info: { prefix: '[Info]', prefixcolor: 'green' }});
// You can use Hex color codes too!
console.log('This is an informational message.', 'info');

Set Presets 📝

Set a collection of presets:

const presets = {
  info: { prefix: '[Info]', prefixcolor: 'green' },
  warning: { prefix: '[Warning]', prefixcolor: 'yellow' },
};

logger.setPresets(presets);

Remove Preset 🗑️

Remove a preset by name:

logger.removePreset('info');

Get Preset 🧐

Get the configuration of a preset:

const infoPreset = logger.getPreset('info');
console.log(infoPreset); // { prefix: '[Info]', prefixcolor: 'green', messageStructure: "%%prefix%% %%message%% %%counter%%" }

Get All Presets 📋

Get all presets as an array of objects:

const allPresets = logger.getAllPresets();
console.log(allPresets);
// Example output:
// [
//   { info: { prefix: '[Info]', prefixcolor: 'green', messageStructure: "%%prefix%% %%message%% %%counter%%" } },
//   { warning: { prefix: '[Warning]', prefixcolor: 'yellow', messageStructure: "%%prefix%% %%message%% %%counter%%" } },
//   { custom: { prefix: '[Custom]', prefixcolor: '\x1b[35m', messageStructure: "%%prefix%% %%message%% %%counter%%" } }
// ]

Message Structures 📝📜

Customize your log messages using message structures:

const logger = new clogUtils({
  presets: { 
    error: {
      prefix: '[error]',
      prefixcolor: 'red',
      messageStructure: "%%counter%% %%message%% %%prefix%%"
    },
    info: {
      prefix: '[info]',
      prefixcolor: '#a8c9e3',
      // Default is --> messageStructure: "%%prefix%% %%message%% %%counter%%"
    }
  }
});

/*
    Placeholders:

    %%prefix%% - Prefix with the prefixColor.
    %%prefixWithoutColor%% - Prefix without the prefixColor.
    %%message%% - Your message content.
    %%counter%% - Anti-spam feature's counter.
*/

console.log("This message is sent with a custom message structure.", "error") 
// Output: (0) This message is sent with a custom message structure. [error]

Console Saving 📝

Console Saving is a utility that allows you to save messages written to the console to a log file. This is useful for keeping a record of console output or debugging information.

const logger = new clogUtils({
    consoleSave: { 
      enabled: true, 
      fileName: `ConsoleSave_${Date.now()}`, // you can add .txt to the end if you want to
      path: "./logs" // Optional
    },
  presets: { 
    error: {
      prefix: '[error]',
      prefixcolor: 'red',
      messageStructure: "%%counter%% %%message%% %%prefix%%"
    },
    // Define more custom presets for your messages
  }
});

// Your application code...

// Console messages will be saved to the specified log file.

License 📄

This library is licensed under the Apache 2.0 License. See the LICENSE file for details.