Basic Logging
console.log()
Log messages to stdout:console.error()
Log errors to stderr:console.warn()
Log warnings to stderr:console.info()
Log informational messages:console.debug()
Log debug messages:Formatting
Format Specifiers
Multiple Arguments
Object Inspection
Default Inspection
Deep Inspection
Control inspection depth:Arrays
Functions
Classes
Grouping
console.group()
Create collapsible groups:console.groupCollapsed()
Create collapsed groups:Tables
console.table()
Display data as a table:Select Columns
Objects as Tables
Timing
console.time()
Measure execution time:console.timeLog()
Log intermediate times:Multiple Timers
Counting
console.count()
Count function calls:console.countReset()
Reset counter:Labeled Counters
Assertions
console.assert()
Log error if assertion fails:With Objects
Stack Traces
console.trace()
Print stack trace:Clearing
console.clear()
Clear console (terminal only):Colors
Bun automatically colors output in terminals:Custom Inspection
inspect Symbol
toJSON()
Best Practices
-
Use appropriate log levels
-
Group related logs
-
Use timers for performance
-
Table for structured data
-
Assertions for debugging
Production Logging
For production apps, consider using a logging library:Platform Differences
Bun vs Node.js
- Colors: Bun has better default colors
- Performance: Bun’s console is faster
- Depth: Bun defaults to depth 2, Node to unlimited
Bun vs Browsers
- Streams: Bun writes to stdout/stderr, browsers to DevTools
- Styling: CSS styling (%c) only works in browsers
- Persistence: Browser console persists between reloads