There are several ways to create a logging system with Node.js. One simple way is to use the console methods provided by Node.js like console.log, console.error, console.warn etc. But for a more sophisticated logging system you can use a logging library like Winston or Bunyan.
Here is a basic example of how to set up a logging system using Winston:
1. First, install Winston via npm:
```
npm install winston —save
```
1. Include Winston in your project:
```
const winston = require(‘winston’);
```
1. Define an instance of logger:
```
const logger = winston.createLogger({
level: ‘info’,
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: ‘error.log’, level: ‘error’ }),
new winston.transports.File({ filename: ‘combined.log’ })
]
});
if (process.env.NODE_ENV !== ‘production’) {
logger.add(new winston.transports.Console({
format: winston.format.simple()
}));
}
```
1. Then you can use the logger in your app like this:
```
logger.info(‘Hello world’);
logger.warn(‘Warning message’);
logger.error(‘Error info’);
```
This will log messages to the console in non-production environments and log error messages and info messages to different files in all environments.
The log level indicates the severity of the messages being logged. Winston uses the following levels: error, warn, info, http, verbose, debug, silly. The log function takes two arguments, the message and an optional meta object containing additional info.
You can customize your logger instance as per your preference. For example, you can add a timestamp to the logs, handle uncaught exceptions etc. Check out the Winston documentation for more info.