-1

I need to have my logs in .json format and currently I use express-js server and my logs looks the following:

Error: Cannot return null for non-nullable field Query.get_bar.
Error: Cannot return null for non-nullable field Query.get_bar.
Error: Cannot return null for non-nullable field Query.get_foo.

I figured out there're some logger frameworks (log4js, winstonjs) but I don't want to log manually (i.e., console.log(123)), I'm interested in redirecting my STDOUT, converting it to JSON and writing to some .log file.

What's the most simple solution for this task?

Update: there's a related issue on GitHub.

A. Karnaval
  • 727
  • 2
  • 8
  • 12

2 Answers2

1

The process.stdout and process.stderr pipes are independent of whatever actual code you're running using Node, so if you want their output sent to files, then make your main entry point script capture stdout/stderr output and that's simply what it'll do for as long as Node.js runs that script.

You can add log writing yourself by tapping into process.stdout.on(`data`, data => ...) (and stderr equivalent), or you can pipe their output to a file, or (because why reinvent the wheel?) you can find a logging solution that does that for you, but then that's on you to find, asking others to recommend you one is off topic on Stackoverflow.

Also note that stdout/stderr have some sync/async quirks, so give https://nodejs.org/api/process.html#process_a_note_on_process_i_o a read because that has important information for you to be aware of.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
-2

The example from winston basically solved the issue.

A. Karnaval
  • 727
  • 2
  • 8
  • 12