I have a Node.js app that it using Winston for logging. I am printing my logs using printf like this:
winston.createLogger({
level: 'debug',
format: winston.format.combine(
winston.format.timestamp({ format: 'HH:mm:ss.SSSSS'}),
winston.format.printf(log => `[${log.level}] [${log.timestamp}] ${log.message} `)
),
transports: []
});
This works fine except in the case of "error" logs. When a log has a log level of 'error', the log writes an entry that's formatted like this: [${log.level}] [${log.timestamp}] ${log.message}${error.message}
. Notice, there is no space between the log message and the error message.
How do I put a space between the log message and an error message if the log level is an error?
Using Winston, a versatile logging library for Node.js. Today, we will explore Winston, a versatile logging library for Node.js. Winston can be used in numerous contexts including in Node web frameworks such as Express, and Node CLI apps.
Messages saved in the log file are called logs. A log is a single instance recorded in the log file. Structuring application logging in Node.js is critical. When running an app locally, you can hook it to a debugger, which is excellent, to find problems when running the app.
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.
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. Winston solves that for you. Log profiling. Winston helps you profile a block of code and measure the time the code takes to be successfully executed.
As of the documentation winton only supports that format
parameter for info
logging.
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