Implement structured logging (Winston, Pino)
✓Works with OpenClaudeYou are a Node.js logging engineer. The user wants to implement structured logging using either Winston or Pino to enable machine-readable log output with consistent formatting and metadata.
What to check first
- Run
npm list winston pinoto see if either logging library is already installed - Check your Node.js version with
node --version(both libraries support Node 12+) - Inspect your current logging calls to identify what metadata you're tracking (user IDs, request IDs, timestamps)
Steps
- Install Pino (lightweight, faster) with
npm install pinoor Winston withnpm install winston - Create a
logger.jsfile to centralize logger configuration and exports - Define a logging format that includes timestamp, level, message, and custom fields as key-value pairs
- Add transport configuration to write logs to stdout for development and file system for production
- Implement context middleware to attach request IDs and user metadata to every log entry
- Replace all
console.log()calls withlogger.info(),logger.error(), etc. with structured metadata as the second argument - Configure log rotation for file transports to prevent disk space issues
- Test log output in JSON format by piping to
jqto verify field accessibility
Code
// logger.js - Pino configuration for structured logging
import pino from 'pino';
import path from 'path';
import fs from 'fs';
const isDev = process.env.NODE_ENV !== 'production';
// Create logs directory if it doesn't exist
const logsDir = path.join(process.cwd(), 'logs');
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir);
}
const pinoConfig = {
level: process.env.LOG_LEVEL || (isDev ? 'debug' : 'info'),
timestamp: pino.stdTimeFunctions.isoTime,
};
const transports = isDev
? pino.transport({
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname',
},
})
: pino.transport({
targets: [
{
level: 'info',
target: 'pino/file',
options: { destination: path.join(logsDir, 'app.log') },
},
{
level: 'error',
target: 'pino/file',
options: { destination: path.join(logsDir, 'error.log') },
},
],
});
const logger = pino(pinoConfig, transports);
// Middleware to attach request context
export const requestLogger = (req, res, next) => {
req.logger = logger.child({
requestId: req.id || crypto.randomUUID
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Monitoring & Logging Skills
Other Claude Code skills in the same category — free to download.
Error Tracking
Set up error tracking (Sentry)
APM Setup
Set up Application Performance Monitoring
Log Rotation
Configure log rotation and management
Health Dashboard
Create health monitoring dashboard
Alert Rules
Configure alerting rules and notifications
Distributed Tracing
Set up distributed tracing
Metrics Collector
Implement custom metrics collection
Uptime Monitor
Set up uptime monitoring
Want a Monitoring & Logging skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.