How to use Node.js Winston in several modules?

Sometimes, we want to use Node.js Winston in several modules.

In this article, we’ll look at how to use Node.js Winston in several modules.

How to use Node.js Winston in several modules?

To use Node.js Winston in several modules, we can create a module to configure the options for Winston.

Then we can require the logger module we created in the entry point file and require winston in other modules.

For instance, we create the log.js module by writing

const winston = require('winston');

winston.configure({
  level: "debug",
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.simple()
  ),
  transports: [
    new winston.transports.Console()
  ]
});

Then we in the app.js entry point file, we add

require('log.js')

And in other modules, we write

require ('winston')

so we can use the logger in any module.

Conclusion

To use Node.js Winston in several modules, we can create a module to configure the options for Winston.

Then we can require the logger module we created in the entry point file and require winston in other modules.