Lighthouse Logger
The `lighthouse-logger` package provides a shared logging utility class primarily used internally by the Google Lighthouse project and its associated tools. It offers basic logging capabilities with a focus on integrating within the Lighthouse ecosystem. The current stable version is 2.0.2, last published 8 months ago, and it ships with TypeScript types for improved developer experience. While the broader Lighthouse project updates frequently with Chrome DevTools releases, `lighthouse-logger` maintains a more independent and slower release cycle. Its key differentiators include its tight integration with Lighthouse's internal architecture and its minimal set of dependencies, notably `debug` and `marky`. However, discussions within the Lighthouse team suggest that maintaining `lighthouse-logger` as an external npm package is considered "awkward," and there's consideration for deprecating its external use, potentially leading to a maintenance-only status or advising direct internal referencing for Lighthouse itself.
Common errors
-
ERR_REQUIRE_ESM: require() of ES Module .../node_modules/lighthouse-logger/index.js from .../my-app.js not supported
cause Attempting to import `lighthouse-logger` (an ES Module) using CommonJS `require()` in a context where only ESM is expected or configured.fixChange your import statement to use ES Modules: `import { Logger } from 'lighthouse-logger';`. If in Node.js, ensure your `package.json` has `"type": "module"` or use `.mjs` file extensions for your module. -
TypeError: Logger is not a constructor
cause The `Logger` class might not be correctly imported or accessed, often due to incorrect import syntax for named exports.fixEnsure you are using named imports for `Logger`: `import { Logger } from 'lighthouse-logger';`. Avoid `import Logger from 'lighthouse-logger';` as it is not a default export.
Warnings
- gotcha The `lighthouse-logger` package is internally managed within the Lighthouse monorepo and has been described as an 'awkward package to maintain' for external consumption. While currently available on npm, future external support or major updates might be limited, or the Lighthouse project might advise against its direct external use. Consider its 'maintenance' status when integrating into new projects.
- breaking Version 2.x of `lighthouse-logger` is primarily distributed as an ES Module (ESM). Attempting to import it using CommonJS `require()` syntax in a Node.js environment configured for ESM or in a browser context might lead to errors.
Install
-
npm install lighthouse-logger -
yarn add lighthouse-logger -
pnpm add lighthouse-logger
Imports
- Logger
const Logger = require('lighthouse-logger');import { Logger } from 'lighthouse-logger'; - setLevel
const { setLevel } = require('lighthouse-logger');import { setLevel } from 'lighthouse-logger'; - *
import LighthouseLogger from 'lighthouse-logger';
import * as LighthouseLogger from 'lighthouse-logger';
Quickstart
import { Logger, setLevel } from 'lighthouse-logger';
// Set the desired logging level (e.g., 'verbose', 'log', 'info', 'warn', 'error', 'silent')
// By default, it might log based on debug environment variables or a default level.
setLevel('verbose');
const myLogger = new Logger('my-app');
myLogger.log('Starting application...');
myLogger.info('Configuration loaded successfully.');
myLogger.warn('Potential issue: API endpoint might be deprecated.');
myLogger.error('Critical error: Failed to connect to database!');
// Example of more detailed logging with specific log IDs
myLogger.log('Processing data for user', { userId: 123, dataSize: '1.5MB' });
// You can also create sub-loggers for different modules
const dataProcessorLogger = new Logger('my-app:data-processor');
dataProcessorLogger.verbose('Entering data processing module.');
// Example of setting level via environment variable for Node.js
// process.env.DEBUG = 'my-app*' // This would enable debug logs for 'my-app' namespace
console.log('\nCheck your console output for logs. Note: ' +
'Actual output depends on environment variables (e.g., DEBUG) and setLevel call.');