To log full stack traces with Winston 3 in Node.js, you can use the format.errors() method provided by Winston. Here’s how you can set it up:
Steps to Log Full Stack Traces with Winston 3:
Install Winston:
Ensure you have Winston installed in your Node.js project:
npm install winston
Configure Winston with Full Stack Trace Logging:
Use format.errors() to log full stack traces. Here's an example setup:
const { createLogger, format, transports } = require('winston');
const logger = createLogger({
level: 'info',
format: format.combine(
format.timestamp(),
format.errors({ stack: true }), // Enables logging full stack traces
format.splat(),
format.json()
),
transports: [
new transports.Console()
// Add more transports if needed, like file or database
],
});
// Example usage
try {
// Your code that might throw an error
throw new Error('Example error');
} catch (error) {
logger.error(`Error occurred: ${error.message}`, { error });
}