console - Console
Stability: 2 - Stable
The console module provides a debugging console similar to those in web browsers. It is used to output debug information, intermediate results, etc. Some functions in this module can also be used as global functions, such as log and print.
console.show()
Show the console. This displays a floating console window (requires floating window permission).
console.hide()
Hide the floating console window.
console.clear()
Clear the console.
console.log([data][, ...args])#
data{any}...args{any}
Print to the console with a trailing newline. You can pass multiple arguments: the first is the main message, and the rest are substitution values similar to printf(3) (all arguments are passed to util.format()).
const count = 5;
console.log('count: %d', count);
// Prints: count: 5 to stdout
console.log('count:', count);
// Prints: count: 5 to stdoutSee util.format() for details.
This function can also be used as a global function.
console.verbose([data][, ...args])
data{any}...args{any}
Similar to console.log, but the output is shown in gray. Lower priority than log, suitable for informational output.
console.info([data][, ...args])
data{any}...args{any}
Similar to console.log, but the output is shown in green. Higher priority than log, suitable for important information.
console.warn([data][, ...args])
data{any}...args{any}
Similar to console.log, but the output is shown in blue. Higher priority than info, suitable for warnings.
console.error([data][, ...args])
data{any}...args{any}
Similar to console.log, but the output is shown in red. Higher priority than warn, suitable for errors.
console.assert(value, message)
value{any} Boolean value to assertmessage{string} Message to output whenvalueis false
Assertion. If value is false, outputs the error message and stops the script.
var a = 1 + 1;
console.assert(a == 2, "Addition failed");console.time([label])
[Added in v4.1.0]
label{String} Timer label (optional)
Start a timer to measure how long an operation takes. The timer is identified by a unique label. When calling console.timeEnd(), use the same label to stop the timer and print the duration in milliseconds. Starting a timer with the same label again overwrites the previous one.
console.timeEnd(label)
[Added in v4.1.0]
label{String} Timer label
Stop a timer previously started with console.time() and print the result. The timer is removed after console.timeEnd(). If no timer with the given label exists, it prints NaNms.
console.time('sum');
var sum = 0;
for(let i = 0; i < 100000; i++){
sum += i;
}
console.timeEnd('sum');
// Prints: sum: xxx msconsole.trace([data][, ...args])
[Added in v4.1.0]
data{any}...args{any}
Similar to console.log, but also prints a stack trace (current file, line number, etc.).
console.trace('Show me');
// Prints: (stack trace varies depending on call site)
// Show me
// at <test>:7console.input(data[, ...args])
data{any}...args{any}
Outputs like console.log, then shows an input box in the console and waits for input. After pressing confirm, the input string is evaluated with eval and returned.
On some devices, the input box may not show up. This is a known bug.
Example:
var n = console.input("Enter a number:");
// After input 123:
toast(n + 1);
// Shows 124console.rawInput(data[, ...args])
data{any}...args{any}
Outputs like console.log, then shows an input box in the console and waits for input. After pressing confirm, the input string is returned directly.
On some devices, the input box may not show up. This is a known bug.
Example:
var n = console.rawInput("Enter a number:");
// After input 123:
toast(n + 1);
// Shows 1231console.setSize(w, h)
w{number} Widthh{number} Height
Set console size in pixels.
console.show();
// Set console size to a quarter of the screen
console.setSize(device.width / 2, device.height / 2);console.setPosition(x, y)
x{number} X coordinatey{number} Y coordinate
Set console position in pixels.
console.show();
console.setPosition(100, 100);console.setGlobalLogConfig(config)
[Added in v4.1.0]
config{Object} Log configuration. Options include:file{string} Log file path. Logs will be written to this file.maxFileSize{number} Max file size in bytes. Default 512 * 1024 (512KB)rootLevel{string} Minimum log level to write. Default"ALL". Can be"OFF","DEBUG","INFO","WARN","ERROR","FATAL", etc.maxBackupSize{number} Max number of backup log files. Default 5.filePattern{string} Log pattern format. See PatternLayout
Configure log saving path and options. For example, save logs to "/sdcard/1.txt":
console.setGlobalLogConfig({
"file": "/sdcard/1.txt"
});Note: this affects logging for all scripts.
print(text)
text{string} | {Object} Content to print to the console
Equivalent to log(text).
