Reputation: 1265
Let us say I have the following API's and their respective routes as following -
1) /api/v1/teachers - For CRUD operation of teachers
2) /api/v1/students - For CRUD operation of students
Currently, I am storing logs of both the API requests in combined.log file. I want to store logs of CRUD operations of teachers in teachers.log file and logs for CRUD operations of students in students.log file.
I have referred the following question in which the author wants to store certain logs based on certain environment.
Winston/Node.js how add a Transport only for certain environment?
const winston = require('winston');
const transports = [
new winston.transports.Console({
level : 'info',
colorize : true
}),
new winston.transports.File({
level : 'info',
filename : './logs/logs.log'
})
];
if (process.env.NODE_ENV === 'production') {
const Mail = require('winston-mail').Mail;
transports.push(new Mail({
to : '[email protected]',
from : '[email protected]',
subject : 'Errors occurred',
level : 'error',
host : 'smtp.xxxxx.xx',
username : '[email protected]',
password : 'xxxxx',
port : 1234
}));
}
const logger = new winston.Logger({
transports
});
But since I am not able to access the request object itself in winston.js so I can not apply a condition-based on routes to store logs of different routes in a different file.
Upvotes: 1
Views: 1072
Reputation: 7112
You can create your 2 transports and use express-winston to choose the specified transport before your router.
var router = require('./teachers-router');
app.use(expressWinston.logger({
transports: [
new winston.transports.teachers, // you will need to create this one by your own
],
}));
app.use(router);
Upvotes: 1