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

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay