DEV Community

Zaira
Zaira

Posted on

Don't use System, better use Logger

Throughout my career, I've relied on System.out.println() to print application logs to the terminal. However, at one point, a fellow Java Developer Engineer suggested using the Logger class instead.

System

This method is handy when developing a basic application and needing a quick way to log messages.



// The main, basic, and simplest way to log
System.out.println("Something");


Enter fullscreen mode Exit fullscreen mode

Logger

This approach becomes invaluable when working on larger applications that require more sophisticated logging.



import java.util.logging.Logger;

class TestLogging {

  // Creating a new Logger object
  private static Logger log = Logger.getLogger(Logger.class.getName());

  public static void main(String[] args) {

    System.out.println("Hello World");
    log.info("Hello World!");

  }
}


Enter fullscreen mode Exit fullscreen mode

This is the output generated when running the application:

Output generated when running the application

That's all!

Top comments (1)

Collapse
 
reidecarvalho profile image
Reinaldo de Carvalho

About static initializers, the Java channel issues an alert:
youtube.com/shorts/52wIam7kkL4