Monitoring and Logging Best Practices for Node.js Applications
Implementing effective monitoring and logging strategies in Node.js applications to track performance and diagnose issues.
0 likes
13 views
Rule Content
{ "title": "Monitoring and Logging Best Practices for Node.js Applications", "description": "Implementing effective monitoring and logging strategies in Node.js applications to track performance and diagnose issues.", "category": "Node.js Cursor Rules", "rules": [ { "id": "nodejs-logging-library", "description": "Use a robust logging library such as Winston, Bunyan, or Pino instead of console.log for enhanced functionality and flexibility.", "severity": "warning", "pattern": "console\\.log\\(.*\\);", "replacement": "logger.info($1);", "example": { "bad": "console.log('User logged in');", "good": "logger.info('User logged in');" } }, { "id": "nodejs-structured-logging", "description": "Implement structured logging using JSON format to facilitate easier parsing and analysis of log data.", "severity": "warning", "pattern": "logger\\.(info|warn|error)\\((?!\\{).*\\);", "replacement": "logger.$1({ message: $1 });", "example": { "bad": "logger.info('User logged in');", "good": "logger.info({ message: 'User logged in' });" } }, { "id": "nodejs-log-levels", "description": "Utilize appropriate log levels (e.g., info, warn, error) to categorize log messages based on their severity.", "severity": "warning", "pattern": "logger\\.log\\(.*\\);", "replacement": "logger.info($1);", "example": { "bad": "logger.log('User logged in');", "good": "logger.info('User logged in');" } }, { "id": "nodejs-log-timestamps", "description": "Include timestamps in log entries to provide context about when events occurred.", "severity": "warning", "pattern": "logger\\.(info|warn|error)\\(\\{.*\\}\\);", "replacement": "logger.$1({ timestamp: new Date().toISOString(), ...$1 });", "example": { "bad": "logger.info({ message: 'User logged in' });", "good": "logger.info({ timestamp: new Date().toISOString(), message: 'User logged in' });" } }, { "id": "nodejs-log-sensitive-data", "description": "Avoid logging sensitive information such as passwords, credit card numbers, or personal data.", "severity": "error", "pattern": "logger\\.(info|warn|error)\\(.*(password|creditCard|ssn).*\\);", "replacement": "", "example": { "bad": "logger.info('User password: ' + user.password);", "good": "logger.info('User logged in', { userId: user.id });" } }, { "id": "nodejs-log-rotation", "description": "Implement log rotation to prevent log files from consuming excessive disk space.", "severity": "warning", "pattern": "new winston\\.transports\\.File\\(\\{ filename: '.*\\.log' \\}\\)", "replacement": "new winston.transports.File({ filename: 'application.log', maxsize: 10485760, maxFiles: 5 })", "example": { "bad": "new winston.transports.File({ filename: 'application.log' })", "good": "new winston.transports.File({ filename: 'application.log', maxsize: 10485760, maxFiles: 5 })" } }, { "id": "nodejs-centralized-logging", "description": "Configure centralized logging to aggregate logs from multiple sources for unified analysis.", "severity": "info", "pattern": "", "replacement": "", "example": { "bad": "", "good": "const { createLogger, transports } = require('winston');\nconst { LogstashTransport } = require('winston-logstash-transport');\n\nconst logger = createLogger({\n transports: [\n new transports.Console(),\n new LogstashTransport({\n host: 'logstash.example.com',\n port: 5000\n })\n ]\n});" } }, { "id": "nodejs-monitoring-tools", "description": "Integrate monitoring tools like Prometheus and Grafana to track application performance metrics.", "severity": "info", "pattern": "", "replacement": "", "example": { "bad": "", "good": "const client = require('prom-client');\nconst express = require('express');\nconst app = express();\n\nconst collectDefaultMetrics = client.collectDefaultMetrics;\ncollectDefaultMetrics({ timeout: 5000 });\n\napp.get('/metrics', (req, res) => {\n res.set('Content-Type', client.register.contentType);\n res.end(client.register.metrics());\n});" } } ] }