2

I am new to winston and trying to store nodejs server logs into file.

const {
    createLogger,
    transports,
    format
} = require('winston');

const logger = createLogger({
    transports: [
        new transports.File({
            filename : 'info.log',
            level: 'info',
            format: format.combine(format.timestamp(), format.json())
        })
    ]
})

module.exports = logger;

And I can store log in the file using logger.log('info',"Inside /login")

How can I store the nodejs server logs which are generated automatically run time when error happens.

For example this log which I can see in console when the mySQL db connection was closed and these logs were generated by nodejs server.

enter image description here

Archit Sandesara
  • 605
  • 1
  • 12
  • 25
  • You question already answer here https://stackoverflow.com/a/43054361/10526425 – Lamri Djamal Dec 03 '20 at 17:39
  • It doesn't answer my question as it is writing `logger.log('error', 'test error message %s', 'my string');` but here I have mentioned the logs I want in the file are generated dynamically. Check the example I have provided in the question. – Archit Sandesara Dec 03 '20 at 19:25
  • One of the way to do is override the method which is called when the logs are displayed on console and store it in file as on production we don't have console. – Archit Sandesara Dec 03 '20 at 19:28
  • It doesn't answer my question as it is writing `logger.log('error', 'test error message %s', 'my string');` but here I have mentioned the logs I want in the file are generated dynamically/unexpected errors while using in production. Check the example I have provided in the question. – Archit Sandesara Dec 03 '20 at 19:30

0 Answers0