【问题标题】:BetterLog duplicates output for each message to Google App Script console two timesBetterLog 将每条消息的输出复制到 Google App Script 控制台两次
【发布时间】:2022-06-13 20:00:29
【问题描述】:

BetterLog Apps Script library 将每条消息打印到Google Apps Script console 两次。 是否可以以某种方式配置它或者它是一个错误?

我使用the library's page推荐的标准配置:

// Add one line to use BetterLog
Logger = BetterLog.useSpreadsheet('your-spreadsheet-key-goes-here'); 

//Now you can log and it will also log to the spreadsheet
Logger.log("That's all you need to do");

带有重复消息的控制台日志示例:

11:23:10 AM Info 2022-06-13 11:23:10:821 +0300 001169 INFO doPost called
11:23:10 AM Info doPost called
11:23:11 AM Info 2022-06-13 11:23:11:289 +0300 001637 INFO doPost - contents: {"message":{"chat":{"id":123},"text":"/test"}}; runFromChatId: 123; text: /test
11:23:11 AM Info doPost - contents: {"message":{"chat":{"id":123},"text":"/test"}}; runFromChatId: 123; text: /test
11:23:11 AM Info 2022-06-13 11:23:11:599 +0300 001947 INFO doPost - not yet validated runFromChatId: 123
11:23:11 AM Info doPost - not yet validated runFromChatId: 123
11:23:11 AM Info 2022-06-13 11:23:11:893 +0300 002241 INFO doPost - authFailedAmount: 0
11:23:11 AM Info doPost - authFailedAmount: 0
...

【问题讨论】:

    标签: javascript google-apps-script logging


    【解决方案1】:

    大约 1 年前没有这样的问题:消息只打印到控制台一次。从那时起,库或App Script 引擎中发生了一些变化。

    现在这似乎是一个错误。 我为这个库创建了相应的问题: BetterLog duplicates output for each message to console two times


    作为解决方法,您可以执行以下操作:

    初始配置:

    Logger = BetterLog.useSpreadsheet(SPREADSHEET_ID);
    Logger.console = undefined; //Do not print messages to console second time.
    //To print to console Logger.nativeLogger_ destination will be used.
    

    用法:

    Logger.log("This message will go to console only one time");
    

    Work around for issue "BetterLog duplicates output for each message to console two times"

    此解决方法禁用打印到console 目标。记录器仍将通过默认启用nativeLogger_ 目的地的方式打印到控制台。


    作为第二个解决方法,您可以启用console 目标并禁用BetterLog.nativeLogger_ 目标。消息仍将仅打印一次。但是此解决方案不会将消息的长时间戳打印到控制台。这可能更方便。

    现在你可以使用BetterLog而不是作为外部库,而是将它的源代码复制到你的项目中,并自己为这个库添加小修复。以后如果图书馆修好了,就不需要了。

    要在本地使用BetterLog,您需要创建新文件,例如名称为“LocalBetterLog”,然后将this file 中的代码粘贴到那里。

    应用修复。而不是:

      //default console logging (built in with Google Apps Script's View > Logs...)
      nativeLogger_.log(convertUsingDefaultPatternLayout_(msg, level));
    

    用途:

    //Anton Samokat fix: messages are printed to console twicely. 
    //Added check for nativeLogger_ is not null and not undefined 
    //in order to be able to set nativeLogger_ as null 
    //and in such a way to skip printing messages to it.
    if(nativeLogger_) { 
      //default console logging (built in with Google Apps Script's View > Logs...)
      nativeLogger_.log(convertUsingDefaultPatternLayout_(msg, level));
    }
    

    然后按以下方式启动和配置记录器:

    //log config start
    //Logger = BetterLog.useSpreadsheet(SPREADSHEET_ID); //Use BetterLog library when it is fixed
    const Logger = useSpreadsheet(SPREADSHEET_ID); // For now use local BetterLog logger
    //To call function from another file you need just to call
    //it by name, no imports, no file names specification.
    //Logger.console = undefined; //do not print messages to console
    Logger.nativeLogger_ = undefined; //do not print messages to console second time
    //Logger.sheet_ = undefined; //do not print messages to sheet (this particular set 
    //can be placed in initTestsEnvironment() function for using it in local tests runs 
    //in order to skip printing to the sheet for not spamming it during the tests)
    //log config end
    

    在所有情况下照常使用:

    Logger.log("This message will go to console only one time");
    

    【讨论】:

      猜你喜欢
      • 2014-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 2015-01-05
      • 1970-01-01
      • 2017-08-22
      • 2022-01-26
      相关资源
      最近更新 更多