In Java, logging is an important feature that helps developers to trace out the errors. Java is the programming language that comes with the logging approach. It provides a Logging API that was introduced in Java 1.4 version. It provides the ability to capture the log file. In this section, we are going to deep dive into the Java Logger API. Also, we will cover logging level, components, Logging handlers or appenders, logging formatters or layouts, Java Logger class,
What is logging in Java?
In Java, Logging is an API that provides the ability to trace out the errors of the applications. When an application generates the logging call, the Logger records the event in the LogRecord. After that, it sends to the corresponding handlers or appenders. Before sending it to the console or file, the appenders format that log record by using the formatter or layouts.
Need for Logging
Components of Logging
The Java Logging components used by the developers to create logs and passes these logs to the corresponding destination in the proper format. There are the following three core components of the Java logging API:
The code used by the client sends the log request to the Logger objects. These logger objects keep track of a log level that is interested in, also rejects the log requests that are below this level.
In other words, it is responsible for capturing log records. After that, it passes the records to the corresponding appender.
Generally, the Loggers objects are named entities. The entities separated by the dot operator. For example, java.net, java.awt, etc.
The namespace is managed by the LogManager and it is represented in hierarchical order. It must be aligned with the packaging namespace. But it is not mandatory to follow it exactly. We can also create an anonymous Logger but it will not appear in the shared namespace.
In the logging namespace, the Logger search for the parent loggers. It is closest to the extant ancestor in the logging namespace. Remember that there is no parent for the root logger. It inherits the various attributes from their parent such as:
Logging Handlers or Appender
Java Logging API allows us to use multiple handlers in a Java logger and the handlers process the logs accordingly. There are the five logging handlers in Java:
In the Java Logging API, the FileHandler and ConsoleHandler are the two handlers provided by default. By extending the Handler class or any subclasses (like MemoryHandler, StreamHandler, etc.), we can create our own and also customize the Handler class. the selection of the appenders depends on the logging requirements. If you are not sure which appenders to use, it may affect the application's performance.
Example of Custom Java Logging Handler
Logging Formatters or Layouts
The logging formatters or layouts are used to format the log messages and convert data into log events. Java SE provides the following two standard formatters class:
It generates a text message that has general information. It writes human-readable summaries of log messages. The ConsoleHandler uses the class to print the log message to the console. For example, the following console message shows
The XMLFormatter generates the log message in XML format. It writes the detailed XML structure information. It is the default formatter for the FileHandler. The log entries look like the following:
By extending the Formatter class, we can customize and create our own Formatter class. We can use the customized class with any handlers.
If you are using Logging frameworks, you can use other layouts such as HTML, JSON, Syslog, plain text, etc.
Example of Customized Formatter Class
Java Logging Levels
It shows the rough guide to the importance and urgency of the log message, and also controls the logging details. Each log level object has an integer value. The higher value indicates higher priorities. There is a total of nine levels, seven standard logs, and two special log levels. The first three logging levels FINEST, FINER, and FINE represent the detailed tracing information that includes what is going on in the application and what happened in the application.
The following table shows the level and the value corresponding to it.
Let's discuss each logging level in detail.
Setting Up the Logging Level
We can use the following statement to set up the logging level.
The above statement sets the logging level to CONFIG and provides the information related to the configuration of the application. It also generates the log from the specified level to the greater levels.
Suppose, we have specified the log level to FINE, then the generated log will be CONFIG, INFO, WARNING, and SEVERE.
We can also set it to the configuration file just by changing the Logger's configuration, as we have shown below:
To log events in Java, you have to make sure that you assign a level to easily filer out the events. To assign a level and mention a message we can use the methods given below:
In the above statement, we have specified the level INFO and the message to be printed is, Display message.
Logger in Java logs only events that are at or above the INFO level, to make sure this, we use the setLevel() method.
Java Log Manager
In Java, the LogManager is a class that belongs to the java.util.logging package. It keeps track the global logging configuration, creates, and maintains the logger instances. We can also use it to set its own application-specific configuration. It includes the following two things:
The LogManager can be retrieved by using the getLogManager() method. It automatically initialized when we create LogManager. This property allows container applications (such as EJB containers) to substitute their own subclass of LogManager in place of the default class.
Create New Logger in Java
We can easily create Logger in Java by using the getLogger() method. It finds a logger if a logger is already defined and returns the same logger, else creates a new logger for a named subsystem.
If it creates a new logger, its log level will be configured that depend on the LogManager configuration, also configures the LogManager, and sends the logging output to its Handlers. It will be registered in the LogManager global namespace.
It parses the logger name as a parameter. The logger name must be separated by the dot (.) operator and should be based on the package name or class name of the subsystem. For example, java.awt, java.net, etc.
It returns a suitable logger and throws NullPointerException if we do not specify the logger name.
Java Logger Class
Java provides a class Logger that belongs to java.util.logging package. It contains the methods related to logging.
The logger class provides methods for logging that are grouped into the following five categories:
Therefore, we can conclude that the LogManager performs the actual logging. The instances of the LogManager accessed by the getLogger() method. The global logger instance is retrieved by the static field GLOBAL_LOGGER_NAME of the Logger class. It provides casual use of the Logging package.
We can also use the logging framework to make the logging concept easy. There is the following popular logging framework used for logging:
Next TopicReverse a String Using Recursion in Java