Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Winston: multi process logging

I have created a nodejs application which encapsulates four nodejs processes. Till now all the individual nodejs processes are using winston npm for logging to different log files. Now I want to make a single log file where every node process can log.

Does winston implicitly ensures the serialization of logging data to make it process safe(multiple process writing to same file without bothering about race conditions or deadlocks etc.)? or it's developer work to ensure only one process exclusively writes to the log file at a certain time.

like image 263
Mintu Kumar Avatar asked Jan 12 '18 10:01

Mintu Kumar


People also ask

What is Winston logging?

Winston is the most popular logging library for Node. js. It aims to make logging more flexible and extensible by decoupling different aspects such as log levels, formatting, and storage so that each API is independent and many combinations are supported. It also uses Node.

How many log levels does Winston support?

'winston' by default uses npm logging levels. Here the severity of all levels is prioritized from the most important to least important from 0 to 6 (highest to lowest).

Does Winston use console log?

With Winston, you can send and save logs in different ways, such as files, databases, emails, and console. Log formats. Winston provides you with several log formats. For example, when saving a log to a Mongo database, the log format needs to be in JSON format.

How do you log objects in Winston?

You can do something like: const { createLogger, format, transports } = require('winston'); const { splat, combine, timestamp, label, printf, simple } = format; const logger = createLogger({ format: combine( label({ label: 'CUSTOM', message: true }), timestamp(), simple() ), transports: [new transports.


1 Answers

Does Winston implicitly ensures the serialization of logging data to make it process safe?

The answer is no.

Data is lost when you have multiple processes writing logs to the same file through Winston. This is actually a know issue that they decided to not address properly.

There are a lot of options, you could change your logging tool, use inter-process communication and only call Winston through the master process, use a message broker or even write the logs to a database for example.

Assuming that your software uses MongoDB, the last one is really easy to achieve with the help of winston-mongodb.

1. Install it using NPM

npm install --save winston-mongodb

2. Configure it

require('winston-mongodb');

const options = {};
winston.add(winston.transports.MongoDB, options);
like image 125
celicoo Avatar answered Oct 11 '22 22:10

celicoo