System.out.println vs Loggers in Java: A Comprehensive Guide

Naveen Metta
4 min readMay 16, 2024
credit goes the owner : https://jenkov.com/tutorials/java-logging/overview.html
source : jenkov.com

When developing Java applications, effective logging is crucial for debugging and maintaining code. Two common approaches are using System.out.println and utilizing logging frameworks like Log4j, SLF4J, or java.util.logging. This article explores these two methods, comparing their use cases, advantages, and disadvantages with detailed explanations and ample code examples.

System.out.println: The Basics

System.out.println is a method provided by the PrintStream class in Java, used to print messages to the console.

Breakdown of System.out.println

  • System: A final class in the java.lang package that provides access to system-level resources.
  • out: A static final member of the System class of type PrintStream.
  • println: A method of PrintStream that prints a message followed by a newline.

Code Example: Using System.out.println

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

This simple example prints “Hello, World!” to the console.

Advantages of System.out.println

  1. Simplicity: Easy to use, requiring no additional setup.
  2. Immediate Feedback: Useful for quick debugging during development.

Disadvantages of System.out.println

  1. Performance: Inefficient for production due to synchronization overhead.
  2. Flexibility: Lacks configurability, making it hard to control log levels and outputs.
  3. Scalability: Not suitable for large applications where complex logging configurations are required.

Loggers: The Advanced Approach

Loggers provide a more robust and flexible way to handle logging in Java applications. They allow for different levels of logging, multiple output destinations, and better performance.

Popular Logging Frameworks

  1. Log4j: A widely-used logging framework providing extensive logging capabilities.
  2. SLF4J: A facade for various logging frameworks, allowing you to switch logging frameworks without changing your code.
  3. java.util.logging (JUL): A built-in logging framework in the Java standard library.

Breakdown of a Logger

  • Logger: An object responsible for logging messages.
  • Log Level: Specifies the severity of the log message (e.g., DEBUG, INFO, WARN, ERROR).
  • Appender: Specifies where the log messages should go (e.g., console, file).
  • Layout: Defines the format of the log messages.

Code Example: Using Log4j

  1. Add Log4j Dependency
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.14.1</version>
</dependency>
  1. Create log4j2.xml Configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
  1. Logger Usage Example
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4jExample {
private static final Logger logger = LogManager.getLogger(Log4jExample.class);

public static void main(String[] args) {
logger.debug("Debug message");
logger.info("Info message");
logger.warn("Warn message");
logger.error("Error message");
}
}

Advantages of Using Loggers

  1. Performance: More efficient, with asynchronous logging options to reduce performance overhead.
  2. Flexibility: Configurable log levels and multiple output destinations.
  3. Maintenance: Easier to manage and change logging behavior without altering the source code.

Disadvantages of Using Loggers

  1. Complexity: Requires initial setup and configuration.
  2. Dependencies: May introduce additional dependencies into the project.

Comparing System.out.println and Loggers

Use Case Scenarios

  • System.out.println: Best for simple, quick debugging during initial development stages.
  • Loggers: Ideal for production environments, complex applications, and when detailed logging is required.

Performance Comparison

  • System.out.println: Synchronized method calls can slow down applications, especially with high log volume.
  • Loggers: Can be configured for asynchronous logging, significantly reducing the impact on performance.

Configuration and Flexibility

  • System.out.println: No configuration options.
  • Loggers: Highly configurable through XML, JSON, or properties files.

Example: Switching from System.out.println to Log4j

Consider the following System.out.println usage:

public class Calculator {
public static void main(String[] args) {
System.out.println("Starting calculation...");
int result = add(5, 3);
System.out.println("Result: " + result);
System.out.println("Calculation completed.");
}

private static int add(int a, int b) {
return a + b;
}
}

Switching to Log4j:

  1. Add Log4j Dependency and Configuration
  2. Update Code to Use Log4j
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Calculator {
private static final Logger logger = LogManager.getLogger(Calculator.class);

public static void main(String[] args) {
logger.info("Starting calculation...");
int result = add(5, 3);
logger.info("Result: {}", result);
logger.info("Calculation completed.");
}

private static int add(int a, int b) {
return a + b;
}
}

Handling Different Log Levels

With System.out.println, all messages are printed to the console with no distinction in severity. Loggers, however, support various log levels:

  • DEBUG: Detailed information for debugging.
  • INFO: General information about application progress.
  • WARN: Potentially harmful situations.
  • ERROR: Error events that might still allow the application to continue running.
  • FATAL: Severe error events leading to application termination.

Example: Logging with Different Levels

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Application {
private static final Logger logger = LogManager.getLogger(Application.class);

public static void main(String[] args) {
try {
logger.debug("Debugging information");
logger.info("Application started");
performTask();
logger.info("Application finished successfully");
} catch (Exception e) {
logger.error("An error occurred", e);
}
}

private static void performTask() throws Exception {
logger.warn("This is a warning message");
if (Math.random() > 0.5) {
throw new Exception("Simulated error");
}
}
}

Conclusion

In conclusion, System.out.println and loggers serve different purposes in Java applications. While System.out.println is suitable for simple debugging tasks, loggers provide a comprehensive and scalable logging solution for production environments. By utilizing logging frameworks like Log4j, developers can benefit from enhanced performance, configurability, and maintainability, making it the preferred choice for robust Java applications.

When deciding between the two, consider the complexity and requirements of your project. For small-scale projects or initial development stages, System.out.println may suffice. However, for larger, production-level applications, investing time in setting up a proper logging framework is highly recommended.

--

--

Naveen Metta

I'm a Full Stack Developer with 2.5 years of experience. feel free to reach out for any help : mettanaveen701@gmail.com