Skip to main content

Java Logger



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

Popular posts from this blog

How to answer conflict-resolution interview questions

QUESTION 1: How do you deal with conflict? People aren’t going to get along with each other all the time. It’s just a fact. Employers want to know that you can respond to conflict diplomatically, says job-search and interview coach Thea Kelley. If you’re a my-way-or-the-highway type of personality, you’re not going to get very far in the interview. Start off by emphasizing communication and respectfulness as a means to conflict resolution. For example, “I always take the person aside and discuss the issue privately. I listen actively to make sure I understand the other person’s point of view, and I work with the person to develop a solution together.” Stress that even if you both don’t completely agree on the end result, you tried to at least meet each other halfway. Pro tip: “Don’t non verbally communicate resentment when telling a story,” Kelley advises. “Aggressive body language and tone of voice can show that you harbor bad feelings.” QUESTION 2: Tell me about a time when you had

Why do I need to override the equals and hashCode methods in Java?

  Imagine you have this MyClass first = new MyClass( "a" , "first" ); MyClass second = new MyClass( "a" , "second" ); Override only  equals If only  equals  is overriden, then when you call  myMap.put(first,someValue)  first will hash to some bucket and when you call  myMap.put(second,someOtherValue)  it will hash to some other bucket (as they have a different  hashCode ). So, although they are equal, as they don't hash to the same bucket, the map can't realize it and both of them stay in the map. Although it is not necessary to override  equals()  if we override  hashCode() , let's see what would happen in this particular case where we know that two objects of  MyClass  are equal if their  importantField  is equal but we do not override  equals() . Override only  hashCode If you only override  hashCode  then when you call  myMap.put(first,someValue)  it takes first, calculates its  hashCode  and stores it in a given bucket. Then wh