Logging with Winston in Node.js
Logging is a critical part of backend development. It helps developers track application behavior, debug issues, monitor performance, and understand what is happening in production systems.
While simple console.log() statements may work during development, they are not suitable for production environments.
This is where Winston comes in. Winston is a powerful logging library for Node.js that allows structured, configurable, and scalable logging.
What is Winston?
Winston is a popular logging library for Node.js that supports multiple log levels, customizable formats, and different output destinations (called transports).
It allows you to store logs in files, console, or even external systems like log management tools.
Why Use Winston Instead of console.log?
- Structured logging: Logs are organized and easy to analyze
- Log levels: Different severity levels like info, error, warn
- Multiple outputs: Logs can be saved to files, console, or remote services
- Better debugging: Helps track issues in production
- Scalable: Suitable for large applications
Installing Winston
Install Winston using npm:
Basic Winston Logger Setup
Create a simple logger:
In this example:
leveldefines the minimum log levelformatdefines how logs are structuredtransportsdefine where logs are stored
Log Levels in Winston
Winston supports different log levels based on severity:
error– Critical issueswarn– Warning messagesinfo– General informationhttp– HTTP request logsverbose– Detailed logsdebug– Debugging informationsilly– Very detailed logs
You can control which logs are recorded by setting the log level.
Logging to Files
In production, logs are often saved to files:
This setup:
- Stores error logs in
error.log - Stores all logs in
combined.log
Logging to Console and File Together
You can combine multiple transports:
Custom Log Format
Winston allows you to customize how logs appear:
This adds timestamps and formats logs in a readable way.
Logging HTTP Requests
You can log incoming HTTP requests in Express:
This helps track API usage and debug request-related issues.
Using Winston in a Separate File
For better structure, you can create a separate logger file:
Then use it in your application:
Logging Errors
Winston can be used to log application errors:
Benefits of Structured Logging
Structured logs (JSON format) make it easier to analyze logs using tools like Elasticsearch, Logstash, and Kibana (ELK stack).
This is especially useful in large-scale applications and microservices architecture.
Best Practices for Logging
- Use appropriate log levels
- Avoid logging sensitive data like passwords or tokens
- Use structured logs (JSON) for production
- Store logs in files or external systems
- Monitor logs regularly
- Rotate log files to avoid large file sizes
Common Mistakes
- Using console.log in production
- Logging too much unnecessary data
- Not separating error logs
- Not using proper log levels
- Ignoring log monitoring
Real-World Use Cases
- Debugging production issues
- Tracking API requests
- Error monitoring
- Security auditing
- Performance tracking
Conclusion
Winston is a powerful logging solution for Node.js applications. It helps developers manage logs efficiently, making debugging and monitoring much easier in production environments.
By using structured logging, multiple transports, and proper log levels, you can build a reliable and scalable logging system for your application.

