console.log and other debugging statements are left here and there. Obviously, at one point those statements need to be removed for the production version or even when the code needs to be checked in into the source repository. There are many different ways to do this, there exists a new tool called
groundskeeper which can do this removal for you.
Written for Node.js, groundskeeper (GitHub: github.com/Couto/groundskeeper) is created by Luís Couto to handle logging removal by understanding the syntax tree of the code and deleting the relevant parts. It is not based on regular expression at all. Groundskeeper parses the code (via Esprima) and modify the syntax nodes (via falafel) associated with any logging. Beside a command-line tool, groundskeeper is also a library ready to be used in any other tools and build systems.
Using groundskeeper is pretty simple (as its documentation explained). Let’s assume we have the following
1 2 3 4 5 6 7 8 9 10
After its package is installed, running something like:
groundskeeper < filter-debug.js > filter.js
will give the following
1 2 3 4 5 6 7 8 9 10
The line containing
console.log is now a blank line. Because groundskeeper doesn’t delete the line, the processed output still has the same amount of lines of code as the original one.
debugger? We know that this statement may cause some problems with old web browsers. Fortunately groundskeeper can also get rid of any debugger statements in the code. In fact, you can even nuke any other custom application logger you might have. Here is a code fragment:
1 2 3
which can be processed via:
groundskeeper -n RentalApp.Logger < rental.js
and give you the remaining output:
var list = filter(customers, 25);
The biggest benefit of automatic removal is when it is combined with Git pre-commit hook so that no more manual step is necessary. This is pretty similar to the use of syntax validation trick. Here is an example of such a hook:
1 2 3 4
*.js files will be processed via groundskeeper before they are checked in. This way, none of them will have stray
debugger statements, or any other custom logger calls anymore. Thus, your repository is guaranteed to be free from debugging leftovers!