简体   繁体   English

Google Apps Script V8 vs Rhino 的“日志”非常慢?

[英]Very slow "Logs" with Google Apps Script V8 vs Rhino?

With Rhino, Logs dialog ("command + Enter" or Logs from View menu) shows logs instantly.使用 Rhino,日志对话框(“command + Enter”或“视图”菜单中的日志)会立即显示日志。 However, with test projects using V8 engine it takes 10-20 seconds to load even the simplest logs, with a message "Waiting for logs, please wait..."但是,对于使用 V8 引擎的测试项目,即使加载最简单的日志也需要 10-20 秒,并显示“正在等待日志,请稍候...”消息。

Both, "Logger.log" or "console.log" are slow to load logs. “Logger.log”或“console.log”加载日志都很慢。 Is anyone else experiencing the same type of slowness?有没有其他人遇到同样类型的缓慢? Is this expected with the new engine?这是新引擎的预期吗?

Thank you in advance!先感谢您!

Here's a basic function I used for testing:这是我用于测试的基本功能:

function logTest() {
 Logger.log("log test");
}

I noticed the same thing and there is an issue about it already.我注意到了同样的事情,并且已经有问题了。

I've been using the below script.我一直在使用下面的脚本。 It's probably not bullet proof but for me it's better than waiting for the log.这可能不是防弹的,但对我来说,这比等待日志要好。 I also notice that if you have an error and go to View Executions that the logs appear to come out there now even before we get them on the script editor.我还注意到,如果您遇到错误并转到“查看执行”,那么即使在我们将它们放入脚本编辑器之前,这些日志现在似乎已经出现了。

Issue Link: https://issuetracker.google.com/issues/149519063问题链接: https : //issuetracker.google.com/issues/149519063

function MyLogger(s,t=5) {
  const cs=CacheService.getScriptCache();
  const cached=cs.get("Logger");
  const ts=Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "yy/MM/dd HH:mm:ss")
  if(cached) {
    var v=Utilities.formatString('%s<br />[%s] - %s',cached,ts,s);   
  }else{
    var v=Utilities.formatString('[%s] - %s',ts,s);
  }
  cs.put("Logger",v,t);
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(v), 'My Logger');
}

Another version of MyLogger(): MyLogger() 的另一个版本:

function MyLogger(s,d=true,w=800,h=400,t=5) {
  const cs=CacheService.getScriptCache();
  const cached=cs.get("Logger");
  const ts=Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MM|dd|HH:mm:ss")
  if(cached) {
    var v=Utilities.formatString('%s<br />[%s] - %s',cached,ts,s);   
  }else{
    var v=Utilities.formatString('[%s] - %s',ts,s);
  }
  cs.put("Logger",v,t);
  //allows logging without displaying.
  if(d) {
    const a='<br /><input type="button" value="Exit" onClick="google.script.host.close();" />';
    const b='<br /><input type="button" value="Exit" onClick="google.script.host.close();" /><br />';
    SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(b+v+a).setWidth(w).setHeight(h), 'My Logger');
  }
}

another simple approach for a custom logger (for sheets) is define this function to write to a 'log' sheet自定义记录器(用于工作表)的另一种简单方法是定义此函数以写入“日志”工作表

const log=(...items)=>{
  SpreadsheetApp.getActive().getSheetByName("log").appendRow(items)
}

and given a sheet called log, it will append arguments into cells of a new row, when called like并给定一个名为 log 的工作表,它会将参数附加到新行的单元格中,当调用时

log(1,0,2,"a","b", "etc")

also has the benefit of creating a log when called via web app execution (which seemed to not invoke Logger in a retrievable way, as far as I could tell).还具有在通过 Web 应用程序执行调用时创建日志的好处(据我所知,它似乎没有以可检索的方式调用 Logger)。

I have done some modification to @blueSkys Code我对@blueSkys 代码做了一些修改

Edit 1 : Added provision to get arrays as well编辑 1:添加了获取数组的规定

 const log=(...data)=>{
 for (i=0; i< data.length;i++){
  if(typeof data[i] === 'object'){
  data[i] = data[i].join("//");
};
};
data.unshift(new Date());
  SpreadsheetApp.openById('1E9s2vI21eRlcnoqaVAF4wCUm4Ojn2Lpv62TM6Xw17Z4').getSheetByName('log').appendRow(data);
}

Also Created Video for a detailed guide还为详细指南创建了视频

https://youtu.be/JQpbB5lR4eY https://youtu.be/JQpbB5lR4eY

Thanks谢谢

Thanks for the insight, the code below runs well in GAS:感谢您的洞察力,以下代码在 GAS 中运行良好:

 /** * @description Class: Logger helper, writes log information to tab sheet 'log' for the active spreadsheet * @tutorial https://stackoverflow.com/questions/24342748/why-does-console-log-say-undefined-and-then-the-correct-value * @param N/A * @return {Object} */ class ConsoleX { constructor(){ this.setSS('**** Log Initialized ****'); this.timeStart = 0; this.timerBase = {}; }; log(...data){ let dataY = ''; data.forEach(c => { dataY += c; }) this.setSS(`${[dataY]}`); Logger.log(`${[dataY]}`); }; setSS(data){ data = [data]; data.unshift(new Date()); SpreadsheetApp.getActive().getSheetByName(logFileName).appendRow(data); }; getLogs(dataX){ this.setSS(`${[dataX]}\\n${Logger.getLog()}`); } time(data = 'base1'){ let dateX = new Date(); this.timerBase[data] = dateX.getTime(); }; timeEnd(data = 'base1'){ let dateX = new Date(); this.log(`${data}: ${(dateX.getTime() - this.timerBase[data])/1000}(ms)`); }; clear() { SpreadsheetApp.getActive().getSheetByName(logFileName).clear({contentsOnly: true}); } };// END ConsoleX /** * @description Function: Test logging methods * @tutorial https://stackoverflow.com/questions/24342748/why-does-console-log-say-undefined-and-then-the-correct-value * @param AS PER METHOD * @return {undefine} */ function testLog(){ let testdata = {name:'hello', value:'world'}; Logger.log('From Logger: ', testdata); test = new ConsoleX(); test.time('**** Time') test.log('**** Standard Log: HELLO WORLD'); test.log('**** Templating Log: THIS IS GOOD: ', 10*10, ` and ${100/2}`); test.getLogs('**** Logger Historical Data: Looking Good :)'); test.getLogs(); test.timeEnd('**** Time') }; function testClear(){ test = new ConsoleX(); test.clear(); };

在此处输入图片说明

The following function has done the trick for me... quick and dirty:以下功能为我完成了诀窍......快速而肮脏:

    function logOut(message){
      var ui = SpreadsheetApp.getUi();
      var html  = HtmlService.createHtmlOutput('<h1>'+message+'</h1>');
      ui.showModalDialog(html,'Logs');
    }

It creates a pop up window showing what would otherwise be shown through Logger.log(message)它会创建一个弹出窗口,显示原本会通过 Logger.log(message) 显示的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM