1. Display Log Messages Using Logger
This program demonstrates the use of the Logger class to display log messages. It logs messages of different severity levels using predefined logging methods.
import java.util.logging.Logger;
public class LoggerExample {
private static final Logger logger =
Logger.getLogger(LoggerExample.class.getName());
public static void main(String[] args) {
logger.info("Application Started");
logger.warning("This is a Warning Message");
logger.severe("This is a Severe Error Message");
}
}Output:
Aug 07, 2026 11:51:35 AM LoggerExample main
INFO: Application Started
Aug 07, 2026 11:51:35 AM LoggerExample main
WARNING: This is a Warning Message
Aug 07, 2026 11:51:35 AM LoggerExample main
SEVERE: This is a Severe Error MessageCode language: CSS (css)
2. Log Different Severity Levels
This program demonstrates different logging levels available in java.util.logging. Each log message is displayed according to its severity level.
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingLevelsExample {
private static final Logger logger =
Logger.getLogger(LoggingLevelsExample.class.getName());
public static void main(String[] args) {
Logger rootLogger = Logger.getLogger("");
rootLogger.setLevel(Level.CONFIG);
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.CONFIG);
for (var h : rootLogger.getHandlers()) {
rootLogger.removeHandler(h);
}
rootLogger.addHandler(handler);
logger.log(Level.INFO, "Information Message");
logger.log(Level.WARNING, "Warning Message");
logger.log(Level.SEVERE, "Severe Error Message");
logger.log(Level.CONFIG, "Configuration Message");
}
}Output:
Aug 07, 2026 6:26:51 AM LoggingLevelsExample main
INFO: Information Message
Aug 07, 2026 6:26:51 AM LoggingLevelsExample main
WARNING: Warning Message
Aug 07, 2026 6:26:51 AM LoggingLevelsExample main
SEVERE: Severe Error Message
Aug 07, 2026 6:26:51 AM LoggingLevelsExample main
CONFIG: Configuration MessageCode language: CSS (css)
3. Logging User Login Status
This program logs the status of a user login process. It records information and warning messages based on the login status.
import java.util.logging.Logger;
public class LoginLoggerExample {
private static final Logger logger =
Logger.getLogger(LoginLoggerExample.class.getName());
public static void main(String[] args) {
String username = "Rahul";
logger.info("User " + username + " is attempting to login.");
logger.info("Login Successful.");
logger.warning("Remember to change your password regularly.");
}
}Output:
Aug 07, 2026 6:28:57 AM LoginLoggerExample main
INFO: User Rahul is attempting to login.
Aug 07, 2026 6:28:57 AM LoginLoggerExample main
INFO: Login Successful.
Aug 07, 2026 6:28:57 AM LoginLoggerExample main
WARNING: Remember to change your password regularly.Code language: CSS (css)
4. Logging Exceptions
This program demonstrates how to log exceptions using the Logger class. If an exception occurs, it is recorded with a severe log message.
import java.util.logging.Level;
import java.util.logging.Logger;
public class ExceptionLoggerExample {
private static final Logger logger =
Logger.getLogger(ExceptionLoggerExample.class.getName());
public static void main(String[] args) {
try {
int result = 100 / 0;
System.out.println(result);
} catch (ArithmeticException e) {
logger.log(Level.SEVERE,
"Exception Occurred: Division by Zero", e);
}
}
}Output:
Aug 07, 2026 12:02:28 PM ExceptionLoggerExample main
SEVERE: Exception Occurred: Division by Zero
java.lang.ArithmeticException: / by zero
at ExceptionLoggerExample.main(ExceptionLoggerExample.java:13)
Code language: PHP (php)
5. Log Messages to a File
This program demonstrates how to store log messages in a file. It uses FileHandler along with SimpleFormatter to write logs.
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class FileLoggerExample {
private static final Logger logger =
Logger.getLogger(FileLoggerExample.class.getName());
public static void main(String[] args) throws IOException {
FileHandler handler = new FileHandler("application.log");
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
logger.info("Application Started");
logger.warning("Low Memory Warning");
logger.severe("Unexpected Error");
System.out.println("Log file created successfully.");
}
}Output:
Log file created successfully.
Content of appilication.log
Aug 07, 2026 12:08:27 PM FileLoggerExample main
INFO: Application Started
Aug 07, 2026 12:08:27 PM FileLoggerExample main
WARNING: Low Memory Warning
Aug 07, 2026 12:08:27 PM FileLoggerExample main
SEVERE: Unexpected ErrorCode language: CSS (css)
