Overview
The logging system provides:- Structured logging - Consistent log format with metadata
- Sentry integration - Automatic error reporting to Sentry
- Log levels - Debug, Info, Warn, Error, Fatal
- Debug contexts - Filter logs by component/feature
- Console transport - Development logging
- In-memory dumps - Export logs for debugging
- Service loggers - Scoped loggers for integrations
Logger Class
src/logger/index.ts
Basic Usage
Log Levels
RainbowError
Always useRainbowError for error logging:
With Cause Chain
Metadata
Attach structured data to logs:Example
Debug Contexts
Filter debug logs by component:Usage
Define Contexts
src/logger/debugContext.ts
Transports
Logs are sent to transports based on environment:Console Transport (Development)
src/logger/index.ts
Sentry Transport (Production)
src/logger/index.ts
Custom Transport
Service Loggers
Create scoped loggers for service integrations:Service Logger Interface
Log Dump
Export in-memory logs for debugging:src/logger/logDump.ts
Enable Log Dump
Export Logs
Real-World Examples
API Requests
Store Operations
Transaction Errors
Best Practices
Always use RainbowError for errors
Always use RainbowError for errors
Wrap errors in RainbowError to preserve stack traces:
Add meaningful metadata
Add meaningful metadata
Include context to help debugging:
Use appropriate log levels
Use appropriate log levels
Choose the right level for each log:
- Debug: Verbose development info
- Info: General information
- Log: Important events
- Warn: Warnings that need attention
- Error: Errors that should be fixed
Use debug contexts
Use debug contexts
Organize debug logs by feature:
Create service loggers
Create service loggers
Use scoped loggers for integrations:
Testing
Mock Logger
Test Logging
Configuration
Environment Variables
Runtime Configuration
Next Steps
Architecture Overview
Back to architecture overview
State Management
Learn about state management