Logging in JavaScript Applications: Best Practices — Part 3
In this final part of the series on logging in JavaScript, we will look at advanced techniques and tools that will help you build a reliable monitoring system in Node.js applications. The material will be useful both for learning and for practical application — from JavaScript for beginners to experienced developers. We will discuss structured logging, log rotation, integration with external services, and security.
1. Structured Logging in Node.js
Regular console.log() is not suitable for a production environment. Instead, use libraries that generate JSON logs. This simplifies their parsing and analysis.
Example with the Winston library
const winston = require('winston');
const logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ]});
// Usagelogger.info('User registered', { userId: 123, email: 'user@example.com' });logger.error('Database error', { error: err.message, stack: err.stack });This approach allows you to easily filter logs by level, time, and context. This is especially important when programming microservices.
2. Log Rotation and Size Management
Logs can quickly fill up the disk. Use rotation — automatic archiving of old files. In Node.js, this is easily implemented using the winston-daily-rotate-file module.
Rotation configuration example
const DailyRotateFile = require('winston-daily-rotate-file');
const transport = new DailyRotateFile({ filename: 'logs/application-%DATE%.log', datePattern: 'YYYY-MM-DD', maxSize: '20m', maxFiles: '14d'});
const logger = winston.createLogger({ transports: [transport]});This ensures that logs will not exceed 20 MB per day and will be stored for 14 days.
3. Integration with External Monitoring Services
For centralized log collection, use services like Logstash, Datadog, or Sentry. In JavaScript, this is done through Winston transports.
Example of sending logs to Logstash
const { LogstashTransport } = require('winston-logstash-transport');
const logger = winston.createLogger({ transports: [ new LogstashTransport({ host: 'logstash.example.com', port: 5000 }) ]});This way, you can analyze logs in real time, build dashboards, and set up alerts.
4. Logging Security
Never log sensitive data: passwords, tokens, credit card numbers. Use filters or masking functions.
Data masking example
function maskSensitive(obj) { const masked = { ...obj }; if (masked.password) masked.password = '***'; if (masked.token) masked.token = '***'; return masked;}
logger.info('User request', maskSensitive(req.body));This will protect you from leaks and help you comply with GDPR.
Conclusion
In this part, we covered key logging practices in JavaScript applications on Node.js: structured logs, rotation, integration with external systems, and security. These techniques will help you create reliable and maintainable systems. For learning, we recommend starting with Winston and gradually introducing more complex tools. JavaScript for beginners may seem challenging, but practice will make you a professional. Good luck with programming!