f6e5a454ae
Mirror to GitHub / mirror (push) Successful in 31s
Test and Lint / backend-test (push) Successful in 1m27s
continuous-integration/drone/push Build is passing
Test and Lint / frontend-test (push) Successful in 2m22s
Version and Release / version-bump (push) Successful in 38s
Version and Release / trigger-drone (push) Successful in 3s
- Add comprehensive logging for rate limit blocks with full request details - IP address (with proper proxy detection), user agent, headers, timestamps - Rate limit info (current count, limit, remaining, reset time) - Separate tracking for auth vs general endpoints - Enhance authentication failure logging - JWT validation failures with detailed error info - Admin auth attempts without token - Failed token validation with user context - All events include IP, path, method, user agent - Improve Winston logger configuration for production - Add automatic log rotation (10MB errors, 50MB combined) - Create separate security.log for auth/rate limit events - Ensure logs directory exists automatically - Add structured JSON format for log aggregation - Support container logging with LOG_TO_CONSOLE env var - Create comprehensive documentation - Security logging guide with examples - Monitoring recommendations - Configuration reference - Add test script to verify logging functionality All rate limit settings remain configurable via admin panel: - Window duration, max requests, auth limits - Skip authenticated requests option - Public endpoints only option 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
99 lines
3.1 KiB
JavaScript
99 lines
3.1 KiB
JavaScript
const winston = require('winston');
|
|
const path = require('path');
|
|
const fs = require('fs');
|
|
|
|
// Ensure logs directory exists
|
|
const logDir = path.join(__dirname, '../../logs');
|
|
if (!fs.existsSync(logDir)) {
|
|
fs.mkdirSync(logDir, { recursive: true });
|
|
}
|
|
|
|
// Custom format for production logs
|
|
const productionFormat = winston.format.combine(
|
|
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
|
|
winston.format.errors({ stack: true }),
|
|
winston.format.json(),
|
|
winston.format.printf(info => {
|
|
// Ensure all security events are properly formatted
|
|
if (info.level === 'warn' && (info.message.includes('rate limit') ||
|
|
info.message.includes('auth') ||
|
|
info.message.includes('login') ||
|
|
info.message.includes('JWT'))) {
|
|
return JSON.stringify({
|
|
timestamp: info.timestamp,
|
|
level: info.level,
|
|
message: info.message,
|
|
security: true,
|
|
...info
|
|
});
|
|
}
|
|
return JSON.stringify(info);
|
|
})
|
|
);
|
|
|
|
const logger = winston.createLogger({
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
format: productionFormat,
|
|
transports: [
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'error.log'),
|
|
level: 'error',
|
|
maxsize: 10 * 1024 * 1024, // 10MB
|
|
maxFiles: 5,
|
|
tailable: true
|
|
}),
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'combined.log'),
|
|
maxsize: 50 * 1024 * 1024, // 50MB
|
|
maxFiles: 10,
|
|
tailable: true
|
|
}),
|
|
// Separate security log for authentication and rate limiting
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'security.log'),
|
|
level: 'warn',
|
|
maxsize: 20 * 1024 * 1024, // 20MB
|
|
maxFiles: 10,
|
|
tailable: true,
|
|
format: winston.format.combine(
|
|
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss.SSS' }),
|
|
winston.format.json(),
|
|
winston.format.printf(info => {
|
|
// Only log security-related warnings
|
|
if (info.message.includes('rate limit') ||
|
|
info.message.includes('auth') ||
|
|
info.message.includes('login') ||
|
|
info.message.includes('JWT') ||
|
|
info.message.includes('lockout') ||
|
|
info.message.includes('suspicious')) {
|
|
return JSON.stringify(info);
|
|
}
|
|
return null;
|
|
})
|
|
)
|
|
})
|
|
].filter(Boolean)
|
|
});
|
|
|
|
// Add console logging for non-production environments
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
logger.add(new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.timestamp({ format: 'HH:mm:ss' }),
|
|
winston.format.printf(info => {
|
|
return `[${info.timestamp}] ${info.level}: ${info.message} ${info.stack || ''}`;
|
|
})
|
|
)
|
|
}));
|
|
} else {
|
|
// In production, also log to console for container environments
|
|
if (process.env.LOG_TO_CONSOLE === 'true') {
|
|
logger.add(new winston.transports.Console({
|
|
format: productionFormat
|
|
}));
|
|
}
|
|
}
|
|
|
|
module.exports = logger;
|