Skip to content

πŸš€ lightweight logging β€’ supports level based filtering and tagging β€’ weighs in at around 500 bytes

License

Notifications You must be signed in to change notification settings

rmartone/missionlog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

missionlog

NPM version Coverage Status

πŸš€ missionlog is a lightweight, structured logging package designed for performance, flexibility, and ease of use. It works as a drop-in replacement for console.log or ts-log, and offers both log level filtering, optional tag filtering, and customizable output handlingβ€”all in a tiny (~1KB) package.

βœ” Fully Typed (TypeScript) β€’ βœ” ESM & CJS Support β€’ βœ” Zero Dependencies β€’ βœ” 100% Coverage


✨ Why Use missionlog?

βœ… Drop-in Replacement for console.log & ts-log – Start using it instantly!

βœ… Seamless Upgrade to Tagged Logging – Reduce log clutter and focus on what's important.

βœ… Configurable Log Levels – Adjust visibility for log level and tags at runtime.

βœ… Customizable Output – Send logs anywhere: console, JSON, cloud services.

βœ… Structured Logging Support – Enhanced callbacks with timestamp and typed message data.

βœ… Blazing Fast Performance – O(1) log level lookups with advanced level caching.

βœ… TypeScript-First – Full type safety with LogMessage and LogConfig interfaces.

βœ… Chainable API – All methods return the logger instance for method chaining.

βœ… Works Everywhere – Browser, Node.js, Firebase, AWS Lambda etc.

πŸ“¦ Installing

npm i missionlog

πŸš€ Getting Started

Basic Usage

Missionlog works as a drop-in replacement for console.log:

import { log } from "missionlog";

// Works just like console.log
log.info("Hello, world!");
log.warn("Warning message");
log.error("Error occurred!");

// Chainable API for fluent logging
log.debug("Starting process")
   .info("Process step 1 complete")
   .warn("Process running slowly");

Using Tags for Categorization

import { log, tag, LogLevel, DEFAULT_TAG } from "missionlog";

// Configure logging levels for different tags
log.init({
  network: LogLevel.DEBUG,
  ui: LogLevel.INFO,
  [DEFAULT_TAG]: LogLevel.WARN  // Default level for uncategorized logs
});

// Log with tags
log.debug(tag.network, "Connection established");
log.info(tag.ui, "Component rendered");

// Untagged logs use the DEFAULT_TAG level
log.debug("This won't be logged because DEFAULT_TAG is WARN");
log.error("This will be logged because ERROR > WARN");

Custom Log Handler

import { log, LogLevel, LogLevelStr, LogCallbackParams } from "missionlog";
import chalk from "chalk";

// Create a custom log handler
function createCustomHandler() {
  const logConfig: Record<
    LogLevelStr,
    { color: (text: string) => string; method: (...args: unknown[]) => void }
  > = {
    ERROR: { color: chalk.red, method: console.error },
    WARN: { color: chalk.yellow, method: console.warn },
    INFO: { color: chalk.blue, method: console.log },
    DEBUG: { color: chalk.magenta, method: console.log },
    TRACE: { color: chalk.cyan, method: console.log },
    OFF: { color: () => '', method: () => {} }
  };

  return (level: LogLevelStr, tag: string, message: unknown, params: unknown[]) => {
    const { method, color } = logConfig[level];
    const logLine = `[${color(level)}] ${tag ? tag + ' - ' : ''}${message}`;
    method(logLine, ...params);
  };
}

// Initialize with custom handler
log.init(
  { network: LogLevel.INFO, [DEFAULT_TAG]: LogLevel.INFO },
  createCustomHandler()
);

// Enhanced structured logging with timestamps and typed data
log.setEnhancedCallback((params: LogCallbackParams) => {
  const { level, tag, message, timestamp, params: extraParams } = params;
  console.log(
    `[${timestamp.toISOString()}] [${level}] ${tag ? tag + ' - ' : ''}${message}`,
    ...extraParams
  );
});

// Check if a level is enabled before expensive logging operations
if (log.isLevelEnabled(LogLevel.DEBUG, 'network')) {
  // Only perform this expensive operation if DEBUG logs will be shown
  const stats = getNetworkStatistics(); // Example of an expensive operation
  log.debug(tag.network, 'Network statistics', stats);
}

πŸ“ API Reference

Log Methods

  • log.trace(messageOrTag?, ...params) - Lowest verbosity level
  • log.debug(messageOrTag?, ...params) - Detailed debugging information
  • log.info(messageOrTag?, ...params) - Notable but expected events
  • log.log(messageOrTag?, ...params) - Alias for info()
  • log.warn(messageOrTag?, ...params) - Potential issues or warnings
  • log.error(messageOrTag?, ...params) - Error conditions

Configuration

  • log.init(config?, callback?) - Configure log levels and custom handler
  • log.setEnhancedCallback(callback) - Set structured logging callback with extended parameters
  • log.isLevelEnabled(level, tag?) - Check if a specific level would be logged for a tag
  • log.reset() - Clear all tag registrations and configurations

Log Levels (in order of verbosity)

  1. LogLevel.TRACE - Most verbose
  2. LogLevel.DEBUG
  3. LogLevel.INFO - Default level
  4. LogLevel.WARN
  5. LogLevel.ERROR
  6. LogLevel.OFF - No logs

πŸ–ΌοΈ Example Output

Example Image


πŸ“„ License

MIT License Β© 2019-2025 Ray Martone


πŸš€ Install missionlog today and make logging clean, structured, and powerful!

About

πŸš€ lightweight logging β€’ supports level based filtering and tagging β€’ weighs in at around 500 bytes

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published