probe.gl Instrumentation and Logging
probe.gl is a robust collection of JavaScript tools designed for console-focused logging, performance instrumentation, benchmarking, and testing across both browser and Node.js environments. The current stable version is 3.6.0. It is developed and maintained as part of the vis.gl open-source visualization suite by Uber, indicating a steady release cadence tied to the evolution of their larger ecosystem. Key differentiators include its 'off by default' design to ensure a minimal performance footprint when not actively used, its lightweight nature, and features like persistent configuration through local storage. It enhances the debugging experience with capabilities such as defeating log cascades (to prevent console flooding) and providing direct source code links from console messages. The library offers a more advanced approach to application insights compared to basic `console.log`.
Common errors
-
TypeError: Probe.enable is not a function
cause `Probe` was not imported correctly as a default export or was not aliased to `window.Probe` in the browser environment.fixEnsure you are using `import Probe from 'probe.gl';`. If exposing to window for debugging, add `window.Probe = Probe;`. -
Module not found: Can't resolve 'probe.gl'
cause This typically indicates an issue with module resolution, often related to attempting to `require()` an ESM-only package in a CommonJS context or a misconfigured bundler.fixVerify your project's module resolution settings. For Node.js, ensure you're running in an ESM context (`'type': 'module'` in `package.json` or `.mjs` files). For bundlers, check `resolve` configurations. -
Logs are not appearing in the console, even after calling Probe.enable()
cause The logging level might be too low, or `isPrintEnabled` is false, preventing output to the console.fixEnsure `Probe.setLevel(level)` is called with a sufficiently high level (e.g., `Probe.setLevel(3)` for verbose logs). Also, confirm `Probe.configure({ isPrintEnabled: true })` to allow console output. Check browser local storage for persistent probe.gl configurations that might override current settings.
Warnings
- gotcha probe.gl is 'off by default' to ensure a minimal performance footprint. If you don't explicitly call `Probe.enable()`, no logging or instrumentation will occur.
- breaking As with many libraries transitioning in the JavaScript ecosystem, `probe.gl` (especially in major version 3.x) has likely solidified its move to ES Modules (ESM). Direct `require()` statements for CommonJS might lead to issues in modern setups, particularly in browser-first environments or Node.js projects configured for ESM.
- gotcha Configuration settings for `probe.gl` are persistent across browser sessions because they are stored in local storage. This can lead to unexpected behavior if you debug with certain settings and then forget to reset them.
- gotcha Specific utilities like `log` or `Timer` are often found in dedicated sub-packages (e.g., `@probe.gl/log`, `@probe.gl/stats`) rather than directly exported from the root `probe.gl` package. Importing from the root for these specific functions will result in `undefined` or module not found errors.
Install
-
npm install probe.gl -
yarn add probe.gl -
pnpm add probe.gl
Imports
- Probe
import { Probe } from 'probe.gl'; // Probe is a default export, not named.import Probe from 'probe.gl';
- log
import { log } from 'probe.gl'; // Specific logging utilities are in sub-modules.import { log } from '@probe.gl/log'; - Timer
import { Timer } from 'probe.gl'; // Performance classes are typically in sub-modules.import { Timer } from '@probe.gl/stats'; - window.Probe (browser)
import Probe from 'probe.gl'; window.Probe = Probe;
Quickstart
import Probe from 'probe.gl';
import { log } from '@probe.gl/log';
import { Timer } from '@probe.gl/stats';
// 1. Enable Probe and set logging level
// Probe is 'off by default' to minimize performance impact.
Probe.enable().setLevel(3); // Enable all features and verbose logging
Probe.configure({ isPrintEnabled: true }); // Ensure messages are printed to console
console.log('Probe.gl initialized. Check browser console for detailed logs.');
// 2. Use basic logging utilities
log.log('Application started successfully.');
log.warn('A non-critical issue detected.', { code: 101, details: 'Example warning payload' });
log.error('Failed to load critical resource!', new Error('Resource not found'));
// 3. Measure performance with a Timer
const myTimer = new Timer('ExpensiveOperation');
function performExpensiveOperation() {
myTimer.start();
// Simulate some work
let sum = 0;
for (let i = 0; i < 1000000; i++) {
sum += Math.sqrt(i);
}
myTimer.end(); // Automatically logs duration if Probe is enabled
log.log(`Result of expensive operation: ${sum}`);
}
performExpensiveOperation();
// 4. Using the main Probe object for options
if (Probe.getOption('myFeatureFlag')) {
log.log('Custom feature flag \'myFeatureFlag\' is enabled.');
} else {
log.log('Custom feature flag \'myFeatureFlag\' is disabled, configuring it...');
Probe.configure({ myFeatureFlag: true });
log.log('Custom feature flag \'myFeatureFlag\' is now enabled for subsequent checks.');
}