Frontend Structured Logging Implementation Guide¶
This comprehensive guide explains how to implement structured logging for production in the PRS Frontend application, providing better error tracking, user action monitoring, and performance insights.
Overview¶
Structured logging in the frontend provides:
- Centralized Logging: Consistent logging interface across the application
- Production Monitoring: Real-time error tracking and performance monitoring
- User Analytics: Track user interactions and workflows
- Debug Information: Detailed context for troubleshooting
- Performance Insights: Monitor API response times and component performance
Current State vs. Target State¶
Current State¶
- ✅ Basic error handling with
react-error-boundary - ✅ API error formatting in
apiClient.js - ✅ Console.error logging for API errors
- ✅ Toast notifications for user feedback
- ❌ No structured logging system
- ❌ No production log aggregation
- ❌ No centralized error tracking
Target State¶
- ✅ Structured logging service with consistent format
- ✅ Production log transport to external services
- ✅ Enhanced error tracking with context
- ✅ User action and performance monitoring
- ✅ Environment-based logging configuration
Implementation Plan¶
Phase 1: Core Logging Infrastructure¶
1.1 Logger Service (src/lib/logger/index.js)¶
| JavaScript | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | |
1.2 Constants (src/lib/logger/constants.js)¶
1.3 Formatters (src/lib/logger/formatters.js)¶
Phase 2: Transport Layer¶
2.1 Transports (src/lib/logger/transports.js)¶
| JavaScript | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
Phase 3: Enhanced Error Tracking¶
3.1 Enhanced API Client (src/lib/apiClient.js - Updates)¶
| JavaScript | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
Usage Examples¶
Basic Logging¶
Component Integration¶
Environment Configuration¶
Update src/config/env.js:
Production Deployment¶
Environment Variables¶
Docker Compose Integration¶
Add frontend logging to your PLG stack:
PLG Stack Integration (Prometheus, Loki, Grafana)¶
The PRS system uses the PLG stack for comprehensive observability:
- Prometheus - Metrics collection and alerting
- Loki - Log aggregation and querying
- Grafana - Visualization and dashboards
Loki Integration¶
Frontend logs are sent to Loki via HTTP API with proper labels for efficient querying:
Prometheus Metrics Integration¶
Export frontend metrics to Prometheus for monitoring:
| JavaScript | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
Grafana Dashboard Configuration¶
Create comprehensive dashboards for frontend monitoring:
LogQL Queries for Frontend Monitoring¶
Essential LogQL queries for monitoring frontend applications:
Prometheus Alerting Rules¶
Configure alerts for critical frontend issues:
Best Practices¶
- Log Levels: Use appropriate log levels (debug for development, info/warn/error for production)
- Sensitive Data: Always sanitize sensitive information before logging
- Performance: Use batching and sampling to avoid performance impact
- Context: Include relevant context (user ID, session ID, correlation IDs)
- Structured Data: Use consistent data structures for easier analysis
- Error Boundaries: Implement error boundaries with logging
- User Privacy: Respect user privacy and comply with data protection regulations
Testing¶
Monitoring and Alerts¶
Set up alerts for:
- High error rates
- Performance degradation
- Authentication failures
- API timeout increases
- Critical business workflow failures
This structured logging system provides comprehensive visibility into your frontend application's behavior, enabling better debugging, monitoring, and user experience optimization.
Advanced Features¶
React Hooks Integration¶
Create a custom hook for easy logging integration:
Higher-Order Component for Automatic Logging¶
Performance Monitoring¶
Global Error Handler¶
Security Considerations¶
Data Privacy and GDPR Compliance¶
Rate Limiting and Sampling¶
Troubleshooting¶
Common Issues¶
- Logs not appearing in production
- Check environment variables
- Verify transport configuration
-
Check network connectivity to log endpoint
-
Performance impact
- Implement log sampling
- Use batching for HTTP transport
-
Reduce log verbosity in production
-
Storage quota exceeded
- Implement log rotation
- Reduce localStorage retention
- Use compression for stored logs
Debug Mode¶
| JavaScript | |
|---|---|
Migration Guide¶
From Console Logging¶
| JavaScript | |
|---|---|
From Basic Error Handling¶
| JavaScript | |
|---|---|
Integration with PRS Backend Logging¶
The frontend logging system is designed to complement the existing PRS backend structured logging:
Correlation IDs¶
Ensure frontend and backend logs can be correlated:
Unified Log Format¶
Align frontend logs with backend log structure:
Cross-Service Monitoring¶
Monitor end-to-end request flows:
Shared Monitoring Dashboards¶
Create unified dashboards showing both frontend and backend metrics:
This comprehensive guide provides everything needed to implement production-ready structured logging in your frontend application that seamlessly integrates with the existing PRS backend logging infrastructure and PLG stack.