Skip to main content

Command Palette

Search for a command to run...

Logging in Java

Updated
1 min readView as Markdown

Logging is an essential part of software development that helps track important events, debug errors, and analyze application behavior. Instead of relying on System.out.println(), which is not efficient for large-scale applications, developers use logging frameworks to capture and manage log messages effectively.

Why Use Logging Instead of System.out.println()?

  1. Not Scalable

  2. No Log Levels

  3. Performance Issues

  4. No External Storage

Example Without Logging


public class WithoutLogging {
    public static void main(String[] args) {
        try {
            int result = 10 / 0; 
        } catch (ArithmeticException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Example With Basic Logging


import java.util.logging.Logger;

public class BasicLogging {
    private static final Logger logger = Logger.getLogger(BasicLogging.class.getName());

    public static void main(String[] args) {
        try {
            int result = 10 / 0;
        } catch (ArithmeticException e) {
            logger.severe("Exception occurred: " + e.getMessage());
        }
    }
}