What is a recommended third-party library for robust file logging in Node.js, and how do I set it up?

Responsive Ad Header

Question

Grade: Education Subject: Support
What is a recommended third-party library for robust file logging in Node.js, and how do I set it up?
Asked by:
101 Viewed 101 Answers

Answer (101)

Best Answer
(936)
Winston is a widely recommended and highly robust logging library for Node.js. It offers multiple transports (console, file, HTTP, etc.), log levels, and custom formatting. To set up Winston for file logging, you first install it via npm (`npm install winston`). Then, you configure a logger instance with a `winston.transports.File` transport, specifying the filename and desired log level. Example: ```javascript 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(), new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ] }); logger.info('Application started.'); logger.warn('A potential issue detected.'); logger.error('An unhandled error occurred!'); ```