Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format error logs with Winston in Node.JS?

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?

like image 915
Some User Avatar asked Apr 07 '19 11:04

Some User


People also ask

What is Winston in Node JS?

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.

What is a log file in Node JS?

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.

What are the best logging libraries for Node JS?

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.

What are the different types of logs in Winston?

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.


1 Answers

As of the documentation winton only supports that format parameter for info logging.

enter image description here

like image 152
Janith Avatar answered Nov 03 '22 00:11

Janith