I know the best way to log something in a real Java project is through a Logging Framework like: Log4J2, Logback, etc. However, I have a personal project which is simple enough to use the good old System.out.print()
.
So, this project requires printing information in the console, reading input from the user, clearing the screen and printing the updated information until a certain condition is met.
How should I design the use of these print methods?
Should I invoke a print() method for every line of information (Example 1) or should I build a String with all the information and invoke print() once to display it (Example 2)?
Example 1
System.out.println("Month: " + month);
System.out.println("Day of the week: " + dayOfTheWeek);
System.out.println("Weather: " + weather);
System.out.println("Status [" + status + "]");
##Example 2
StringBuilder stringBuilder = new StringBuilder();
stringBuilder
.append("Month: " + month)
.append("Day of the week: " + dayOfTheWeek)
.append("Weather: " + weather)
.append("Status [" + status + "]");
System.out.println(stringBuilder.toString());
I tried searching this specific question here and on Google but found none. I expect to use the best code practice and with less resources required.
Rogério Ferreira de Souza is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.