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
It provides the complete tracing information of the application.It records the critical failure if any occur in an application.
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:
- Loggers
- Logging Handlers or Appender
- Logging Formatters or Layouts
Loggers
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 Levels: If it is specified to null, it traverses towards the parent and search for the first non-null level.
- Handlers: a logger will log any message to the handlers of the parent recursively up to the tree.
- Resource bundle names: If any logger has a null resource bundle name, it inherits any resource bundle name for its parent recursively up to the tree.
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:
- StreamHandler: It writes the formatted log message to an OutputStream.
- ConsoleHandler: It writes all the formatted log messages to the console.
- FileHandler: It writes the log message either to a single file or a group of rotating log files in the XML format.
- SocketHandler: It writes the log message to the remote TCP ports.
- MemoryHandler: It handles the buffer log records resides in the memory.
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.
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:
- SimpleFormatter
- XMLFormatter
- SimpleFormatter
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
Oct 28, 2020 11:55:55 PM DemoClass main
SEVERE: An exception occurred.
XMLFormatter
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:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2020-10-29T11:59:55</date>
<millis>1595664378</millis>
<sequence>0</sequence>
<logger>DemoClass</logger>
<level>SEVERE</level>
<class>DemoClass</class>
<method>main</method>
<thread>1</thread>
<message>An exception occurred.</message>
</record>
</log>
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
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class MyFormatter extends Formatter
{
@Override
public String format(LogRecord record)
{
return record.getThreadID()+"::"+record.getSourceClassName()+"::"+record.getSourceMethodName()+"::"+new Date(record.getMillis())+"::"+record.getMessage()+"\n";
}
}
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.
Logging Frameworks
We can also use the logging framework to make the logging concept easy. There is the following popular logging framework used for logging:
- Log4j: Apache Log4j is an open-source Java-based logging utility.
- SLF4J: It stands for Simple Logging Facade for Java (SLF4J). It is an abstraction layer for multiple logging frameworks such as Log4j, Logback, and java.util.logging.
- Logback: It is an open-source project designed as a successor to Log4j version 1 before Log4j version 2 was released.
- tinylog(tinylog): It is a light weighted and open-source logger.
Comments
Post a Comment