πŸ’Ύ Logging

Learn about Toolkitify Logger.

What is Logger?

Toolkitify Logger is a lightweight, flexible logging utility designed to help developers debug and monitor their code efficiently. It improves the default logging of programming languages by adding features like frequency control, maximum uses, colored output, and log levels, all while keeping resource usage low.

Toolkitify is an open-source project, meaning it’s free to use and doesn’t require an account.


Logger Features

  • Log Levels: DEBUG, INFO, WARN, ERROR.
  • Colored Output: Supports standard colors (red, green, yellow, blue, magenta, cyan, white) or custom hex colors.
  • Frequency: Log messages only every N occurrences.
  • Max Uses: Limit how many times a message can be logged globally or per message.
  • Easy Integration: Works out-of-the-box with any JavaScript/TypeScript project.

Usage Example

Logger Demo
import { Logger } from "toolkitify/logger";

const logger = new Logger({
  level: "DEBUG",    // Minimum log level.
  frequency: 1,      // Default log every 1 message.
  maxUses: Infinity  // Default unlimited uses.
});

// Normal logs.
logger.debug("Debug message");          // Will be shown.
logger.info("Info message");            // Will be shown.

// Logs with frequency: only log every 3 calls.
for (let i = 1; i <= 6; i++) {
  logger.info("Freq message", { frequency: 3 });
  // Will only be shown on iterations 3 and 6.
}

// Logs with color.
logger.warn("Warning in red", { color: "red" });      // Will be shown in red.
logger.info("Info in green", { color: "green" });    // Will be shown in green.

// Logs with global maxUses.
const limitedLogger = new Logger({ maxUses: 2 });

limitedLogger.info("Limited message"); // Will log.
limitedLogger.info("Limited message"); // Will log.
limitedLogger.info("Limited message"); // Will not log, maxUses reached.

// Logs with maxUses for a specific message.
limitedLogger.info("Single-use message", { maxUses: 1 }); // Will log.
limitedLogger.info("Single-use message", { maxUses: 1 }); // Will not log.

How It Works

  1. Level Filtering: Messages below the configured log level are ignored.
  2. Frequency Control: Only logs every Nth occurrence of a message.
  3. Max Uses: Ensures that logs are not repeatedly spammed.
  4. Color Formatting: Adds colored output to make logs easier to read in the console.

Toolkitify Logger is perfect for developers looking for a clean, configurable, and colorful logging system without adding heavy dependencies to their projects.

Interfaces

Logger Methods

MethodTypeRequiredDescription
debugLogging Functionβœ…Logs a debug-level message
infoLogging Functionβœ…Logs an info-level message
warnLogging Functionβœ…Logs a warning-level message
errorLogging Functionβœ…Logs an error-level message

Logging Function

ParameterTypeRequiredDescription
messageStringβœ…Message to log
optionsLogOptions❌Options for the log message

LogOptions

ParameterTypeRequiredDescription
frequencyNumber❌Log only every N occurrences (default 1)
colorColor❌Color of the log message (named or hex)
maxUsesNumber❌Maximum times this message should be logged

Logger Constructor Options

ParameterTypeRequiredDescription
levelLogLevel❌Minimum log level (DEBUG | INFO | WARN | ERROR)
frequencyNumber❌Default log frequency (logs every N messages)
maxUsesNumber❌Default maximum uses for all messages
flushIntervalNumber❌Default flush interval in milliseconds
asyncFlushBoolean❌Default async flush

LogLevel

ValueDescription
DEBUGUsed for detailed debug messages
INFOGeneral information messages
WARNWarnings that require attention
ERRORError messages indicating failures

Color

ValueDescription
greenStandard green text
redStandard red text
yellowStandard yellow text
blueStandard blue text
magentaStandard magenta text
cyanStandard cyan text
whiteStandard white text
#RRGGBBAny hex color, e.g., #ff00ff