{"id":10956,"library":"glogg","title":"Glogg Global Logger","description":"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.","status":"active","version":"2.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/gulpjs/glogg","tags":["javascript","global","log","logger","logging","shared"],"install":[{"cmd":"npm install glogg","lang":"bash","label":"npm"},{"cmd":"yarn add glogg","lang":"bash","label":"yarn"},{"cmd":"pnpm add glogg","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only. Attempting to use ESM `import` will result in a runtime error like 'ERR_REQUIRE_ESM' or 'Cannot use import statement outside a module'.","wrong":"import getLogger from 'glogg';","symbol":"getLogger","correct":"const getLogger = require('glogg');"},{"note":"Used to subscribe to log events ('debug', 'info', 'warn', 'error'). Attaching a listener to the 'error' event is critical to prevent the Node.js process from crashing on unhandled errors.","symbol":"logger.on","correct":"logger.on('event', handler);"},{"note":"These methods (e.g., `logger.debug()`, `logger.info()`) are properties of the logger instance returned by `getLogger()`. They cannot be directly imported from the package. Destructuring them from the logger instance has been supported since v1.0.1.","wrong":"import { info } from 'glogg';","symbol":"debug / info / warn / error methods","correct":"const { info, error } = logger;"}],"quickstart":{"code":"const getLogger = require('glogg');\n\n// Create a logger for a specific namespace\nconst mainLogger = getLogger('my-app');\n\n// Create another logger for a different module\nconst dataModuleLogger = getLogger('data-processor');\n\n// Listen for 'info' events from the 'my-app' namespace\nmainLogger.on('info', (message) => {\n  console.log(`[MainApp Info Listener]: ${message}`);\n});\n\n// IMPORTANT: You *must* handle 'error' events, or the process will crash.\nmainLogger.on('error', (errMessage) => {\n  console.error(`[MainApp Error Handler]: CRITICAL! ${errMessage}`);\n  // In a real application, you might exit, log to a different system, etc.\n});\n\n// Log various types of messages\nmainLogger.debug('This is a verbose debug message at %s.', new Date().toLocaleTimeString());\nmainLogger.info('Application started successfully at %s.', new Date().toISOString());\nmainLogger.warn('A configuration setting is missing, falling back to default.');\nmainLogger.error('Failed to connect to database: Connection refused!');\n\n// Log without string formatting - all arguments are emitted directly\ndataModuleLogger.info({ event: 'data_processed', count: 123, status: 'success' });\ndataModuleLogger.debug(['debug array', { key: 'value' }]);\n\n// Demonstrates a message from a different logger\ndataModuleLogger.warn('Data processing took longer than expected.');\n\n// Output from a listener that applies custom filtering (conceptual)\n// For instance, only process warnings from 'data-processor' in a specific way\ndataModuleLogger.on('warn', (msg) => {\n    if (msg.includes('longer than expected')) {\n        console.log(`[Special Data Warn]: Specific issue noted: ${msg}`);\n    }\n});\n\n// Simulate another error (this would also crash if not handled by mainLogger's handler)\n// mainLogger.error('Another critical failure point!');","lang":"javascript","description":"Demonstrates how to create namespaced loggers, subscribe to different log levels (info, error), and emit various types of messages using `util.format()` style string interpolation and direct object logging. It explicitly highlights the critical error handling requirement to prevent process crashes."},"warnings":[{"fix":"Ensure your Node.js environment meets or exceeds version 10.13.0. Users on older Node.js versions must remain on glogg v1.x.","message":"The minimum Node.js version requirement was updated to `>= 10.13.0`.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Always attach at least one listener to the `error` event using `logger.on('error', handlerFunction)` to prevent process termination. This is standard Node.js `EventEmitter` behavior for 'error' events.","message":"The Node.js process will crash if an `error` event is emitted by a logger and there are no listeners attached to handle it.","severity":"gotcha","affected_versions":"*"},{"fix":"Implement conditional logic within your `logger.on()` handlers to process or discard messages based on their level or other criteria (e.g., `if (level === 'debug' && process.env.NODE_ENV !== 'development') return;`).","message":"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.","severity":"gotcha","affected_versions":"*"},{"fix":"Use CommonJS `require()` syntax: `const getLogger = require('glogg');`. If you are in an ESM module context, you may need to use a CJS wrapper or consider alternatives that support ESM directly.","message":"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.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change 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.","cause":"Attempting to use `import getLogger from 'glogg';` in an ESM module context or a bundler that strictly enforces ESM.","error":"ERR_REQUIRE_ESM: Must use import to load ES Module"},{"fix":"Add an error event listener to your logger instance: `logger.on('error', (msg) => { console.error('Caught glogg error:', msg); /* Handle gracefully */ });`","cause":"A `logger.error()` method was called, but no listener was registered for the 'error' event on that logger instance.","error":"Unhandled 'error' event (process exit)"}],"ecosystem":"npm"}