Loglevel: Minimal Logging for JavaScript

1.9.2 · active · verified Sun Apr 19

loglevel is a minimal, lightweight logging library for JavaScript environments, including browsers and Node.js. Currently at version 1.9.2, it maintains a steady release cadence focusing on stability and reliability. Its core functionality extends standard `console.log` methods to provide level-based logging (trace, debug, info, warn, error) and filtering, while gracefully handling environments where `console` methods might be absent or limited. Key differentiators include its small footprint (1.4 KB minified and gzipped), lack of external dependencies, preservation of original stack trace line numbers (unlike many wrapper-heavy logging frameworks), and seamless integration with various module systems (CommonJS, AMD, global). It ships with built-in TypeScript definitions and ensures consistent logging behavior across diverse JavaScript runtimes, making it a reliable choice for everyday debugging without introducing complex features like appender reconfiguration or advanced filtering rules.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of loglevel, including setting log levels dynamically and outputting messages at various severities.

import log from 'loglevel';

// Default log level is 'warn'. To see all messages, set the level.
log.setLevel('trace');

// Or enable all messages
// log.enableAll();

// Or programmatically set a specific level
const currentEnv = process.env.NODE_ENV || 'development';
if (currentEnv === 'production') {
  log.setLevel('error');
} else {
  log.setLevel('debug');
}

log.trace('This is a trace message');
log.debug('This is a debug message');
log.info('This is an info message');
log.warn('This is a warning message');
log.error('This is an error message');

// Example of changing the level dynamically
setTimeout(() => {
  log.setLevel('info');
  log.info('Log level changed to info after 2 seconds.');
  log.debug('This debug message will now be hidden.');
}, 2000);

view raw JSON →