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

mini-ansi-logger

v1.0.4

Published

A simple customizable logger for Node.js applications

Downloads

30

Readme

Mini ANSI Logger Documentation

Mini ANSI Logger

A simple, customizable logger for Node.js applications with ANSI color support.

npm version GitHub stars license


Table of Contents

Installation

npm install mini-ansi-logger

If you are using TypeScript, type definitions are included automatically.

Features

  • Supports multiple log levels: log, info, success, warn, error
  • Customizable colors for log messages using ANSI codes
  • Option to log messages to a file with a customizable path
  • Configurable logger instances
  • TypeScript support with type definitions included

Usage

Importing the Logger

// CommonJS
const { Logger, Colors } = require('mini-ansi-logger');

// ES Modules
import { Logger, Colors } from 'mini-ansi-logger';

Creating a Logger Instance

// Create a new logger instance with default configuration
const logger = new Logger();

// Create a logger with custom configuration
const customLogger = new Logger({ logPath: '/custom/log/path' });

Logging Messages

logger.log('This is a general log message');
logger.info('This is an informational message');
logger.success('Operation was successful');
logger.warn('This is a warning message');
logger.error('An error occurred');

Using Custom Colors

logger.log('This is a magenta log message', { color: Colors.FgMagenta });

Logging to a File

logger.info('This message will be logged to a file', { logToFile: true });

API Reference

Class: Logger

The Logger class provides methods to log messages with different levels and configurations.

Constructor

new Logger(config?: LoggerConfig)

Parameters:

  • config (optional): An object containing configuration options.

Methods

  • log(message: string, options?: LogOptions): void

    Logs a general message.

  • info(message: string, options?: LogOptions): void

    Logs an informational message.

  • success(message: string, options?: LogOptions): void

    Logs a success message.

  • warn(message: string, options?: LogOptions): void

    Logs a warning message.

  • error(message: string, options?: LogOptions): void

    Logs an error message.

  • configure(config: LoggerConfig): void

    Updates the logger's configuration.

Interfaces

LoggerConfig

Configuration options for the logger instance.

interface LoggerConfig {
  logPath?: string;
}

Properties:

  • logPath (optional): A string specifying the directory path where logs will be saved.

LogOptions

Options for individual log messages.

interface LogOptions {
  name?: string;
  logToFile?: boolean;
  isThrow?: boolean;
  color?: Colors;
}

Properties:

  • name (optional): A custom label for the log message.
  • logToFile (optional): A boolean indicating whether to log the message to a file.
  • isThrow (optional): If set to true in an error log, it will throw an exception.
  • color (optional): A color code from the Colors enum to customize the message color.

Enum: Colors

An enumeration of ANSI color codes for styling console output.

enum Colors {
  Reset = '\x1b[0m',
  Bright = '\x1b[1m',
  Dim = '\x1b[2m',
  Underscore = '\x1b[4m',
  Blink = '\x1b[5m',
  Reverse = '\x1b[7m',
  Hidden = '\x1b[8m',

  FgBlack = '\x1b[30m',
  FgRed = '\x1b[31m',
  FgGreen = '\x1b[32m',
  FgYellow = '\x1b[33m',
  FgBlue = '\x1b[34m',
  FgMagenta = '\x1b[35m',
  FgCyan = '\x1b[36m',
  FgWhite = '\x1b[37m',

  BgBlack = '\x1b[40m',
  BgRed = '\x1b[41m',
  BgGreen = '\x1b[42m',
  BgYellow = '\x1b[43m',
  BgBlue = '\x1b[44m',
  BgMagenta = '\x1b[45m',
  BgCyan = '\x1b[46m',
  BgWhite = '\x1b[47m',
}

Configuration

Logger Configuration

You can configure the logger instance by passing a LoggerConfig object when creating a new instance or by using the configure method.

// Setting configuration during instantiation
const logger = new Logger({ logPath: '/custom/log/path' });

// Updating configuration later
logger.configure({ logPath: '/another/log/path' });

Log Options

You can customize individual log messages using LogOptions.

logger.info('Custom log message', {
  name: 'CUSTOM',
  logToFile: true,
  color: Colors.FgCyan
});

Examples

Basic Logging

const logger = new Logger();

logger.log('This is a general log message');
logger.info('This is an info message');
logger.success('This is a success message');
logger.warn('This is a warning message');
logger.error('This is an error message');

Customizing Log Messages

logger.log('Custom color log message', {
  color: Colors.FgMagenta
});

logger.error('This error will throw an exception', {
  isThrow: true
});

Logging to a File

const logger = new Logger({ logPath: './logs' });

logger.warn('This warning will be logged to a file', {
  logToFile: true
});

Using Multiple Logger Instances

const logger1 = new Logger({ logPath: './logs/logger1' });
const logger2 = new Logger({ logPath: './logs/logger2' });

logger1.info('Message from logger1', { logToFile: true });
logger2.info('Message from logger2', { logToFile: true });

License

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


Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Questions

If you have any questions or need further assistance, feel free to open an issue or contact the maintainer.