I have a node.js app. I need to log each event in this program and I use winston library for this. I am trying to create a log file for each day. All logs should be separated day by day. I want to save the daily files on my desktop. For example the requests (actions in my program) that I have done today will be saved as 18/10/2019.log on my desktop. And tomorrow when I do some requests again (e.g: get, post) should be logged as 19/10/2019.log on desktop again. I can't do this. Any suggestions?
EDIT: SOLVED!!
With winston-daily-rotate-file, it is resolved. To use that module:
npm install winston-daily-rotate-file
THE CODE:
require('winston-daily-rotate-file');
const logDir= 'C://Users/Desktop/LogFiles';
var options = {
file: {
level:'info',
filename: path.resolve(`${logDir}/${new
Date().getFullYear().toString()} - ${new Date().getMonth()+1}/%DATE%.log`),
datePattern: 'YYYY-MM-DD',
timestamp: new Date()
};
let logger = winston.createLogger({
level:'info',
format: winston.format.combine(
winston.format.printf(info => { return `${info.timestamp} ||
${info.level} || Message: ${info.message}`; })
),
transports: [
new winston.transports.DailyRotateFile(options.file)
],
exitOnError: false,
});