Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 7, 2020 08:11 am GMT

Hide all console logs in production with just 3 lines of code

We basically use the console.log() in our JS application to check wether our code is working properly or to replicate the bug or issue in the app. without the logs it is very time consuming and difficult to find the problem.

But these logs are meant for the developer only and you don't want to show these to the end users so we have to remove the console statement or to comment that.

Alt Text

Before i know this stuff i was commenting all the console.logs in my application and deploying the app on the live server. so the console's are not visible to the users

How I hide all the consoles

if (env === 'production') {    console.log = function () {};}

Here we are overriding the default console.log function with the new one which was returing nothing. Here we have added the environment check to override console function only if the environment is production. if you don't have environment variable then you can jsut simply do.

    console.log = function () {};

I am using this on my live app to hides the console. If someone know any other method or any disadvantage of using this one. plz leave your comment.

To keep up with everything Im doing, follow me on Twitter. I am posting these hacks and tricks there also ==> Kushal Sharma


Original Link: https://dev.to/sharmakushal/hide-all-console-logs-in-production-with-just-3-lines-of-code-pp4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To