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

cout-colors

v1.0.2

Published

````markdown # Cout Package

Downloads

1

Readme

# Cout Package

## Introduction

The `Cout` package provides a simple and effective way to log messages in Node.js applications with color-coded outputs. It leverages the `colors/safe` library to add color to console messages, making logs easier to read and distinguish.

## Installation

To use the `Cout` package, you need to install the `colors` package. You can do this using npm:

```bash
npm install colors
```

Usage

First, require the colors/safe package and enable colors:

var colors = require("colors/safe");
colors.enable();

Next, you can create an instance of the Cout class:

const logger = new Cout("MyApp", "LoggerClass");

Logging Methods

The Cout class provides various methods to log messages in different colors:

  • General Logging:

    • log(message: any): Logs a message in green.
    • warn(message: any): Logs a message in yellow.
    • error(message: any): Logs a message in red and prints a stack trace if the input is an error.
  • Color-specific Logging:

    • black(message: string): Logs a message in black.
    • red(message: string): Logs a message in red.
    • green(message: string): Logs a message in green.
    • yellow(message: string): Logs a message in yellow.
    • blue(message: string): Logs a message in blue.
    • magenta(message: string): Logs a message in magenta.
    • cyan(message: string): Logs a message in cyan.
    • white(message: string): Logs a message in white.
    • gray(message: string): Logs a message in gray.
  • Special Effects:

    • rainbow(message: string): Logs a message in rainbow colors.
    • zebra(message: string): Logs a message in zebra stripes.
    • america(message: string): Logs a message in America colors.
    • trap(message: string): Logs a message with a trap effect.
    • random(message: string): Logs a message in random colors.
    • zalgo(message: string): Logs a message with Zalgo text.

Example

Here is a complete example demonstrating how to use the Cout class:

var colors = require("colors/safe");
colors.enable();

class Cout {
    private appName: string;
    private pid: number;
    private className: string;

    constructor(appName: string = "Nodejs", className: string = "Cout") {
        this.appName = appName;
        this.className = className;
        this.pid = process.pid;
    }

    log(inpt: any) {
        this._log(inpt.message || inpt, "green", "LOG");
    }

    warn(inpt: any) {
        this._log(inpt.message || inpt, "yellow", "WARN");
    }

    error(inpt: any) {
        this._log(inpt.message || inpt, "red", "ERROR");

        if (inpt instanceof Error) {
            //@ts-ignore
            console.trace(inpt.details || inpt.cause || inpt);
        }
    }

    black(message: string) {
        this._log(message, "black", "LOG");
    }
    red(message: string) {
        this._log(message, "red", "LOG");
    }
    green(message: string) {
        this._log(message, "green", "LOG");
    }
    yellow(message: string) {
        this._log(message, "yellow", "LOG");
    }
    blue(message: string) {
        this._log(message, "blue", "LOG");
    }
    magenta(message: string) {
        this._log(message, "magenta", "LOG");
    }
    cyan(message: string) {
        this._log(message, "cyan", "LOG");
    }
    white(message: string) {
        this._log(message, "white", "LOG");
    }
    gray(message: string) {
        this._log(message, "gray", "LOG");
    }

    rainbow(message: string) {
        this._log(message, "rainbow", "LOG");
    }
    zebra(message: string) {
        this._log(message, "zebra", "LOG");
    }
    america(message: string) {
        this._log(message, "america", "LOG");
    }
    trap(message: string) {
        this._log(message, "trap", "LOG");
    }
    random(message: string) {
        this._log(message, "random", "LOG");
    }
    zalgo(message: string) {
        this._log(message, "zalgo", "LOG");
    }

    private _log(message: string, color: string = "cyan", type = "LOG") {
        const colorFn = (colors as any)[color];

        console.log(
            "\n",
            //@prettier-ignore
            colorFn(this.appName),
            " - ",
            `${new Date().toLocaleString()}`,
            "  ",
            colorFn(type),
            " ",
            colors.yellow.bold(`[${this.className}]`),
            " ",
            colorFn(message),
        );
    }
}

// Create an instance of Cout
const logger = new Cout("MyApp", "LoggerClass");

// Log messages with different severity levels
logger.log("This is a log message.");
logger.warn("This is a warning message.");
logger.error(new Error("This is an error message."));

// Log messages with different colors
logger.green("This message is green.");
logger.red("This message is red.");
logger.blue("This message is blue.");

By using the Cout class, you can enhance your Node.js application's logging functionality, making it more readable and visually appealing.