Server Components
Logging in React Server Components with automatic context propagation.
Overview
In Server Components, use getLogger() to get a logger that:
- Inherits correlation context — From middleware
- Uses React cache — Deduplicated across the request
- Creates child loggers — With your namespace
Basic Usage
typescript
With Request Context
Access correlation IDs for tracing:
typescript
Nested Components
Context propagates through nested async components:
typescript
All logs share the same requestId and traceId:
json
Data Fetching
Log data fetching operations:
typescript
Error Handling
Log errors with full context:
typescript
With Suspense
Logging works correctly with Suspense boundaries:
typescript
Parallel Data Fetching
Log parallel operations:
typescript
Layout Logging
Log in layouts for request-level info:
typescript
Best Practices
1. Use Descriptive Namespaces
typescript
2. Log Business Events
typescript
3. Include Relevant Context
typescript