Remove console.log from Production Mode

Remove console.log from Production Mode

console.log is one the debugging weapon or logger we use as javascript developer. The console. log method is a way for developers to construct code that informs them about what the code is doing in a non-obtrusive way. But this tiny little snippets can do the following to our code base. log

๐ŸŽฏ Impact our app performance and increase our computation power and time at production level.

๐ŸŽฏ Also creates a variable and consumes memory, however minute.

๐ŸŽฏ Expose some information which might put your app at risk.

Let's consider the code snippet below

const { email, password } = req.body;
const user = await User.findOne({ email });
console.log(user);
if (!user || user === null) {
  return errorResMsg(res, 400, "this email does not exist");
}
//...
//create token
const token = await jwt.sign(
  {
    id: user._id,
    email: user.email,
    fullName: user.fullName,
  },
  process.env.USER_SECRET,
  {
    expiresIn: "2d",
  }
);
console.log(token);

In the code above, i logged the user and the token and this can be used by attackers to steal information from our app.

With that being said, let's look at two ways to remove console.log from our app

Vscode method

This method uses the search icon and regex to remove all logs

// Classes are templates for creating objects
// Method 1: Class function

class Person {
  constructor(name, age, occupation) {
    this.age = age;
    this.name = name;
    this.occupation = occupation;
  }

  todo() {
    console.log("kill");
  }
}

const createPerson = new Person("Abayomi", 78, "dev");
console.log(createPerson.todo());

// Method 2: Class Expression
const doSomething = class HouseChores {
  constructor(cut, clean, arrange) {
    this.cut = cut;
    this.clean = clean;
    this.arrange = arrange;
  }
};

const datInfo = {
  cut: (doSomething.cut = "grass"),
  clean: (doSomething.clean = "cars"),
  arrange: (doSomething.arrange = "house"),
};

console.log(datInfo);

// static types
class Music {
  constructor(viola, trombone) {
    this.viola = viola;
    this.trombone = trombone;
  }

  static musicConstant = "drums";
}

const result = new Music("Eb", "F#");
console.log(result);
console.log(Music.musicConstant); // static types are called without instantiating
  • Click on the search Icon Image description

  • Type console.log Image description

  • Click the regex option Image description

  • Click replace all

Image description


  • Click the replace option

Image description


  • Result: Image description

Method 2:

While method one is cool, i consider it as been destructive way. what if you need the logs during development mode again ๐Ÿ™„

Here is the work around.

Create .env file in your root project with NODE_ENV=development

Install the dotenv package and configure it

const env = require("dotenv");
env.config();

Now let's test our environment variable with our friend

console.log(process.env.NODE_ENV);

Image description

The last thing to write is a simple line of code

if (process.env.NODE_ENV === "development") {
  console.log = function () {};
}

The code above says if our environment variable is in development mode, output an empty function that says nothing.

With the snippet active, If you run your code, you should get nothing from the terminal.

Image description

With the snippet commented out, it log results to our terminal Image description


Discuss

What other methods can you use other than the aforementioned.

ย