Console
Origin
In coding, we often need to debug and output some information. Apart from printing logs, the most commonly used methods are System.out
and System.err
.
For example, if we want to print “Hello World”, we can write:
System.out.println("Hello World");
However, System.out.println
cannot meet the diverse printing needs. For example:
- It does not support parameters, and object printing requires string concatenation.
- It cannot directly print arrays and needs to manually call
Arrays.toString
.
Considering the above issues, I encapsulated the Console
object.
The usage of the
Console
object is more similar to theconsole.log()
method in Javascript, which is also a syntactic sugar borrowed from JS.
Usage
Console.log
This method is basically equivalent toSystem.out.println
, but it supports string template syntax similar to Slf4j, and automatically converts objects (including arrays) to string form.
String[] a = {"abc", "bcd", "def"};
Console.log(a); // Output on the console: [abc, bcd, def]
Console.log("This is Console log for {}.", "test");
// Output on the console: This is Console log for test.
Console.error
This method is basically equivalent toSystem.err.println
, but it supports string template syntax similar to Slf4j, and automatically converts objects (including arrays) to string form.