← Back to Chapters

Java Logging Basics

? Java Logging Basics

? Quick Overview

Logging in Advance Java is used to record application events such as errors, warnings, and informational messages. It helps developers monitor application behavior and troubleshoot issues without using System.out.println().

? Key Concepts

  • Logging captures runtime information
  • Different log levels control message importance
  • Logs can be written to console or files
  • Java provides built-in logging via java.util.logging

? Syntax & Theory

Java’s built-in logging framework uses the Logger class. Each logger is identified by a name, usually the class name. Log levels include:

  • SEVERE
  • WARNING
  • INFO
  • CONFIG
  • FINE

? Code Example

? View Code Example
// Import Logger class for logging support
import java.util.logging.Logger;

public class LoggingDemo {

// Create a logger instance for this class
private static final Logger logger = Logger.getLogger(LoggingDemo.class.getName());

public static void main(String[] args) {

// Log an informational message
logger.info("Application started successfully");

// Log a warning message
logger.warning("This is a warning log message");

// Log a severe error message
logger.severe("A severe error occurred");

}
}

? Live Output / Explanation

When this program runs, log messages are printed to the console with timestamps and log levels. Only messages of level INFO and above are shown by default.

? Interactive Log Simulator

Click the buttons below to act as the Logger and generate simulated console output.

System Console ready... waiting for logs.

✅ Tips & Best Practices

  • Use logging instead of print statements
  • Choose appropriate log levels
  • Use class name as logger name
  • Avoid logging sensitive data

? Try It Yourself

  • Create a logger in another class
  • Experiment with different log levels
  • Disable INFO logs and observe output