Java Logging
What is Logging In Java?
Java Logging is a mechanism used to capture important messages generated by a Java application. It provides a way to track application behavior, diagnose issues, and monitor performance.
The Java Logging API is included in the Java Development Kit (JDK) and provides several built-in logging levels and handlers for directing log output to various destinations. Developers can also customize logging behavior through configuration files or programmatically.
To understand the Java Generics, Read the Complete Article.
Java Logging Components
The Java Logging API consists of several key components:
- Logger: A Logger is the main class used to generate log messages. It can be configured to produce logs for different parts of an application or for different levels of severity.
- Handler: A Handler is responsible for directing log messages to a particular destination, such as a file, console, or network socket.
- Formatter: A Formatter specifies the format of the log messages generated by a Logger. It can be customized to include information such as timestamps, thread names, and logging levels.
- Level: A Level is used to categorize log messages according to their severity. The Java Logging API defines several levels, including SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST.
- Filter: A Filter can be used to further refine the types of log messages that are produced by a Logger. It can be customized to include or exclude messages based on their content, source, or severity level.
Advantages of Java Logging:
Log Manager
LogManager is a class in the Java Logging API that manages the logging configuration for a Java application. It provides a central point for configuring logging behavior, including specifying the logging levels, output destinations, and formatting options for log messages.
Example 1: How logging works in java
import java.util.logging.Level; import java.util.logging.Logger; public class Main { private static final Logger LOGGER = Logger.getLogger(Main.class.getName()); public static void main(String[] args) { LOGGER.info("Starting the LoggingExample program"); try { int result = 1 / 0; } catch (Exception e) { LOGGER.log(Level.SEVERE, "An error occurred", e); } LOGGER.info("Finishing the LoggingExample program"); } }
Output
Apr 14, 2023 7:03:53 AM Main main INFO: Starting the LoggingExample program Apr 14, 2023 7:03:53 AM Main main SEVERE: An error occurred java.lang.ArithmeticException: / by zero at Main.main(Main.java:11) Apr 14, 2023 7:03:53 AM Main main INFO: Finishing the LoggingExample program
Example 2 : Logging the message
import java.io.IOException; import java.util.logging.*; // CustomLoggingExample public class Main { private static final Logger LOGGER = Logger.getLogger (Main.class.getName ()); public static void main (String[]args) throws IOException { // Create a new console handler ConsoleHandler consoleHandler = new ConsoleHandler (); // Set the log level for the console handler consoleHandler.setLevel (Level.ALL); // Add the console handler to the logger LOGGER.addHandler (consoleHandler); // Set the log level for the logger LOGGER.setLevel (Level.INFO); // Log some messages with different levels LOGGER.severe ("This is a severe error"); LOGGER.warning ("This is a warning"); LOGGER.info ("This is an informational message"); LOGGER.config ("This is a configuration message"); LOGGER.fine ("This is a fine message"); LOGGER.finer ("This is a finer message"); LOGGER.finest ("This is a finest message"); } }
Output :
Apr 14, 2023 7:26:32 AM Main main SEVERE: This is a severe error Apr 14, 2023 7:26:32 AM Main main SEVERE: This is a severe error Apr 14, 2023 7:26:32 AM Main main WARNING: This is a warning Apr 14, 2023 7:26:32 AM Main main WARNING: This is a warning Apr 14, 2023 7:26:32 AM Main main INFO: This is an informational message Apr 14, 2023 7:26:32 AM Main main INFO: This is an informational message
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment