Logs - guiled/LRE GitHub Wiki
LRE Logs
Introduction
LRE provides some log functions you can freely use. They of course all based on Let's role native log()
function.
All logs from LRE are prefixed with [LRE]
and then the
Log methods and levels
LRE provides many levels of logs : "none", "error", "warn", "info", "profile", "trace", "all". By default, the LRE logs are disabled (log level : none). You can define the log level you want (in order to know what LRE does) by using the following code :
lre.setLogLevel("warn");
With this code, all warning and error messages will appear in the console. Lower priority logs will be dismissed. If you choose to use the debug build, take care not to use the trace log level unless necessary because it can affect your script speed.
multiple arguments
You can give multiple arguments to the log functions, each will be pushed to the console. Moreover, each given argument will be logged as string first and, if it is not a scalar type, will be directly logged (in order to debug and inspect the variable)
lre.log("sheet", sheet, sheet.getSheetId());
This will log three lines in the console :
[LRE] sheet
[LRE] [object Object]
Object { cleanCmpData: v(), lreType: v(), sheet: v(), getSheetType: v(), getVariable: v(), prompt: v(), id: v(), getSheetAlphaId: v(), realId: v(), getSheetId: v(), … }
[LRE] 89206
You can notice here the 3 different logs and an additional one that dump the variable sheet
Profile code
LRE provides a way to profile (read "measure the execution time").
You can use LRE.push("Name of the profile")
and LRE.pop()
to find in the log the start and end log with the elapsed time.
If you use the lre.log methods, the messages will be indented during this profiling
The following code
lre.push("My measure"); // start profiling
doSomethingLong();
lre.log("a log in a profiling")
doSomethingElseLong();
lre.log("another warning log in a profiling")
lre.pop(); // stop profiling
Will generate the following logs
[LRE][TRC] My measure
[LRE][LOG] a log in a profiling
[LRE][WRN] another warning log in a profiling
[LRE][TRC] My measure (166ms)
Note that profiling is only logged when you set the log level at trace
with lre.setLogLevel("trace");
The title used to start the profiling will be automatically re-used at the profiling stop. Never forget to stop profiling (remind to handle errors by stopping the profiler)
stringify
LRE provides the useful stringify
function to debug variables in the logs that is largely inspired from JSON.stringify which is not available in Let's Role.
Use it like this
lre.log(stringify(Tables.get("data").get("1")));
This code shows in logs the content cleanly formatted of a Table row
[LRE] {
"id": 1,
"lbl": "Un",
"lbl2": "One"
}
Error handlings
One of the most useful feature provided by LRE is the error handling that greatly helps the system author to understand an error. It can give you the code line that raised the error and the context of the code run (sheet init, event, …).
The following code
const option = sheet.get("option");
thisTriggersAnError = null;
option.on("click:activation", function () {
thisTriggersAnError(); // <-- null can not be call as a function
});
With the full log level it will output in logs the following lines
[LRE][TRC] Run events option:click
[LRE][TRC] Run handler option:click:activation
[LRE][ERR] [Event:option:click:activation] Unhandled error : T is null at line 4242
[LRE][TRC] Run handler option:click:activation (2ms)
[LRE][TRC] Run events option:click (4ms)
You can see a label describing the context [Event:option:click:activation]
which means it is an error raised during an click
event, on the component option
which was labeled activation
(see the LRE aliased event notation)