A lightweight, vanilla JavaScript web component for capturing and displaying console logs with syntax highlighting and type-aware formatting.
npm install @profpowell/browser-console// ES Module import
import '@profpowell/browser-console';
// Or import the class directly
import { BrowserConsole } from '@profpowell/browser-console';<!-- From unpkg -->
<script type="module" src="https://unpkg.com/@profpowell/browser-console"></script>
<!-- Or from jsDelivr -->
<script type="module" src="https://cdn.jsdelivr.net/npm/@profpowell/browser-console"></script>Download browser-console.min.js from the releases page and include it in your project:
<script type="module" src="browser-console.min.js"></script>- Zero Dependencies - Pure vanilla JavaScript, no frameworks required
- Syntax Highlighting - Beautiful color-coded output for different data types
- Type-Aware Formatting - Smart rendering of strings, numbers, BigInt, objects, arrays, Map, Set, Symbol, and more
- Console Method Support - Captures log, info, warn, error, debug, table, time/timeEnd/timeLog, group/groupEnd, trace, count, dir
- Table Rendering - Proper HTML table rendering for console.table() with full support for arrays and objects
- Timer Tracking - Automatically tracks and displays console.time/timeEnd/timeLog durations
- Light & Dark Themes - Switch between light and dark themes with a single click
- Better Object Expansion - Click to fully expand/collapse complex objects and arrays with structured view
- Filter by Level - Filter logs by method type (log, info, warn, error)
- Text Search - Filter logs by searching text content
- Copy to Clipboard - Export all logs or filtered logs as plain text
- Console Groups - Collapsible console.group/groupCollapsed with visual indentation
- Stack Traces - console.trace displays full stack trace with expandable view
- Performance - Efficient rendering with configurable log limits
- Accessible - ARIA landmarks, keyboard navigation, screen reader support
- Easy Integration - Just add the custom element to your HTML
- CSS Customizable - Full theming via CSS custom properties
<!DOCTYPE html>
<html>
<head>
<title>Browser Console Demo</title>
</head>
<body>
<browser-console></browser-console>
<script src="browser-console.js"></script>
<script>
console.log('Hello, World!');
console.warn('This is a warning');
console.error('This is an error');
</script>
</body>
</html>browser-console {
display: block;
width: 100%;
height: 400px;
}That's it! The component will automatically hook into console methods and display all logs.
<browser-console></browser-console>The component automatically hooks into all console methods when connected to the DOM.
<!-- Dark theme (default) -->
<browser-console theme="dark"></browser-console>
<!-- Light theme -->
<browser-console theme="light"></browser-console><browser-console auto-hook="false"></browser-console>const feed = document.querySelector('browser-console');
// Hook console methods
feed.hookConsole();
// Unhook console methods
feed.unhookConsole();
// Clear all logs
feed.clearLogs();
// Change theme programmatically
feed.setTheme('light'); // or 'dark'
// Set filter (null, 'log', 'info', 'warn', 'error')
feed.setFilter('error');
// Manually add a log
feed.addLog({
method: 'log',
data: ['Custom message', { foo: 'bar' }],
timestamp: new Date()
});The component intercepts and displays the following console methods:
console.log()- Standard loggingconsole.info()- Informational messagesconsole.warn()- Warning messagesconsole.error()- Error messagesconsole.debug()- Debug messagesconsole.table()- Renders data as proper HTML tables (arrays, objects, array of objects)console.time()/console.timeEnd()- Tracks and displays execution time with millisecond precisionconsole.timeLog()- Logs elapsed time without stopping the timerconsole.group()/console.groupEnd()- Collapsible log groups with visual indentationconsole.groupCollapsed()- Creates a collapsed group (click to expand)console.trace()- Displays stack trace with expandable viewconsole.count()/console.countReset()- Labeled counters with running totalsconsole.dir()- Object inspection with expanded viewconsole.clear()- Clears the consoleconsole.assert()- Assertions (captured but basic display)
The component provides intelligent formatting for:
- Primitives: strings, numbers, booleans, null, undefined
- BigInt: Large integer values with
nsuffix - Symbol: Symbol descriptions with proper formatting
- Objects: Plain objects with expandable properties
- Arrays: Arrays with expandable elements
- Map: Map instances with key-value pair display
- Set: Set instances with value display
- Functions: Function definitions with preview
- Dates: ISO formatted dates
- RegExp: Regular expression patterns
- Errors: Error objects with stack traces
- DOM Elements: HTML elements with tag, id, and class display
Different data types are color-coded for easy identification:
- Strings: orange
- Numbers: light green
- Booleans: blue
- null/undefined: gray italic
- Functions: yellow
- Objects/Arrays: white
- Errors: red
- Dates: cyan
- RegExp: dark red
- DOM Elements: teal
Different console methods have distinct visual styles:
log: Blue accentinfo: Light blue accentwarn: Yellow accent with warning backgrounderror: Red accent with error backgrounddebug: Purple accent
The component now renders console.table() calls as proper HTML tables:
- Array of Objects: Displays with column headers for each property
- Simple Arrays: Shows index and value columns
- Plain Objects: Renders key-value pairs in table format
- Styled tables with hover effects and proper borders
- Supports both light and dark themes
console.table([
{ id: 1, name: 'Alice', role: 'Admin' },
{ id: 2, name: 'Bob', role: 'User' }
]);Automatically tracks performance timings with console.time() and console.timeEnd():
- Tracks multiple named timers simultaneously
- Displays duration in milliseconds with 2 decimal precision
- Uses
performance.now()for high-precision timing - Formatted with special timer icon and color
console.time('my-operation');
// ... some code ...
console.timeEnd('my-operation'); // Shows: my-operation: 123.45msSwitch between light and dark themes:
- Dark Theme: Developer-friendly with VS Code-inspired colors (default)
- Light Theme: Clean, bright theme for daytime use
- Click the ☀️ button in the console header to toggle
- Set programmatically with
setTheme('light')orsetTheme('dark') - Set via attribute:
<browser-console theme="light"></browser-console> - Smooth transitions between themes
Enhanced expandable view for objects and arrays:
- Click the ▶ icon to expand and view all properties
- Shows type label (e.g., "Array(5)", "Object")
- Properly formatted property list when expanded
- Nested expandable items for deep object inspection
- Unique IDs for each expandable section
Click the filter buttons in the header to show only specific log levels:
- All
- Log
- Info
- Warn
- Error
- Configurable maximum log entries (default: 1000)
- Efficient re-rendering on log additions
- Automatic cleanup of old logs
- Limited depth for nested objects to prevent performance issues
| Attribute | Type | Default | Description |
|---|---|---|---|
auto-hook |
boolean | true | Automatically hook console methods on mount |
theme |
'dark' | 'light' | 'dark' | Color theme for the console display |
| Property | Type | Default | Description |
|---|---|---|---|
logs |
Array | [] | Array of captured log entries |
filter |
String | null | null | Current filter ('log', 'info', 'warn', 'error', or null for all) |
maxLogs |
Number | 1000 | Maximum number of logs to keep |
theme |
String | 'dark' | Current theme ('light' or 'dark') |
timers |
Object | {} | Internal timer tracking for console.time/timeEnd |
counters |
Object | {} | Counter tracking for console.count |
searchQuery |
String | '' | Current search filter text |
groupDepth |
Number | 0 | Current nesting depth for console.group |
groupStack |
Array | [] | Stack of open groups |
| Method | Parameters | Description |
|---|---|---|
hookConsole() |
- | Hook into console methods to capture logs |
unhookConsole() |
- | Restore original console methods |
addLog(log) |
log: {method, data, timestamp} | Manually add a log entry |
clearLogs() |
- | Clear all captured logs |
setFilter(filter) |
filter: String | null | Set the current filter |
setTheme(theme) |
theme: 'light' | 'dark' | Change the color theme |
setSearchQuery(query) |
query: String | Filter logs by search text |
copyLogs() |
- | Copy all visible logs to clipboard (async) |
getExpansionStates() |
- | Get Map of current expand/collapse states |
restoreExpansionStates(states) |
states: Map | Restore expand/collapse states |
The component doesn't emit custom events currently, but you can extend it to do so.
console.log('String:', 'Hello World');
console.log('Number:', 42, 3.14);
console.log('Boolean:', true, false);
console.log('Null:', null);
console.log('Undefined:', undefined);
console.log('Array:', [1, 2, 3, 4, 5]);
console.log('Object:', { name: 'John', age: 30 });const user = {
name: 'Jane Smith',
profile: {
age: 28,
location: {
city: 'San Francisco',
state: 'CA'
}
},
hobbies: ['coding', 'reading']
};
console.log(user);try {
throw new Error('Something went wrong!');
} catch (error) {
console.error('Caught error:', error);
}// Start a timer
console.time('data-processing');
// Simulate some work
const data = Array.from({ length: 1000 }, (_, i) => i * 2);
const sum = data.reduce((a, b) => a + b, 0);
// End timer - displays duration
console.timeEnd('data-processing'); // Output: data-processing: 2.35ms
// Multiple simultaneous timers
console.time('timer-1');
console.time('timer-2');
setTimeout(() => console.timeEnd('timer-1'), 100);
setTimeout(() => console.timeEnd('timer-2'), 200);const users = [
{ id: 1, name: 'Alice', role: 'Admin' },
{ id: 2, name: 'Bob', role: 'User' },
{ id: 3, name: 'Charlie', role: 'Moderator' }
];
console.table(users);This component uses:
- Custom Elements (Web Components)
- Shadow DOM
- ES6+ JavaScript features
Supported browsers:
- Chrome 54+
- Firefox 63+
- Safari 10.1+
- Edge 79+
browser-console {
display: block;
height: 400px;
border: 2px solid #333;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}The component supports extensive theming via CSS custom properties. Override these in your stylesheet to customize colors:
| Property | Description |
|---|---|
--bg-primary |
Main background color |
--bg-secondary |
Header/toolbar background |
--bg-tertiary |
Search input, code blocks |
--bg-hover |
Hover state background |
--bg-warn |
Warning log background |
--bg-error |
Error log background |
| Property | Description |
|---|---|
--text-primary |
Main text color |
--text-secondary |
Timestamps, labels |
--border-color |
Borders and dividers |
| Property | Description |
|---|---|
--color-log |
Log level accent |
--color-info |
Info level accent |
--color-warn |
Warning level accent |
--color-error |
Error level accent |
--color-debug |
Debug level accent |
--color-table |
Table method accent |
--color-time |
Timer method accent |
| Property | Description |
|---|---|
--value-string |
String values |
--value-number |
Number/BigInt values |
--value-boolean |
Boolean values |
--value-null |
null/undefined values |
--value-function |
Function values |
--value-date |
Date values |
--value-regexp |
RegExp values |
--value-element |
DOM element values |
| Property | Description |
|---|---|
--btn-bg |
Button background |
--btn-border |
Button border |
--btn-hover |
Button hover state |
--btn-active |
Active/pressed button |
--table-border |
Table borders |
--table-header-bg |
Table header background |
--table-row-hover |
Table row hover |
--scrollbar-track |
Scrollbar track |
--scrollbar-thumb |
Scrollbar thumb |
--scrollbar-thumb-hover |
Scrollbar thumb hover |
browser-console {
--bg-primary: #0d1117;
--bg-secondary: #161b22;
--text-primary: #c9d1d9;
--color-log: #58a6ff;
--color-error: #f85149;
--value-string: #a5d6ff;
--value-number: #79c0ff;
}Open index.html in your browser to see the interactive demo with various test cases and examples.