Reputation: 137
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,
});
Upvotes: 1
Views: 4158
Reputation: 151
You can manually create each folder with Date()
Example : 3052021 (For 30-MAY-2021)
let errorLogFolderName = new Date().toLocaleDateString().replace(/\D/g, ''); //creating folders each day
function logger() {
return createLogger({
//level: 'info',
format: combine(
timestamp(),
errors({ stack: true }),
printf(info => {
return `${info.timestamp} [${info.level}] : ${(info.stack || info.message)}`;
})
),
transports: [
new transports.File({ filename: `./logs/${errorLogFolderName}/dailylog.log` })
]
});
}
Upvotes: 0
Reputation: 741
Install winston daily rotate
npm install winston-daily-rotate-file
Note: Check for compatibility here first
var winston = require('winston');
require('winston-daily-rotate-file');
var fileRotateTransport = new (winston.transports.DailyRotateFile)({
filename: '%DATE%.log',
datePattern: 'DD/MM/YYYY',
maxSize: '20m'
});
var logger = winston.createLogger({
transports: [
fileRotateTransport
]
});
Check for the winston daily rotate options to configure as per your requirements.
Upvotes: 6
Reputation: 415
var winston = require('winston');
function getLogger(module) {
var path = module.filename.split('/').slice(-2).join('/');
return winston.createLogger({
transports: [
new winston.transports.Console({
colorize: true,
level: 'debug',
label: path
})
]
});
}
module.exports = getLogger;
put this seperate file in project file
Upvotes: 0
Reputation: 199
Try this :
Change errorLogFileName to date format which is comfortable to you. Logs will be appended to same file on same day. New file is created if date will changes.
**let errorLogFileName = new Date().toLocaleDateString()+'error.log';**
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: errorLogFileName, level: 'error' }),
new winston.transports.File({ filename: 'combined.log' })
]
});
Upvotes: 1