obug Lightweight Debugging Utility

2.1.1 · active · verified Sun Apr 19

obug is a lightweight JavaScript debugging utility, forked from the popular `debug` package, specifically engineered for modern JavaScript environments. It offers comprehensive TypeScript support and native ES Module (ESM) compatibility, making it suitable for contemporary Node.js applications and browser environments (ES2015+). The current stable version is 2.1.1, with recent updates focusing on performance enhancements and customizable features. Unlike its predecessor `debug`, obug prioritizes a minimal footprint (7.7 kB package size, 1.4 KB minified + gzipped for browsers) and zero external dependencies. Key differentiators include full TypeScript type definitions, native ESM support from the ground up, and an API optimized for modern runtimes. Version 2 introduced a significant rewrite, refactoring API imports and usage for improved modularity, customization, and further reduced package size compared to v1, which aimed for `debug` compatibility but dropped older runtime support.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates `obug` installation, creating debuggers, enabling/disabling namespaces, and using sub-namespaces.

import { createDebug, disable, enable, enabled, namespaces } from 'obug';

// Enable debug messages for 'my-namespace' and its sub-namespaces
enable('my-namespace*');

// Get the currently enabled namespaces
console.log('Enabled namespaces:', namespaces());

const debug = createDebug('my-namespace', {
  // All options are optional
  useColors: true, // false, true, undefined for auto-detect
  color: 2, // custom color (e.g., ANSI color code)
  // custom formatArgs (Node.js only)
  formatArgs(args) {
    // Example: prepend a timestamp
    args[0] = `[${new Date().toISOString()}] ${args[0]}`;
  },
  formatters: {}, // custom formatters (like debug.formatters)
  // Node.js only options
  inspectOpts: { depth: 4 },
  // custom log function
  log: console.log,
});

// This message will be logged because 'my-namespace' is enabled
debug('This is a debug message for %s', 'my-component');
console.log(
  'Namespace:', debug.namespace, // 'my-namespace'
  'Enabled:', debug.enabled,     // true
  'Using colors:', debug.useColors, // true
);

// Create a sub-namespace, inheriting options from the parent
const sub = debug.extend('sub-namespace');
sub('This is a sub-namespace debug message with an object: %o', { id: 123, status: 'active' });
console.log('Sub-namespace:', sub.namespace); // 'my-namespace:sub-namespace'

// Example of disabling debug messages
disable('my-namespace*');
console.log('Enabled after disable:', enabled('my-namespace')); // Should be false or empty

const debug2 = createDebug('another-namespace');
debug2('This message will NOT appear because its namespace is not enabled.');

view raw JSON →