-
Notifications
You must be signed in to change notification settings - Fork 50
Description
BetterLog duplicates output to console two times . This happens if the logger is run from Apps Script development environment. This behavior probably appeared after update in Apps Script engine about 1 year ago. Before this update BetterLog outputted messages to console as expected only one time (without doubling them).
Example of console log with messages duplications:
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
...
Config for such output is the following:
const Logger = BetterLog.useSpreadsheet(SPREADSHEET_ID);
Logger.log("doPost called");
...
Fix
//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));
}
There are already such checks for console:
if (typeof console !== "undefined" && typeof console.time !== "undefined") {
switch(level) {
case Level.INFO:
console.info(msg);
break;
case Level.SEVERE:
console.error(msg);
break;
case Level.WARNING:
console.warn(msg);
break;
default:
console.info(msg);
}
}
And for sheet destinations:
if (sheet_) {
logToSheet_(msg, level);
}
This checks give possibility to turn off output destinations by the following logger initial configuration:
//log config start
//Logger = BetterLog.useSpreadsheet(SPREADSHEET_ID); //use BetterLog library
const Logger = useSpreadsheet(SPREADSHEET_ID); // use local BetterLog logger
//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
So please add check on null or undefined for nativeLogger_ var and publish changes in Google Apps BetterLog library.
May be it will be good to make API for the configurations above to be able to use them more simpler and transparently.
By default in BetterLog I prefer to use console destination, not nativeLogger_ since its default timestamp took much space and there are already timestamps (but without seconds and milliseconds) on the left side of Apps Script console.
Thanks, Anton.