Structured logging involves logging in a format that makes it easy to search and analyze logs. JSON is a common structured logging format.
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.Console()
]
});
logger.info('User login', { userId: 123, username: 'john_doe' });
Structured logs are easy to query and analyze with tools like Elasticsearch, Kibana, or log aggregators.
Leave a Reply