Friday, October 8, 2021

Tip/trick for Visual Code logpoint debugging - nodejs

TLDR; you can decorate logpoint messages to include the file and line number. Useful for tracking the execution of nodejs code/module. 

Add these lines of code to the beginning of the file:


You just need add it once. In case you are running multiple test files, include this code in the file that would execute the earliest. So for e.g., if you use mocha as your test runner, this piece of code could go in a require script (mocha --require test/init.js test/**/*.spec.js).

So the getTrace() is now available globally.

Next, add your logpoint messages in the way shown below:




Thats it!!! Now watch the magic unfold when you run your code in debugging mode, and, these logpoints hit. You can either view them in the Debug Console. (Even elsewhere depending on how you started your debugging session).