DEV Community

Cover image for Say Goodbye to console.log from Production environment
Gulshan Aggarwal
Gulshan Aggarwal

Posted on

Say Goodbye to console.log from Production environment

I have been working on a project with Next.js since last month and used the console.log statement in lots of components & files even though I don't remember. Usually, we use it to check API responses and some other areas of the application.

Making console statements public is not a good idea at all, it may bring your app at a security risk. Checking every file, and deleting the statement is very time-consuming & boring too. As a developer, we all hate boring tasks so, in the article here we discuss how you can say goodbye to console.log in your Next.js app from the Production environment in just less than 1 minute.

sounds good

Sounds good! let's goooo...๐Ÿ˜Ž

๐Ÿ‘‰ Follow the steps mentioned below -

1. Install the babel-plugin-transform-remove-console package as a development dependency.

2. Create a .babelrc file at the root of your project, copy the below content & that's all.



{
    "presets": [
        "next/babel"
    ],
    "env": {
        "production": {
            "plugins": [
                "transform-remove-console"
            ]
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

๐Ÿ This is just a short trick that I used to enhance my productivity, even you should use it next time.
I will keep sharing more tips & tricks related to web dev,stay tuned!.

Thanks for reading.

goodbye

Latest comments (59)

Collapse
 
littletreeme profile image
littleTreeme

hi๏ผŒyour article is so good๏ผ Can I translate your artical and post to my wechat public account๏ผŸ

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

Sure, but don't forget to give me credits

Collapse
 
dikamilo profile image
dikamilo

I prefer to create a custom logger wrapper/class and use it in app. In react apps as custom context/provider with hook to get logger instance in components.

Why? Several advantages:

  • log to multiple services, not just console if necessary
  • different logger implementation for tests, you can mock logger methods and assert them in tests (if you have business flow logs, then this is useful)
  • different logger implementation for storybook: you can see logs as actions in storybook
  • production? Just use dummy logger that do nothing or log only to production services

"Checking every file, and deleting the statement is very time-consuming"
ESLint's rule to deny console logs + ignore them just in console logger implementation

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

Actually, while searching for the solution I had no idea about logger services so I solved it using the package. Yeah! thanks for the detailed solution โ™ฅ

Collapse
 
andrewbaisden profile image
Andrew Baisden

Linting rules seem like a great solution for this.

Collapse
 
attkinsonjakob profile image
Jakob Attkinson

How about an ESLint rule for no console and a pre-commit hook?

This way, you don't need any ninja-hacks. You do your testing and remove them before the commit.

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

Hey Jakob, you are right! but If I say when your are too close to ship?

Collapse
 
attkinsonjakob profile image
Jakob Attkinson

I don't think I'm following?

Thread Thread
 
gulshanaggarwal profile image
Gulshan Aggarwal

what do you mean? ๐Ÿค”

Collapse
 
abdurrkhalid333 profile image
Abdur Rehman Khalid

Some may be thinking that why we are not allowed to use the console.log in the production plus some other development enviornemnt as well, so the answer is that if you are fetching a lot of data from Back-End and printing that data in the Front-End in any framework then it is quite possible that, that data will again some time to get printed at the console so due to this reason we should avoid printing direct data to the console.

The other thing that has been mentioned in this article is the security risk, this also put the application on the risk as well.

This plugin looks good but it is there is a need to communicate the need and usage of this plugin to the other members of the team as well. However, I appreciate that you have bring this matter of writing console log on the production deployed application. This is a critical thing to which only a few developers pay attention, more power to you and looking forward to see some important articles from your side. Good Luck.

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

Thanks for the detailed explanation & lovey wishes! ๐Ÿค—

Collapse
 
pixeliner profile image
Pixeliner

console.logs should just be used to bug check and afterwards removed. Don't leave it in the source code, especially not in big companies. You can maybe create a peek functionality in your utils, but that's that. Using build tools to remove code is just bad practice.

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

True ๐Ÿ’ฏ but what do you mean by build tools to remove code ?

Collapse
 
nicoatridge profile image
nicoatridge

I just add this code:
let testMode = true; // console log suppressed if false
if (!testMode) {console.log = function() {};} // to suppress console log

Collapse
 
ayyash profile image
Ayyash

you don't need a plugin, you can wrap it yourself: dev.to/ayyash/writing-a-wrapper-fo...

 
gulshanaggarwal profile image
Gulshan Aggarwal

Next time, I'm gonna surely try all these suggestions but this time I will continue it.

Collapse
 
gulshanaggarwal profile image
Gulshan Aggarwal

I couldn't find something that's the reason I tried it but now lots of solutions are available in comments.