1. Logging Using ConsoleHandler
This program demonstrates the use of ConsoleHandler to display log messages on the console. The handler is attached to a logger and configured to display all log levels.
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ConsoleHandlerExample {
private static final Logger logger =
Logger.getLogger(ConsoleHandlerExample.class.getName());
public static void main(String[] args) {
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.ALL);
logger.addHandler(handler);
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
logger.info("Application Started");
logger.warning("Warning Message");
logger.severe("Severe Error");
}
}Output:
Aug 07, 2026 12:14:03 PM ConsoleHandlerExample main
INFO: Application Started
Aug 07, 2026 12:14:03 PM ConsoleHandlerExample main
WARNING: Warning Message
Aug 07, 2026 12:14:03 PM ConsoleHandlerExample main
SEVERE: Severe ErrorCode language: CSS (css)
2. Logging Messages to a File Using FileHandler
This program demonstrates the use of FileHandler to store log messages in a file. The log messages are formatted using SimpleFormatter.
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class FileHandlerExample {
private static final Logger logger =
Logger.getLogger(FileHandlerExample.class.getName());
public static void main(String[] args) throws Exception {
FileHandler handler = new FileHandler("application.log");
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
logger.info("Program Started");
logger.warning("Low Memory");
logger.severe("Unexpected Error");
System.out.println("Log file created successfully.");
}
}Output:
Log file created successfully.
Content of application.log:
Aug 07, 2026 12:17:42 PM FileHandlerExample main
INFO: Program Started
Aug 07, 2026 12:17:42 PM FileHandlerExample main
WARNING: Low Memory
Aug 07, 2026 12:17:42 PM FileHandlerExample main
SEVERE: Unexpected ErrorCode language: CSS (css)
3. Logging Using StreamHandler
This program demonstrates the use of StreamHandler to write log messages to an output stream. The handler writes formatted log records to the console.
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.StreamHandler;
public class StreamHandlerExample {
private static final Logger logger =
Logger.getLogger(StreamHandlerExample.class.getName());
public static void main(String[] args) {
StreamHandler handler =
new StreamHandler(System.out, new SimpleFormatter());
handler.setLevel(Level.ALL);
logger.addHandler(handler);
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
logger.info("Information Message");
logger.warning("Warning Message");
handler.flush();
}
}Output:
Aug 07, 2026 12:20:11 PM StreamHandlerExample main
INFO: Information Message
Aug 07, 2026 12:20:11 PM StreamHandlerExample main
WARNING: Warning MessageCode language: CSS (css)
4. Set Logging Level for a Handler
This program demonstrates how to assign a logging level to a handler. Only messages matching the specified level are displayed.
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
public class HandlerLevelExample {
private static final Logger logger =
Logger.getLogger(HandlerLevelExample.class.getName());
public static void main(String[] args) {
ConsoleHandler handler = new ConsoleHandler();
handler.setLevel(Level.WARNING);
logger.addHandler(handler);
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
logger.info("Information Message");
logger.warning("Warning Message");
logger.severe("Severe Error");
}
}Output:
Aug 07, 2026 12:22:30 PM HandlerLevelExample main
WARNING: Warning Message
Aug 07, 2026 12:22:30 PM HandlerLevelExample main
SEVERE: Severe ErrorCode language: CSS (css)
5. Multiple Handlers in a Logger
This program demonstrates the use of multiple handlers with a single logger. The same log messages are displayed on the console and stored in a log file.
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class MultipleHandlersExample {
private static final Logger logger =
Logger.getLogger(MultipleHandlersExample.class.getName());
public static void main(String[] args) throws Exception {
ConsoleHandler consoleHandler = new ConsoleHandler();
FileHandler fileHandler =
new FileHandler("multiple.log");
fileHandler.setFormatter(new SimpleFormatter());
logger.addHandler(consoleHandler);
logger.addHandler(fileHandler);
logger.setUseParentHandlers(false);
logger.info("Program Started");
logger.warning("Disk Space is Low");
logger.severe("Program Terminated");
System.out.println("Logging completed.");
}
}Output:
Aug 07, 2026 12:24:09 PM MultipleHandlersExample main
INFO: Program Started
Aug 07, 2026 12:24:10 PM MultipleHandlersExample main
WARNING: Disk Space is Low
Aug 07, 2026 12:24:10 PM MultipleHandlersExample main
SEVERE: Program Terminated
Logging completed.
Content of multiple.log:
Aug 07, 2026 12:24:09 PM MultipleHandlersExample main
INFO: Program Started
Aug 07, 2026 12:24:10 PM MultipleHandlersExample main
WARNING: Disk Space is Low
Aug 07, 2026 12:24:10 PM MultipleHandlersExample main
SEVERE: Program TerminatedCode language: CSS (css)
6. Logging Using SocketHandler
This program demonstrates the use of SocketHandler to send log records over a network socket. The log messages are transmitted to a remote host and port where a logging server can receive them.
import java.util.logging.Logger;
import java.util.logging.SocketHandler;
import java.util.logging.SimpleFormatter;
public class SocketHandlerExample {
private static final Logger logger =
Logger.getLogger(SocketHandlerExample.class.getName());
public static void main(String[] args) {
try {
SocketHandler handler =
new SocketHandler("localhost", 5000);
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
logger.setUseParentHandlers(false);
logger.info("Application Started");
logger.warning("Warning Message");
logger.severe("Critical Error");
handler.close();
System.out.println("Logs sent to socket successfully.");
} catch (Exception e) {
System.out.println("Unable to connect to logging server.");
System.out.println(e.getMessage());
}
}
}Output:
Logs sent to socket successfully.
