Skip to content

Logging Plugin

The logging plugin provides console output for state changes, instance lifecycle events, and monitoring alerts.

Installation

bash
pnpm add @blac/logging-plugin

Quick setup

ts
import { LoggingPlugin } from '@blac/logging-plugin';
import { getPluginManager } from '@blac/core';

getPluginManager().install(
  new LoggingPlugin({ level: 'info' }),
  { environment: 'development' },
);

Configuration

Pass a LoggingPluginConfig object to the constructor:

ts
new LoggingPlugin({
  level: 'debug',
  format: 'grouped',
  include: ['CartCubit', 'AuthCubit'],
  logStateChanges: true,
  logLifecycle: true,
})

Options

OptionTypeDefaultDescription
level'minimal' | 'info' | 'debug' | 'verbose''info'Log verbosity
format'simple' | 'grouped''grouped'Output format. 'grouped' uses console.group
loggerLoggerconsoleCustom logger implementation
prefixstring'[BlaC]'Prefix for log messages
logLifecyclebooleantrueLog instance creation and disposal
logStateChangesbooleantrueLog state changes
includeCallstackbooleanfalseShow call stacks for state changes
includestring[]Whitelist: only log these class names
excludestring[]Blacklist: skip these class names
filterFilterFnCustom filter function

Monitoring options

OptionTypeDefaultDescription
instanceCountWarningThresholdnumber50Warn when instance count exceeds this
detectRapidLifecyclesbooleantrueDetect rapid create/dispose cycles
rapidLifecycleWindowMsnumber1000Time window for rapid lifecycle detection
rapidLifecycleThresholdnumber5Cycles in window to trigger warning

Log levels

LevelLifecycleState changesMonitoring
minimalNoNoYes
infoYesYesYes
debugYesYes (detailed)Yes
verboseYesYes (full)Yes

Filtering

By class name

ts
new LoggingPlugin({
  include: ['CartCubit', 'AuthCubit'],  // only these
  exclude: ['TimerCubit'],              // or skip these
})

Custom filter

ts
new LoggingPlugin({
  filter: (ctx) => {
    // ctx: { instance, className, instanceId, isIsolated }
    return !ctx.isIsolated; // skip isolated instances
  },
})

Custom logger

Replace console with your own logging implementation:

ts
new LoggingPlugin({
  logger: {
    log: (...args) => myLogger.info(...args),
    warn: (...args) => myLogger.warn(...args),
    error: (...args) => myLogger.error(...args),
    group: (label) => myLogger.group(label),
    groupEnd: () => myLogger.groupEnd(),
  },
})

Registry stats

Call logStats() to print a summary of the current registry state:

ts
const logging = new LoggingPlugin({ level: 'info' });
getPluginManager().install(logging);

// later, in a debug context:
logging.logStats();

Rate limiting

State change logging is automatically disabled if more than 1,000 changes per second are detected. This prevents flooding the console in high-frequency scenarios. A warning is logged when rate limiting kicks in.

Released under the MIT License.