Node.js Log Utility
log-util is a minimalist Node.js logging utility designed for terminal output. It provides a simple API for common log levels such as debug, info, success, warn, and error, allowing developers to categorize and display messages with distinct visual feedback in the console. The current stable version is 2.3.0. The package appears to be in a maintenance or stable state, with its last publish date being approximately six years ago (as of April 2026), indicating a lack of recent active development or frequent feature additions. Its primary differentiator is its simplicity and small footprint, offering basic logging capabilities without the extensive configuration or advanced features found in more comprehensive logging frameworks. It ships with TypeScript type definitions, enabling a typed development experience for users.
Common errors
-
TypeError: log.debug is not a function
cause Attempting to use `log.debug` (or other methods) after an incorrect ESM import, where `log` might be `undefined` or not the expected object.fixEnsure you are using `import log from 'log-util';` for ESM, or `const log = require('log-util');` for CommonJS. If using `import * as log from 'log-util'`, methods might be nested, e.g., `log.default.debug`. -
SyntaxError: Cannot use import statement outside a module
cause This error occurs when using ESM `import` syntax in a Node.js file that is being treated as a CommonJS module (default behavior for `.js` files without explicit configuration).fixTo use ESM imports, either rename your file to `.mjs`, or add `"type": "module"` to your project's `package.json` file. Alternatively, switch back to CommonJS `require()` syntax for this package. -
ReferenceError: require is not defined
cause This happens when `require()` is called in a file that Node.js is treating as an ES Module (e.g., `.mjs` file or `"type": "module"` in `package.json`), where `require` is not globally available.fixFor ES Modules, use the `import` statement: `import log from 'log-util';`. If you need to load CommonJS modules within an ES module, consider using `import { createRequire } from 'module'; const require = createRequire(import.meta.url);` as a workaround for specific cases.
Warnings
- gotcha The package is primarily designed for CommonJS (`require`) usage, and its ESM (`import`) interoperability relies on Node.js's module resolution for CommonJS default exports. Direct ESM usage might lead to unexpected behavior in certain Node.js environments or older bundlers without proper configuration.
- gotcha The `log.setLevel()` method globally modifies the logging level for all subsequent `log` calls (unless a new `Log` instance is created). This can lead to unexpected filtering if different parts of an application expect independent logging verbosity.
- deprecated The `log-util` package has not seen updates in approximately six years (since its last publish date in June 2019). While functional, it may lack modern features, performance optimizations, or compatibility with newer Node.js versions or TypeScript constructs compared to actively maintained logging libraries.
Install
-
npm install log-util -
yarn add log-util -
pnpm add log-util
Imports
- log (CommonJS)
import log from 'log-util';
const log = require('log-util'); - log (ESM)
const log = require('log-util');import log from 'log-util';
- LogUtil (Type)
import type { LogUtil } from 'log-util';
Quickstart
import log from 'log-util';
// Set the global log level to 'info'
log.setLevel('info');
console.log('--- Default Logger (Level: Info) ---');
log.debug('This is a debug message (should not appear)');
log.info('This is an info message (level: 1)', { user: 'Alice' });
log.success('Operation completed successfully (level: 2)');
log.warn('A warning occurred (level: 3)');
log.error('An error happened (level: 4)', new Error('Something went wrong'));
// Create a new Log instance with a custom level
// The 'Log' constructor is exposed on the default 'log' object
const customLogger = new log.Log(log.levels.debug);
console.log('\n--- Custom Logger (Level: Debug) ---');
customLogger.setLevel('debug'); // Set instance level to debug
customLogger.debug('This is a debug message from custom logger (level: 0)');
customLogger.info('Info from custom logger (level: 1)', [1, 2, 3]);
customLogger.success('Success from custom logger (level: 2)');
// Demonstrate changing level mid-execution
console.log('\n--- Changing Level on Default Logger ---');
log.setLevel('error');
log.info('This info message will not appear now.');
log.error('Only error messages will show (level: 4) after changing level.');