or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

basic-logging.mdindex.mdlevel-control.mdmulti-logger.mdplugins.md
tile.json

tessl/npm-loglevel

Minimal lightweight logging for JavaScript, adding reliable log level methods to any available console.log methods

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/loglevel@1.9.x

To install, run

npx @tessl/cli install tessl/npm-loglevel@1.9.0

index.mddocs/

loglevel

Minimal lightweight logging for JavaScript, adding reliable log level methods to any available console.log methods across all environments (browsers, Node.js, etc.) with zero dependencies.

Package Information

  • Package Name: loglevel
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install loglevel

Core Imports

ES6 imports:

import log from 'loglevel';

For some tools, wildcard import:

import * as log from 'loglevel';

CommonJS:

const log = require('loglevel');

Direct usage (browser):

<script src="loglevel.min.js"></script>
<script>
  // Available as global 'log'
</script>

Basic Usage

import log from 'loglevel';

// Set the logging level (defaults to "warn")
log.setLevel("info");

// Use different logging levels
log.trace("Very detailed debug info");
log.debug("Debug information");
log.info("General information");
log.warn("Warning message");
log.error("Error occurred!");

// Disable all logging
log.disableAll();

// Enable all logging
log.enableAll();

Architecture

loglevel provides a simple yet powerful architecture with these key components:

  • Root Logger: The default logger instance with global methods
  • Named Loggers: Independent logger instances for modular applications
  • Level System: Hierarchical filtering (TRACE → DEBUG → INFO → WARN → ERROR → SILENT)
  • Plugin System: Extensible method factory for custom logging behavior
  • Persistence: Automatic level persistence via localStorage/cookies
  • Environment Adaptation: Graceful fallbacks across all JavaScript environments

Capabilities

Basic Logging

Core logging methods that provide reliable output across all JavaScript environments with proper fallbacks and line number preservation.

/**
 * Output trace message with full stack trace
 * @param {...any} msg - Any data to log to the console
 */
trace(...msg: any[]): void;

/**
 * Output debug message (alias: log)
 * @param {...any} msg - Any data to log to the console
 */
debug(...msg: any[]): void;

/**
 * Alias for debug method
 * @param {...any} msg - Any data to log to the console
 */
log(...msg: any[]): void;

/**
 * Output info message
 * @param {...any} msg - Any data to log to the console
 */
info(...msg: any[]): void;

/**
 * Output warning message
 * @param {...any} msg - Any data to log to the console
 */
warn(...msg: any[]): void;

/**
 * Output error message
 * @param {...any} msg - Any data to log to the console
 */
error(...msg: any[]): void;

Basic Logging Documentation

Level Control

Manage logging levels with persistence, providing fine-grained control over which messages are displayed.

/**
 * Log level constants
 */
interface LogLevel {
  TRACE: 0;
  DEBUG: 1;
  INFO: 2;
  WARN: 3;
  ERROR: 4;
  SILENT: 5;
}

/**
 * Possible log level numbers
 */
type LogLevelNumbers = 0 | 1 | 2 | 3 | 4 | 5;

/**
 * Possible log level names (lowercase)
 */
type LogLevelNames = 'trace' | 'debug' | 'info' | 'warn' | 'error';

/**
 * Valid log level descriptors
 * Accepts numbers, lowercase strings, uppercase strings, or 'silent'
 */
type LogLevelDesc = LogLevelNumbers | LogLevelNames | 'silent' | keyof LogLevel;

/**
 * Set the current logging level
 * @param {LogLevelDesc} level - Level name, number, or constant
 * @param {boolean} persist - Whether to persist the level (default: true)
 */
setLevel(level: LogLevelDesc, persist?: boolean): void;

/**
 * Get current logging level as number
 * @returns {LogLevelNumbers} Current level (0-5)
 */
getLevel(): LogLevelNumbers;

Level Control Documentation

Multi-Logger System

Create and manage multiple independent logger instances for modular applications.

/**
 * Create or retrieve a named logger instance
 * @param {string | symbol} name - Logger name (non-empty string or symbol)
 * @returns {Logger} Logger instance with independent level settings
 */
getLogger(name: string | symbol): Logger;

/**
 * Get dictionary of all created loggers
 * @returns {Object} Object with logger names as keys and Logger instances as values
 */
getLoggers(): { [name: string]: Logger };

/**
 * Release global 'log' variable and return loglevel instance
 * @returns {RootLogger} The loglevel root logger instance
 */
noConflict(): RootLogger;

/**
 * ES6 default export compatibility property
 * Points to the root logger instance for consistent import behavior
 */
default: RootLogger;

Multi-Logger Documentation

Plugin System

Extend loglevel with custom logging behavior through the method factory system.

/**
 * Method factory function type for creating logging methods
 * @param {string} methodName - Logging method name ('trace', 'debug', 'info', 'warn', 'error')
 * @param {number} level - Current logging level (0-5)
 * @param {string | symbol | undefined} loggerName - Name of the logger instance (undefined for root logger)
 * @returns {Function} Logging method implementation
 */
type MethodFactory = (methodName: string, level: number, loggerName: string | symbol | undefined) => (...message: any[]) => void;

/**
 * Plugin API entry point for custom method factories
 */
methodFactory: MethodFactory;

/**
 * Rebuild logging methods (for plugin developers)
 */
rebuild(): void;

Plugin System Documentation