0

I am using Winston library in nodes for error logs. Now what I want is to create multiple logger files dynamically.

The code I am using is this

const fs = require("fs");
const winston = require("winston");
const logDir = "log";

if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const tsFormat = () => (new Date()).toLocaleTimeString();
module.exports = logger = winston.createLogger({
    transports: [
        new (winston.transports.Console)({
            format: winston.format.combine(
                winston.format.colorize(),
                winston.format.timestamp(),
                winston.format.align(),
                winston.format.simple(),
            ),
            level: 'info'
        }),
        new (require("winston-daily-rotate-file"))({
            filename: `${logDir}/-results.log`,
            format: winston.format.combine(
                winston.format.timestamp(),
                winston.format.json(),
            )
        }),
        new winston.transports.File({ 
            filename: 'log/error.log', 
            level: 'error',
            format: winston.format.combine(
                winston.format.timestamp(),
                winston.format.simple(),
            )
        }),
    ]
});

I need something like if I pass a filename calling logger

logger.info(`Test info Log!`,'filename');

It should log file in this filename. if file doesn't exist it creates it and appends everything in that file

1

1 Answer 1

1

What you are trying to achieve here is creating the log files dynamically by specifying the filename.

Which is Not Possible as we have to declare the files and logger options while we instantiate the logger instance in the winston.js file.

Here:

module.exports = logger = winston.createLogger({}

And you cannot update it on the go!

Hope I have cleared your thoughts!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.