Glogg Global Logger
Glogg is a global logging utility designed for Node.js environments, primarily associated with the Gulp.js build system. It offers a straightforward, event-emitter-based interface for namespaced logging. The current stable version is 2.2.0. Releases are infrequent but occur when features or maintenance are required, typically in conjunction with updates within the Gulp ecosystem. A key differentiator is its reliance on `sparkles` (an augmented EventEmitter) to create loggers that emit events for different log levels (debug, info, warn, error). This architecture allows various parts of an application to retrieve a logger by its namespace and subscribe to specific log events, facilitating decoupled log handling. It supports `util.format()` for string interpolation, enabling printf-style logging, and can also log any data type directly. A critical operational aspect is that `error` events *must* be handled by at least one listener to prevent the Node.js process from crashing.
Common errors
-
ERR_REQUIRE_ESM: Must use import to load ES Module
cause Attempting to use `import getLogger from 'glogg';` in an ESM module context or a bundler that strictly enforces ESM.fixChange the import statement to `const getLogger = require('glogg');`. Ensure your project's `package.json` does not have `"type": "module"` if you intend to primarily use CommonJS modules without transpilation. -
Unhandled 'error' event (process exit)
cause A `logger.error()` method was called, but no listener was registered for the 'error' event on that logger instance.fixAdd an error event listener to your logger instance: `logger.on('error', (msg) => { console.error('Caught glogg error:', msg); /* Handle gracefully */ });`
Warnings
- breaking The minimum Node.js version requirement was updated to `>= 10.13.0`.
- gotcha The Node.js process will crash if an `error` event is emitted by a logger and there are no listeners attached to handle it.
- gotcha Glogg does not filter log levels internally. All calls to `debug`, `info`, `warn`, and `error` will emit their respective events. Filtering based on desired verbosity or environment must be implemented within your event listeners.
- gotcha Glogg is a CommonJS-only package. Attempting to use ESM `import` syntax will lead to runtime errors (`ERR_REQUIRE_ESM` or 'Cannot use import statement outside a module') in environments that don't transpile or handle CJS interoperability.
Install
-
npm install glogg -
yarn add glogg -
pnpm add glogg
Imports
- getLogger
import getLogger from 'glogg';
const getLogger = require('glogg'); - logger.on
logger.on('event', handler); - debug / info / warn / error methods
import { info } from 'glogg';const { info, error } = logger;
Quickstart
const getLogger = require('glogg');
// Create a logger for a specific namespace
const mainLogger = getLogger('my-app');
// Create another logger for a different module
const dataModuleLogger = getLogger('data-processor');
// Listen for 'info' events from the 'my-app' namespace
mainLogger.on('info', (message) => {
console.log(`[MainApp Info Listener]: ${message}`);
});
// IMPORTANT: You *must* handle 'error' events, or the process will crash.
mainLogger.on('error', (errMessage) => {
console.error(`[MainApp Error Handler]: CRITICAL! ${errMessage}`);
// In a real application, you might exit, log to a different system, etc.
});
// Log various types of messages
mainLogger.debug('This is a verbose debug message at %s.', new Date().toLocaleTimeString());
mainLogger.info('Application started successfully at %s.', new Date().toISOString());
mainLogger.warn('A configuration setting is missing, falling back to default.');
mainLogger.error('Failed to connect to database: Connection refused!');
// Log without string formatting - all arguments are emitted directly
dataModuleLogger.info({ event: 'data_processed', count: 123, status: 'success' });
dataModuleLogger.debug(['debug array', { key: 'value' }]);
// Demonstrates a message from a different logger
dataModuleLogger.warn('Data processing took longer than expected.');
// Output from a listener that applies custom filtering (conceptual)
// For instance, only process warnings from 'data-processor' in a specific way
dataModuleLogger.on('warn', (msg) => {
if (msg.includes('longer than expected')) {
console.log(`[Special Data Warn]: Specific issue noted: ${msg}`);
}
});
// Simulate another error (this would also crash if not handled by mainLogger's handler)
// mainLogger.error('Another critical failure point!');