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.
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.
'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).
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.
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.
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.
npm install --save winston-mongodb
require('winston-mongodb');
const options = {};
winston.add(winston.transports.MongoDB, options);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With