Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
Cannot override google app script's console.log()

I have a large google app script writing lots of log line to the log explorer. I want to be able to disable or enable logging if needed.

Here's a code that seems to work as expected on a browser. Here allowlog is set in code, but it will retrieved from external source. This is not important for the purpose of this post.

The self extracting function overrides console.log and blocks logging if needed.

let allowLog = false; 

function func1(){
  console.log("func1 log msg");
}

(function(){
  //test log 1
  console.log("self extracting");

  //keep ref to global console.log
  const _consolelog = console.log;

  //test log 2 - verify it works
  _consolelog("self extracting2");


//override global console.log
  console.log = function(val){
    if (allowLog){
      _consolelog(val);
      _consolelog("allowing");
    }else{
      _consolelog("log disabled");
    }

  }

})();

This code does not work on google app script, and logs keep on writing.

In app script I can see console.log("self extracting"); and _consolelog("self extracting2"); log messages when module is loaded. However when func1 is called log is written even though allowLog = false. _consolelog("allowing"); nor _consolelog("log disabled"); is NOT logged. the global console.log is not overridden.

Why is that, and how (if at all) it is possible to fix it?

The expected log for the above code is (allowLog=false):

  • self extracting
  • self extracting2
  • log disabled

For the case where allowLog=true:

  • self extracting
  • self extracting2
  • func1 log msg
  • allowing

The first two lines self extracting and self extracting2 should be printed only once, when the anonymous self invoking function is executed. The other should come from the internal function which overides the global one

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is because the log property of console is not writable. You can check that using Object.getOwnPropertyDescriptors. However, it is still configurable. So, you can delete the log property and set a new value.

let allowLog = false; 
(function(){
  //test log 1
  console.log("self extracting");

  //keep ref to global console.log
  const _consolelog = console.log;

  //test log 2 - verify it works
  _consolelog("self extracting2");


//override global console.log
//console.log(Object.getOwnPropertyDescriptors(console));
/*{ toString: 
   { value: [Function],
     writable: true,
     enumerable: true,
     configurable: true },
  time: 
   { value: [Function],
     writable: true,
     enumerable: true,
     configurable: true },
  timeEnd: 
   { value: [Function],
     writable: true,
     enumerable: true,
     configurable: true },
  error: 
   { value: [Function],
     writable: false,
     enumerable: true,
     configurable: true },
  info: 
   { value: [Function],
     writable: false,
     enumerable: true,
     configurable: true },
  log: 
   { value: [Function],
     writable: false,
     enumerable: true,
     configurable: true },
  warn: 
   { value: [Function],
     writable: false,
     enumerable: true,
     configurable: true } }*/
  delete console.log;
  console.log = function (val){
    if (allowLog){
      _consolelog(val);
      _consolelog("allowing");
    }else{
      _consolelog("log disabled");
    }
  }
})();

function func1(){
  console.log("func1 log msg");
}
about 4 years ago · Juan Pablo Isaza Report

0

Do you mean this?

Logger.log('Test1');  // it will print as usual

var _log = Logger.log
Logger.log = function(_){};

Logger.log('Test2'); // <-- it will not print since we disabled the 'log()' method

Logger.log = _log;   // restore the method 'log()' back
Logger.log('Test3'); // it will print again

Output:

Test1
Test3

enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!