Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda