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:

  1. It does not support parameters, and object printing requires string concatenation.
  2. 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 the console.log() method in Javascript, which is also a syntactic sugar borrowed from JS.

Usage

  1. Console.log This method is basically equivalent to System.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.
  1. Console.error This method is basically equivalent to System.err.println, but it supports string template syntax similar to Slf4j, and automatically converts objects (including arrays) to string form.