For web developers console.log() is Boring.
Everyone who is learning or knows JavaScript uses console.log() frequently.
Basically the console.log method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.
It seems like it is extremely handy method. Yes it is, but in case of debugging, NO its not.
Why?
The problem while debugging is after debugging we generally prefer to remove or comment all the console.log() if we found that there is no errors in the implementation of logic. Now if we found out that there are few new errors caught by team or self then we have to again write or uncomment the console.log(). Its tedious task and is Boring.
So what is Solution?
Solution is to use a seperate npm package to make our work more easy.
We will use npm package named “debug” to make our task more systematic and will add some fun in debugging. Note that the version that i installed is :
"debug": "^4.3.1"
Install debug
npm i debug
With debug we can control, which type of debugging should be visible using namespaces. And by using namespace we can define environment variable. Lets do it step by step.
Step 1:
const debug = require(‘debug’);
Step 2:
This “require” function returns a function so here constant debug
is a function, now lets call the debug function by passing a namespace as
argument. You can create namespace with any names.
debug(‘nameSpace : what_kind_of_debugging_messages_you_want_to_see’)
lets say in your app (named ‘nodeGod’) there is index.js file and in that file you are dealing with two things one is calculation and other one is database. Now you want to see the error message for both functioning separately so you can create a namespace like:
for getting data
debug(‘nodeGod : calculation’)
for deleting data
debug(‘nodeGod : database’)
Instead of using ‘type of debugging message‘ you can also use the ‘file name’.
Step 3:
But the question is why we have created this namespace?
It will be extremely handy for us to decide while running our app that what kind of debugging message we want to see or we don’t want to see any messages.
‘debug’ function again return a function which allow us to write a debugging message in given namespace. So we will save it as variable.
const calDebugger = debug(‘nodeGod : calculation’); const dbDebugger = debug(‘nodeGod : database’);
we can make above steps shorter and make it one liner code like this:
const calDebugger = require('debug')('nodeGod:calculation');
const dbDebugger = require('debug')('nodeGod:database');
Finally we can pass our error message as argument of function ‘calDebugger’ and ‘dbDebugger’ to display on console.
calDebugger('successfully Completed all calculations....');
dbDebugger('successfully connected to database....');
step 4:
My index.js file look like below:
const express = require("express");
const calDebugger = require('debug')('nodeGod:calculation');
const dbDebugger = require('debug')('nodeGod:database');
const app = express();
// calculation section
calDebugger('successfully Completed all calculations....');
// database related section
dbDebugger('successfully connected to database....');
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`listening on port ${port}...`));
To control the debugging message using namespace & environment variable, we need namespace and environment variable. Namespace is already created so now we need to set an environment variable, lets create an environment variable named “DEBUG” on cmd :
For windows
set DEBUG = namespace_that_is _created_by_you
For Mac
export DEBUG = namespace_that_is _created_by_you
For Ex:
Here we are logging only debugging message about calculation
(Note that after setting an environment variable we are running our app.)
Here we are logging all debugging message of namespace ‘nodeGod’.
OR
You can include all the debugging messages for namespaces that were created by you with below command.
set DEBUG=nodeGod:*
What if you don’t want to see any debugging message for that
Just set an environment variable with no value.
we can set an environment variable during runtime means to set environment variable and to run our app with one line of command.
DEBUG=nodeGod:*node index.js
Thank you For reading till last, I hope you get something from this blog.
Comments
Post a Comment