Reputation: 7302
I'm writing a rest api with NodeJS and express and I'm using express-winston to log accesses and erros. But I want to separate de log daily. Like in this post
I'm trying to do so with winston.transports.DailyRotateFile. A piece of code below.
api.use(expressWinston.logger({
transports: [
new winston.transports.DailyRotateFile({
name: 'file',
datePattern: '.yyyy-MM-ddTHH',
filename: path.join(__dirname, "log-access", "log_file.log")
})
]
}));
Then I receive the error: winston.transports.DailyRotateFile is not a function
I guess I have to install another pack, since reading winston's doc I found that you can write custom transports.
Would you have information on which package I would have to install? I found some that doesn't match or was discontinued.
Thanks for any help
Upvotes: 4
Views: 10869
Reputation: 1966
My take with import, ES6 and TypeScript:
package.json
"winston-daily-rotate-file": "5.0.0"
logger.ts
import { createLogger, transports, format } from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
...
export const logger = createLogger({
level: 'info',
format: format.combine(
// format.colorize(), // messes up the log file
format.printf((info) => {
return `${info.level}: ${info.message}`;
}),
),
// prettyPrint: true,
transports: [
new transports.Console,
new DailyRotateFile({
filename: `my-app-%DATE%.log`,
dirname: config.logFileDir,
datePattern: 'YYYY-MM-DD-HH',
maxSize: 20971520, //20MB
maxFiles: 3,
})
]
});
That's it, the server, log and morgan settings are not in the scope of this question.
Upvotes: 0
Reputation: 101
You do not need to assign:
var winston = require('winston'), expressWinston = require('express-winston');
winston.transports.DailyRotateFile = require('winston-daily-rotate-file');
You just need to require
it:
const winston = require('winston');
require('winston-daily-rotate-file');
Upvotes: 7
Reputation: 7302
I had to do this so it would work:
var winston = require('winston'), expressWinston = require('express-winston');
winston.transports.DailyRotateFile = require('winston-daily-rotate-file');
I already had the right package but it wouldn't work, until I wrote the lines above.
Upvotes: 7
Reputation: 783
What you're looking for is this module.
Just follow the documentation, and you're good to go.
Upvotes: 1