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

@jfabello/log-to-console

v1.0.1

Published

Console logger with coloring and configurable log levels for Node.js.

Downloads

38

Readme

Console logger with coloring and configurable log levels for Node.js

License: MIT

Table of Contents

Installation

You can install this module via npm:

npm install @jfabello/log-to-console

Usage

To use the log-to-console module, first import it into your code and then create an instance of the ConsoleLogger class.

Here are some examples:

Basic Usage

const ConsoleLogger = require('@jfabello/log-to-console');

// Creates a new console logger instance
const logToConsole = new ConsoleLogger(ConsoleLogger.DEBUG);

// Writes an info message to the console.
logToConsole.info("This is an info message.");
logToConsole.same("This is the continuation of the info message above.");

Using Multiple Message Types

const ConsoleLogger = require('@jfabello/log-to-console');

// Creates a new console logger instance
const logToConsole = new ConsoleLogger(ConsoleLogger.DEBUG);

// Writes several messages to the console.
logToConsole.debug("This is a debug message.");
logToConsole.same("This is the continuation of the debug message above.");
logToConsole.info("This is an info message.");
logToConsole.same("This is the continuation of the info message above.");
logToConsole.warning("This is a warning message.");
logToConsole.same("This is the continuation of the warning message above.");
logToConsole.error("This is an error message.");
logToConsole.same("This is the continuation of the error message above.");

Changing the Log Level

const ConsoleLogger = require('@jfabello/log-to-console');

// Creates a new console logger instance
const logToConsole = new ConsoleLogger(ConsoleLogger.DEBUG);

// Writes several messages to the console.
// All the messages will be displayed because the log level is set to DEBUG.
logToConsole.debug("This is a debug message.");
logToConsole.same("This is the continuation of the debug message above.");
logToConsole.info("This is an info message.");
logToConsole.same("This is the continuation of the info message above.");
logToConsole.warning("This is a warning message.");
logToConsole.same("This is the continuation of the warning message above.");
logToConsole.error("This is an error message.");
logToConsole.same("This is the continuation of the error message above.");

// Sets the console logger instance log level to WARNING
logToConsole.logLevel = ConsoleLogger.WARNING;

// Writes several messages to the console.
// Only the warning and error messages will be displayed because the log level is set to WARNING.
logToConsole.debug("This is a debug message.");
logToConsole.same("This is the continuation of the debug message above.");
logToConsole.info("This is an info message.");
logToConsole.same("This is the continuation of the info message above.");
logToConsole.warning("This is a warning message.");
logToConsole.same("This is the continuation of the warning message above.");
logToConsole.error("This is an error message.");
logToConsole.same("This is the continuation of the error message above.");

The ConsoleLogger Class

Console logger with coloring and configurable log levels.

Static Properties

  • DEBUG: Read-only constant representing the debug logging level (4).
  • INFO: Read-only constant representing the information logging level (3).
  • WARNING: Read-only constant representing the warning logging level (2).
  • ERROR: Read-only constant representing the error logging level (1).
  • NO_LOGGING: Read-only constant representing the no logging level (0).

Instance Properties

  • logLevel: Gets or sets the current console logger instance log level.

Instance Methods

constructor(logLevel = 1)

Creates a new instance of the console logger.

Parameters
  • logLevel (number): The desired console logger instance log level. 4 = Debug, 3 = Info, 2 = Warning, 1 = Error, 0 = No logging. Defaults to 1.
Throws
  • TypeError: If the provided log level is not an integer.
  • RangeError: If the provided log level is not between 0 and 4.

debug(message, ...rest)

Logs a debug message to the console if the log level is set to DEBUG or higher.

Parameters
  • message (string): The debug message. Must be a string.
  • ...rest (any): Extra parameters.
Returns
  • this: The console logger instance.
Throws
  • TypeError: If the message is not a string.

info(message, ...rest)

Logs an information message to the console if the log level is set to INFO or higher.

Parameters
  • message (string): The information message. Must be a string.
  • ...rest (any): Extra parameters.
Returns
  • this: The console logger instance.
Throws
  • TypeError: If the message is not a string.

warning(message, ...rest)

Logs a warning message to the console if the log level is set to WARNING or higher.

Parameters
  • message (string): The warning message. Must be a string.
  • ...rest (any): Extra parameters.
Returns
  • this: The console logger instance.
Throws
  • TypeError: If the message is not a string.

error(message, ...rest)

Logs an error message to the console if the log level is set to ERROR or higher.

Parameters
  • message (string): The error message. Must be a string.
  • ...rest (any): Extra parameters.
Returns
  • this: The console logger instance.
Throws
  • TypeError: If the message is not a string.

same(message, ...rest)

Logs a message to the console with the same log level as the previous message.

Parameters
  • message (string): The message. Must be a string.
  • ...rest (any): Extra parameters.
Returns
  • this: The console logger instance.
Throws
  • TypeError: If the message is not a string.

Testing

To run the tests for this module, first clone the repository using the following command:

git clone https://github.com/jfabello/log-to-console.git

Then, navigate to the project directory and install the npm dependencies:

cd log-to-console
npm install

Finally, run the tests using the following command:

npm test

Contributing

Unfortunately, we are not able to accept contributions at this time.

If you find a bug in the code, please open an issue.

Thank you for your understanding.

License

This project is licensed under the MIT License. See the LICENSE file for details.