When I create a nodejs winston console logger and set json:true
, it always output JSON logs in multiline format. If I pipe these to a file and try to grep that file, my grep hits only include part of the log line. I want winston to output my log lines in JSON format, but not to pretty print the JSON
Here is my config (coffeescript, apologies):
winston = require 'winston'
logger = new (winston.Logger)(
transports: [
new winston.transports.Console({
json: true
})
]
)
And some sample output:
{
"name": "User4",
"level": "info",
"message": "multi line whyyyyy"
}
will pretty print the json object
On
"winston": "^3.2.1"
This is works great for me
"winston": "^3.0.0"
Output:
2018-08-11T13:13:37.554Z [info] : {"data":{"hello":"Hello, World"}}
winston 3.x (current version)
Default formatter
Example
Custom formatter
Example:
winston 2.x (legacy version)
It seems that the accepted answer is outdated. Here is how to do this for current winston version (2.3.1):
Note the parenthesis around
winston.transports.Console
.The winston transports provide a way to override the stringify method, so by modifying the config above I got single line JSON output.
New config: