Module console
Console access object
The console module is a core module that provides functions similar to the console object in the browser. It can output information to the console to facilitate debugging and output information.
The most commonly used method in the console module is log(), which prints any JavaScript value to the console, automatically adding newlines. In addition to the log() method, there are also info(), warn(), and error() methods, which are used to output information, warnings, and errors respectively. Their functions are basically the same as the log() method, except for the style displayed in the console. different.
The console module also provides the dir() method, which is used to output the properties and methods of an object to the console in a more readable form, making it easier to debug complex objects. In addition, there are time() and timeEnd() methods for timing code execution in the console and outputting the time difference.
In addition to the above commonly used methods, the console module also provides some other methods, such asassert(), notice(), trace(), etc., can facilitate debugging and information output in different situations.
The console module is a very practical module that can improve debugging efficiency during the development process and output various information conveniently and quickly.
static function
add
Add a console output system. Supported devices are console, syslog, and event. Up to 10 outputs can be added.
1static console.add(String type);
Call parameters:
- type: String, output device
By configuring the console, program output and system errors can be sent to different devices for collection of running environment information.
type is the configuration and is the device name string:
1console.add("console");
syslog is only valid on posix platform:
1console.add("syslog");
event is only valid on windows platform:
1console.add("event");
Add console output system. Supported devices are console, syslog, event and file. Up to 10 outputs can be added.
1static console.add(Object cfg);
Call parameters:
- cfg: Object, output configuration
By configuring the console, program output and system errors can be sent to different devices for collection of running environment information.
cfg can configure objects for a device:
1
2
3
4console.add({
type: "console",
levels: [console.INFO, console.ERROR] // optional, default is all levels
});
syslog is only valid on posix platform:
1
2
3
4console.add({
type: "syslog",
levels: [console.INFO, console.ERROR]
});
event is only valid on windows platform:
1
2
3
4console.add({
type: "event",
levels: [console.INFO, console.ERROR]
});
file log:
1
2
3
4
5
6
7console.add({
type: "file",
levels: [console.INFO, console.ERROR],
path: "path/to/file_%s.log", // specifies the log output file, can use %s to specify the insertion date location, or add at the end if not specified
split: "30m", // Optional values are "day", "hour", "minute", "####k", "####m", "####g", default is "1m"
count: 10 // option, selectable from 2 to 128, default is 128
});
Add console output systems in batches. Supported devices are console, syslog, event and file. Up to 10 outputs can be added.
1static console.add(Array cfg);
Call parameters:
- cfg: Array, output configuration array
By configuring the console, program output and system errors can be sent to different devices for collection of running environment information.
1
2
3
4console.add(["console", {
type: "syslog",
levels: [console.INFO, console.ERROR]
}]);
reset
Initialize to default settings and only output information to the console.
1static console.reset();
log
Record common log information, which is equivalent to info
1
2static console.log(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Record general level log information. Usually used to output non-error prompt information.
Record common log information, which is equivalent to info
1static console.log(...args);
Call parameters:
- args: ..., optional parameter list
Record general level log information. Usually used to output non-error prompt information.
debug
Record debug log information
1
2static console.debug(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Record debug log information. Usually used to output debugging information. unimportant.
Record debug log information
1static console.debug(...args);
Call parameters:
- args: ..., optional parameter list
Record debug log information. Usually used to output debugging information. unimportant.
info
Record common log information, which is equivalent to log
1
2static console.info(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Record general level log information. Usually used to output non-error prompt information.
Record common log information, which is equivalent to log
1static console.info(...args);
Call parameters:
- args: ..., optional parameter list
Record general level log information. Usually used to output non-error prompt information.
notice
Record warning log information
1
2static console.notice(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Record warning log information. Usually used to output informative debugging information. Generally important.
Record warning log information
1static console.notice(...args);
Call parameters:
- args: ..., optional parameter list
Record warning log information. Usually used to output informative debugging information. Generally important.
warn
Record warning log information
1
2static console.warn(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Record warning log information. Usually used to output warning debugging information. important.
Record warning log information
1static console.warn(...args);
Call parameters:
- args: ..., optional parameter list
Record warning log information. Usually used to output warning debugging information. important.
error
Record error log information
1
2static console.error(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Records information for error logs. Usually used to output error messages. Very important. System error messages will also be logged at this level.
Record error log information
1static console.error(...args);
Call parameters:
- args: ..., optional parameter list
Records information for error logs. Usually used to output error messages. Very important. System error messages will also be logged at this level.
crit
Record key error log information
1
2static console.crit(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Log information for critical errors. Usually used to output critical error information. Very important.
Record key error log information
1static console.crit(...args);
Call parameters:
- args: ..., optional parameter list
Log information for critical errors. Usually used to output critical error information. Very important.
alert
Record alert error log information
1
2static console.alert(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Records error log messages for alerts. Usually used to output alert error messages. Very important. is the highest level of information.
Record alert error log information
1static console.alert(...args);
Call parameters:
- args: ..., optional parameter list
Records error log messages for alerts. Usually used to output alert error messages. Very important. is the highest level of information.
trace
Output the current call stack
1
2static console.trace(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Output the current call stack through the log.
Output the current call stack
1static console.trace(...args);
Call parameters:
- args: ..., optional parameter list
Output the current call stack through the log.
dir
Output objects in JSON format
1
2static console.dir(Value obj,
Object options = {});
Call parameters:
- obj: Value, specifies the object to be processed
- options: Object, specify format control options
The following parameters are supported:
1
2
3
4
5
6
7
8
9{
"colors": false, // specify if output should be colorized, defaults to false
"depth": 2, // specify the max depth of the output, defaults to 2
"table": false, // specify if output should be a table, defaults to false
"encode_string": true, // specify if string should be encoded, defaults to true
"maxArrayLength": 100, // specify max number of array elements to show, set to 0 or negative to show no elements, defaults to 100
"maxStringLength": 10000, // specify max string length to output, set to 0 or negative to show no strings, defaults to 10000
"fields": [], // specify the fields to be displayed, defaults to all
}
table
Output objects in JSON format
1static console.table(Value obj);
Call parameters:
- obj: Value, given the object to be displayed
Output objects in JSON format
1
2static console.table(Value obj,
Array fields);
Call parameters:
- obj: Value, given the object to be displayed
- fields: Array, given the fields to be displayed
time
start a timer
1static console.time(String label = "time");
Call parameters:
- label: String, title, default is empty string.
timeElapse
Output the current timing value of the specified timer
1static console.timeElapse(String label = "time");
Call parameters:
- label: String, title, default is empty string.
timeEnd
End the specified timer and output the last time value
1static console.timeEnd(String label = "time");
Call parameters:
- label: String, title, default is empty string.
assert
Assertion test, if the test value is false, an error will be reported
1
2static console.assert(Value value,
String msg = "");
Call parameters:
- value: Value, the value to be tested
- msg: String, error message
Output formatted text to the console, the output content will not be recorded in the log system, the text will not automatically wrap after output, and can be output continuously.
1
2static console.print(String fmt,
...args);
Call parameters:
- fmt: String, format string
- args: ..., optional parameter list
Output formatted text to the console, the output content will not be recorded in the log system, the text will not automatically wrap after output, and can be output continuously.
1static console.print(...args);
Call parameters:
- args: ..., optional parameter list
moveTo
Move the console cursor to the specified position
1
2static console.moveTo(Integer row,
Integer column);
Call parameters:
- row: Integer, specifies the row coordinate of the new cursor
- column: Integer, specifies the column coordinates of the new cursor
hideCursor
Hide console cursor
1static console.hideCursor();
showCursor
Show console cursor
1static console.showCursor();
clear
clear console
1static console.clear();
readLine
Read user input from console
1static String console.readLine(String msg = "") async;
Call parameters:
- msg: String, prompt message
Return results:
- String, returns the information entered by the user
getpass
Read the password entered by the user from the console
1static String console.getpass(String msg = "") async;
Call parameters:
- msg: String, prompt message
Return results:
- String, returns the password entered by the user
static properties
loglevel
Integer, output level, used to filter output information, the default is NOTSET, all output. After the information is filtered, it will be output to each device set by add.
1static Integer console.loglevel;
width
Integer, query the number of characters per line of the terminal
1static readonly Integer console.width;
height
Integer, query the number of terminal lines
1static readonly Integer console.height;
constant
FATAL
loglevel level constant
1const console.FATAL = 0;
ALERT
loglevel level constant
1const console.ALERT = 1;
CRIT
loglevel level constant
1const console.CRIT = 2;
ERROR
loglevel level constant
1const console.ERROR = 3;
WARN
loglevel level constant
1const console.WARN = 4;
NOTICE
loglevel level constant
1const console.NOTICE = 5;
INFO
loglevel level constant
1const console.INFO = 6;
DEBUG
loglevel level constant
1const console.DEBUG = 7;
loglevel is only used for output. There is no line break after the information is output. File and syslog do not save this level information.
1const console.PRINT = 9;
NOTSET
loglevel level constant
1const console.NOTSET = 10;