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

@cmang/logger

v1.1.1

Published

## Overview This logger module provides a flexible and configurable logging mechanism for Node.js applications. It supports different log levels, custom log domains, and allows customization of the log output and format. It uses `chalk` for colored consol

Downloads

5

Readme

Logger Module

Overview

This logger module provides a flexible and configurable logging mechanism for Node.js applications. It supports different log levels, custom log domains, and allows customization of the log output and format. It uses chalk for colored console output and ensures that only one instance of the logger is created (singleton pattern).

Usage

Importing the Logger

import Logger, { LogLevel } from "./logger";

Getting the Logger Instance

To get the singleton instance of the logger, use:

const logger = Logger.getInstance();

Setting Logger Options

You can set options like log level, log domains, custom output function, and custom format function when getting the instance:

const logger = Logger.getInstance({
  logLevel: LogLevel.DEBUG,
  logDomains: ["APP", "DB"],
  customOutput: (logMessage) => {
    // Custom output logic
    console.log(logMessage);
  },
  customFormat: (level, domain, message) => {
    // Custom format logic
    return {
      level,
      domain,
      message,
      timestamp: new Date().toISOString(),
    };
  },
});

Logging Messages

The logger supports different log levels:

  • INFO
  • WARN
  • ERROR
  • DEBUG

You can log messages using the corresponding methods:

logger.info("APP", "This is an info message");
logger.warn("APP", "This is a warning message");
logger.error("APP", "This is an error message");
logger.debug("APP", "This is a debug message");

Example

import Logger, { LogLevel } from "./logger";

const logger = Logger.getInstance({
  logLevel: LogLevel.DEBUG,
  logDomains: ["APP", "DB"],
});

logger.info("APP", "Application has started");
logger.warn("DB", "Database connection is slow");
logger.error("APP", "Unhandled exception occurred");
logger.debug("DB", "Query executed: SELECT * FROM users");

Logger Options

logLevel

  • Specifies the minimum level of messages to log.
  • Type: LogLevel
  • Default: LogLevel.INFO

logDomains

  • Specifies the domains to log messages for.
  • Type: string[]
  • Default: [] (log all domains)

customOutput

  • Custom function for handling the log output.
  • Type: (logMessage: LogMessage, ...additionalArgs: any[]) => void

customFormat

  • Custom function for formatting log messages.
  • Type: (level: LogLevel, domain: LogDomain, message: string, ...additionalArgs: any[]) => LogMessage

Log Levels

export enum LogLevel {
  NONE = "none",
  INFO = "info",
  WARN = "warn",
  ERROR = "error",
  DEBUG = "debug",
}

API Reference

Logger.getInstance(options?: LoggerOptions): Logger

Returns the singleton instance of the logger, optionally configuring it with the provided options.

Logger.resetInstance(): void

Resets the logger instance. Useful for testing purposes.

Logger.log(level: LogLevel, domain: LogDomain, message: string, ...additionalArgs: any[]): void

Logs a message with the specified level and domain.

Logger.info(domain: LogDomain, message: string, ...additionalArgs: any[]): void

Logs an info message.

Logger.warn(domain: LogDomain, message: string, ...additionalArgs: any[]): void

Logs a warning message.

Logger.error(domain: LogDomain, message: string, ...additionalArgs: any[]): void

Logs an error message.

Logger.debug(domain: LogDomain, message: string, ...additionalArgs: any[]): void

Logs a debug message.

Testing

The logger module includes tests to ensure its functionality. The tests cover creating singleton instances, respecting log levels and domains, and using custom output and format functions.