My Blog

My WordPress Blog

My Blog

My WordPress Blog

Log Enrichment

Enrich logs with additional context or metadata. For example, you might add user-specific information or request context.

const winston = require('winston');

const requestLogger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
winston.format.timestamp(),
winston.format.json(),
winston.format.printf(({ level, message, timestamp, requestId }) => {
  return ${timestamp} [${level}] [RequestId: ${requestId}] ${message};
})
), transports: [
new winston.transports.Console()
] }); // Middleware to attach requestId app.use((req, res, next) => { req.requestId = Math.random().toString(36).substring(2); next(); }); // Log with requestId app.get('/', (req, res) => { requestLogger.info('Handled request', { requestId: req.requestId }); res.send('Hello World!'); });
Log Enrichment

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top