You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.2 KiB
3.2 KiB
Logging Strategy
Current State
The application currently uses console.log, console.error, and console.warn for logging, with:
- Context prefixes (e.g.,
[Fork] [repo1 → repo2]) - Security sanitization (truncated pubkeys, redacted private keys)
- Structured audit logging via
AuditLoggerservice
Recommendation: Use Pino for Production
Why Pino?
- Performance: Extremely fast (async logging, minimal overhead)
- Structured Logging: JSON output perfect for ELK/Kibana/Logstash
- Log Levels: Built-in severity levels (trace, debug, info, warn, error, fatal)
- Child Loggers: Context propagation (request IDs, user IDs, etc.)
- Ecosystem: Excellent Kubernetes/Docker support
- Small Bundle: ~4KB minified
Implementation Plan
1. Install Pino
npm install pino pino-pretty
2. Create Logger Service
// src/lib/services/logger.ts
import pino from 'pino';
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
...(process.env.NODE_ENV === 'development' && {
transport: {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'HH:MM:ss Z',
ignore: 'pid,hostname'
}
}
})
});
export default logger;
3. Replace Console Logs
// Before
console.log(`[Fork] ${context} Starting fork process`);
// After
import logger from '$lib/services/logger.js';
logger.info({ context, repo: `${npub}/${repo}` }, 'Starting fork process');
4. Structured Context
// Create child logger with context
const forkLogger = logger.child({
operation: 'fork',
originalRepo: `${npub}/${repo}`,
forkRepo: `${userNpub}/${forkRepoName}`
});
forkLogger.info('Starting fork process');
forkLogger.info({ relayCount: combinedRelays.length }, 'Using relays');
forkLogger.error({ error: sanitizedError }, 'Fork failed');
5. ELK/Kibana Integration
Pino outputs JSON by default, which works perfectly with:
- Filebeat: Collect logs from files
- Logstash: Parse and enrich logs
- Elasticsearch: Store and index logs
- Kibana: Visualize and search logs
Example log output:
{
"level": 30,
"time": 1703123456789,
"pid": 12345,
"hostname": "gitrepublic-1",
"operation": "fork",
"originalRepo": "npub1.../repo1",
"forkRepo": "npub2.../repo2",
"msg": "Starting fork process"
}
Migration Strategy
- Phase 1: Install Pino, create logger service
- Phase 2: Replace console logs in critical paths (fork, file operations, git operations)
- Phase 3: Replace remaining console logs
- Phase 4: Add request ID middleware for request tracing
- Phase 5: Configure log aggregation (Filebeat → ELK)
Benefits
- Searchability: Query logs by operation, user, repo, etc.
- Alerting: Set up alerts for error rates, failed operations
- Performance Monitoring: Track operation durations
- Security Auditing: Enhanced audit trail with structured data
- Debugging: Easier to trace requests across services
Alternative: Winston
Winston is also popular but:
- Slower than Pino
- More configuration overhead
- Better for complex transports (multiple outputs)
Recommendation: Use Pino for this project.