How to write custom logging function in JavaScript

Spread the love

In this article we will be discussing, how to write custom logging function in JavaScript. Javascript provide various logging function by default like:

Why Custom Logging:

But there are various instances when you would like to have custom logging function. Some of these instances might be like :

  1. Logging messages based on components basis(Each logging message have component name prefixed).
  2. Enable/disable logging with a flag, e.g., isDebug = true i.e You might want to have logs while development while no logs for production environment.
  3. Short logging syntax i.e logs(“Hello world”);

 

Above are just some common example in case of web app evelopment. Let’s not waste any more of our time and start coding.

 

Custom Logging function with prefixing app name:

 

 

Custom Logging function prefixing app name & component name:

 

 

Custom Logging function which Enable/disable logging with a flag:

 

That’s it for this article. You can provide suggestion or initiate discussion on this thread below in comment section.

 

Leave a Reply

Your email address will not be published. Required fields are marked *