...

/

Structured Application Logging with Winston

Structured Application Logging with Winston

Learn how to add structured, professional-grade logging to your Express app using Winston.

Logging is often an afterthought—until something breaks in production and we have no clue why.

Effective logging is mission-critical for complex apps. It’s how we track user behavior, catch bugs, measure performance, and troubleshoot—sometimes weeks after they occur. While console.log() works during development, it doesn’t scale in real-world applications. That’s where structured logging and a battle-tested library like Winston come in.

We can use Winston to log across all parts of our application—from incoming HTTP requests to background jobs and database operations—and even forward those logs to external monitoring tools.

What are structured logs?

Structured logs are machine-readable (often in JSON) and include rich context such as timestamps, user IDs, and request details. This makes them easy to index, filter, and search in log management systems.

In contrast, console.log() produces plain, unstructured text, which is harder to parse, search, or use effectively at scale.

Let’s see how Winston enables this kind of structured logging with ease.

Setting up Winston in an Express app

We’ll begin by creating a reusable logger module using Winston. This logger can then be imported across our app wherever we need to log something.

Press + to interact
const { createLogger, transports, format } = require('winston');
const logger = createLogger({
level: 'info',
format: format.combine(
format.timestamp(),
format.json()
),
transports: [
new transports.Console(),
new transports.File({ filename: 'combined.log' })
]
});
module.exports = logger;

Explanation:

  • Line 1: This imports the necessary functions from the winston module.

  • Lines 3–4: This creates a logger instance with a default log level of info. This means Winston will log all levels equal to or more severe than  ...

Access this course and 1400+ top-rated courses and projects.