Overview
The backend uses Pino for structured logging, Sentry for error tracking, and implements comprehensive monitoring across all layers of the application.Structured Logging with Pino
Logger Configuration
The logger is configured insrc/core/logger.ts:
logger.ts
- Development: Pretty-printed, colorized output
- Production: JSON-formatted logs for aggregation
Log Structure
All logs follow a standardized structure:Example Logs
Log Levels
Configure log level viaLOG_LEVEL environment variable:
.env
debug- Development diagnostics (verbose)info- General operational eventswarn- Warning conditions (non-critical)error- Error conditions requiring attention
Usage by Level
Request Logging Middleware
All HTTP requests are logged via Morgan middleware:server.ts
Error Tracking with Sentry
Sentry Configuration
Sentry is configured for production error tracking:Environment Variables
.env
Automatic Error Capture
Sentry automatically captures:- Unhandled exceptions
- Unhandled promise rejections
- HTTP errors (4xx, 5xx)
Manual Error Reporting
Job Monitoring
All automated jobs emit structured logs for monitoring:Sorteos Automation Job
sorteosAuto.job.ts
Account Statement Settlement Job
accountStatementSettlement.job.ts
Monthly Closing Job
monthlyClosing.job.ts
Database Monitoring
Connection Warmup
The backend implements connection warmup before job execution:Connection Error Logging
DB_UNREACHABLE- P1001: Cannot connect to databasePOOLER_TIMEOUT- P2028: Transaction timeoutPOOLER_WAIT_TIMEOUT- Query wait timeout exceeded
Activity Log System
The backend persists critical activities to theActivityLog table:
Tracked Actions
Sorteos:SORTEO_CREATESORTEO_UPDATESORTEO_OPENSORTEO_CLOSESORTEO_EVALUATE
TICKET_CREATETICKET_CANCELTICKET_PAYTICKET_REVERSE_PAYMENT
LOTERIA_CREATELOTERIA_UPDATELOTERIA_DELETE
USER_CREATEUSER_UPDATEUSER_DELETEUSER_LOGIN
Activity Log Cleanup
Automated cleanup runs daily at 2:00 AM UTC:activityLogCleanup.job.ts
Performance Monitoring
Query Execution Tracking
Dashboard and analytics endpoints include query performance metadata:Transaction Retry Logging
Alerting Setup
Log Aggregation
For production, integrate with log aggregation services: Recommended services:- Datadog: Full APM and log aggregation
- New Relic: Application monitoring
- Logtail: Serverless-friendly logging
- CloudWatch Logs: AWS-native solution
index.ts
Alert Rules
Recommended alerts:-
Error Rate Spike
- Trigger: Error logs > 10 per minute
- Severity: High
-
Job Failures
- Trigger:
SORTEOS_AUTO_*_FAILorSETTLEMENT_JOB_ERROR - Severity: Critical
- Trigger:
-
Database Connection Issues
- Trigger:
DB_UNREACHABLEorPOOLER_TIMEOUT - Severity: Critical
- Trigger:
-
High Response Times
- Trigger: P95 latency > 2 seconds
- Severity: Medium
-
Low Disk Space
- Trigger: Database disk usage > 85%
- Severity: High
Log Analysis Examples
Find Failed Tickets
Track Job Execution
Monitor Settlement Job
Graceful Shutdown Logging
The server logs all shutdown phases:Best Practices
Avoid Logging Sensitive Data
Never log:
- Passwords or tokens
- Full credit card numbers
- Social security numbers
- Personal identification data (beyond user ID)
Next Steps
Deployment
Learn about production deployment
Automated Jobs
Configure and monitor automated jobs