Integration Guide
How to integrate safety patterns
Integration Guide: How to Use Safety Patterns
Complete walkthrough of integrating log compaction and context validation in your application.
Part 1: Log Compaction Integration
What We Added
Enhanced logger with built-in compaction to reduce noise from repetitive logs.
File: packages/hono-middleware/src/logger.ts
Features
✅ Compact State Updates
✅ Abbreviate Field Names
✅ Hide Sensitive Refs
✅ Filter Support
Usage
Global Configuration
Set globally in your app initialization:
Per-Call Configuration
Override for specific logs:
Custom Filters
Skip logs matching conditions:
Integration Example: Telegram Bot
File: apps/telegram-bot/src/index.ts (ready to integrate)
Configuration Options
| Option | Type | Default | Purpose |
|---|---|---|---|
compact | boolean | false | Enable log compaction |
abbreviate | boolean | false | Shorten field names |
hideRefs | boolean | false | Truncate IDs and trace IDs |
filter | function | undefined | Custom filter logic |
Part 2: Context Validation Integration
What We Added
Strong TypeScript typing + runtime validation for context passing.
File: packages/cloudflare-agent/src/context-validation.ts
Core Components
1. Strict Interfaces
2. Runtime Assertions
3. Type Guards
4. Builder Pattern
5. Middleware Validation
Integration: Telegram Bot
File: apps/telegram-bot/src/index.ts ✅ Already integrated
When to Use Each Approach
| Scenario | Use | Example |
|---|---|---|
| Type safety | Interfaces | All function parameters |
| Boundary validation | assertContextComplete() | After context creation |
| Admin operations | assertAdminContext() | Footer display, debug info |
| Conditional logic | isAdminContext() | Type narrowing in if/else |
| Complex objects | TelegramContextBuilder | When combining data from multiple sources |
| Global guard | validateContextMiddleware() | Wrap all handlers |
Part 3: Combined Usage
Real-World Example
Output
Part 4: Best Practices
✅ DO
Validate at boundaries
Use type guards for conditional logic
Enable compaction for noisy logs
Filter unnecessary logs
❌ DON'T
Skip validation
Use unsafe casts
Enable compaction everywhere
Ignore validation errors
Part 5: Migration Checklist
Phase 1: Enable Log Compaction (Optional)
- Review
packages/hono-middleware/src/logger.tschanges - Call
configureLogger()at app startup: - Monitor logs in Cloudflare dashboard
- Adjust filter rules if needed
Phase 2: Add Context Validation (Recommended)
- Review
packages/cloudflare-agent/src/context-validation.ts - Import validation functions:
- Add validation after context creation:
- Add type guards for conditional logic:
- Test with invalid contexts to verify errors
Phase 3: Adopt Middleware Validation (Advanced)
- Import middleware:
- Register globally:
- All handlers now have validated context
- Remove per-handler validation (middleware handles it)
Part 6: Testing
Test Log Compaction
Test Context Validation
Part 7: Troubleshooting
"Context missing required fields"
Cause: One or more required fields not set
Check:
Fix: Ensure all required fields are passed to createTelegramContext()
"isAdmin but missing debugContext"
Cause: Admin user without debug context
Fix: Ensure debug context is set before logging footer:
Logs not compacting
Cause: Compaction not enabled
Check:
Verify:
Summary
✅ Log Compaction: 10x smaller logs, especially for repetitive state updates ✅ Context Validation: Catch missing fields early with TypeScript + runtime checks ✅ Integration: Ready to use in Telegram bot, GitHub bot, or other platforms ✅ Best Practices: Follow patterns to prevent silent failures
See Also
packages/hono-middleware/src/logger.ts- Logger implementation with compactionpackages/cloudflare-agent/src/context-validation.ts- Context validation implementationapps/telegram-bot/src/index.ts- Real-world integration exampledocs/debug-footer-analysis.md- Debug footer detailsdocs/context-typing-guide.md- Detailed context typing guide