Loglevel: Minimal Logging for JavaScript
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
-
My log.debug() or log.info() messages are not appearing in the console.
cause The default log level for loglevel is 'warn', which means messages below this severity (info, debug, trace) are suppressed by default.fixCall `log.setLevel('debug');` (or 'info', 'trace', 'all') at the start of your application, for example, `import log from 'loglevel'; log.setLevel('debug');`. -
Webpack is reporting module resolution errors for 'loglevel'.
cause Using loglevel versions older than `1.6.4` with webpack could lead to issues due to an improperly qualified 'main' entry in the `package.json`.fixUpdate your `loglevel` package to `1.6.4` or a newer version: `npm install loglevel@latest`. -
TypeScript compile error: 'Property 'log' does not exist on type 'Global'.' or similar global type conflicts.
cause Before version `1.6.3`, loglevel's type definitions could clash with other libraries that define a global `log` type (like `core-js`).fixUpgrade `loglevel` to version `1.6.3` or higher: `npm install loglevel@latest`.
Warnings
- gotcha By default, loglevel's logging is filtered to 'warn' level. This means 'info', 'debug', and 'trace' messages will not appear unless you explicitly set a lower log level.
- gotcha When creating plugins that wrap loglevel's methods, you may lose the original stack trace line numbers in your console output. Loglevel explicitly avoids this issue with its core methods, but it's a trade-off for custom wrappers.
- breaking Versions of loglevel older than `1.6.4` might experience webpack build issues due to an unqualified path in `package.json`'s 'main' field.
- breaking Version `1.6.5` introduced bugs that caused issues in Node.js and Internet Explorer versions older than 9, which were subsequently fixed in `1.6.6`.
- gotcha Prior to version `1.6.3`, loglevel's global `log` type could conflict with other global `log` types (e.g., from `core-js`), leading to TypeScript compilation errors.
Install
-
npm install loglevel -
yarn add loglevel -
pnpm add loglevel
Imports
- log
import { log } from 'loglevel';import log from 'loglevel';
- log (CommonJS)
import log from 'loglevel';
const log = require('loglevel'); - LogLevelDesc
import { LogLevel } from 'loglevel';import log, { LogLevelDesc } from 'loglevel';
Quickstart
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);