Java Logging Functionality

Seralahthan
4 min readMar 10, 2019

In this article, let’s discuss about the logging functionality in Java.

What is Logging

Logging in simple words refers to the recording of an application activity.
Logging is used to store exceptions, information, and warnings as messages that occur during the execution of a program.

Proper logging helps a programmer a lot in the debugging process of a program.

There are several logging options available for Java. Widely used java logging options are the following.

  • java.util.logging (Log implementation of native java)
  • Log4j with MDC / Log4J with NDC
    Log implementation of logging facades like apache.commons.logging or SLF4J)
  • SLF4J (Simple logging facade or abstraction for logging frameworks)
  • Logback (Successor Log implementation of Log4J)

    In this blog let’s discuss some insides on each of these loggers.

java.util.logging

Java’s native logging facility without using any external framework/packages.

Java.util.logging is a built in logging mechanism in Java, but that doesn’t make it the greatest!

Log4J

Log4J means “Log for Java” which explains that is a java framework/package to do application logging of java applications.

Log4j is a defacto standard logging library for Java. It is an Apache library available for logging. Log4J isn’t the newest tool to do this, there are newer ways (SLF4J, for instance).

Log4J has basic components loggers, appenders, and layouts which are used to serve the purpose of logging in a systematic manner.

In order to use Log4J we need to configure the packages that need to be logged with the relevant log levels applied to the packages.

Log4J allows to provide configurations in various ways, using either log4j.properties file or log4j.xml file.

Depending on the log level provided, the amount of information logged will vary.

Log4J is an implementation of apache.commons.logging.
Log4J Apache Commons Logging is an abstraction for the concrete implementation. It uses log4j, if present and configured.

For example, if you write a code using commons logging and deploy it on JBOSS, the logging is done by log4j. If the same code is deployed on WebSphere logging is done by WebSphere’s own logging implementation.
Run the same code as a stand alone application it uses Java’s own logging.

Log4j with MDC

Using Log4J and its MDC (mapped diagnostic contexts) Or Log4J and its NDC (nested diagnostic contexts), we can achieve interleaved different user account logging.

Class MDC (org.apache.log4j.MDC) is inherited from the java.lang.Objectclass. It provides mapped diagnostic contexts.

An MDC is an instrument for distinguishing interleaved log output from different sources. Log output is typically interleaved when a server handles multiple clients near-simultaneously.

Interleaved log output can still be meaningful if each log entry from different contexts had a distinctive stamp.

The MDC is managed on a per thread basis. A child thread automatically inherits a copy of the mapped diagnostic context of its parent.

Example :

MDC.put(user);
logger.log("illegal unauthorized attempt!");

log output will be something like this

[user TestUser] illegal unauthorized attempt!

Log4j with NDC

MDC class is similar to the NDC class except that it is based on a map instead of a stack.

NDC class (org.apache.log4j.NDC) implements nested diagnostic contexts as defined by Neil Harrison in the article “Patterns for Logging Diagnostic Messages” part of the book “Pattern Languages of Program Design 3”.

SLF4J

SLF4J stands for “Simple logging Facade for Java”.

It serves as a simple facade or abstraction for various logging frameworks, such as java.util.logging, logback and log4j.

SLF4J allows the end-user to plug in the desired logging framework at deployment time.

SLF4J is a abstract logging framework like JCL(jakarta-commons-logging), JCL is also called as Apache Commons Logging.

Apache Commons Logging (previously known as Jakarta Commons Logging or JCL) is a Java-based logging utility and a programming model for logging and for other tool-kits. It provides APIs, log implementations, and wrapper implementations over some other tools.

The only difference between selecting SLF4J over Apcahe Commons Logging is due to the class loader issues with JCL.

Read on more details on JCL Class loader issues

Logback

Logback is a successor to the popular log4j project.

logback architecture is very generic to be applied to any circumstances.

logback is divided into three modules

  • logback-core
  • logback-classic
  • logback-access

logback-core module provides the backbone for other two modules.

logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks such as log4j or java.util.logging (JUL).

The logback-access module integrates with Servlet containers, such as Tomcat and Jetty, to provide HTTP-access log functionality.

We can also build our own logback module’s on top of logback-core module.

References :

https://docs.oracle.com/javase/10/docs/api/java/util/logging/Logger.html
http://logging.apache.org/log4j/1.2/manual.html
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html
https://www.slf4j.org/manual.html
https://articles.qos.ch/classloader.html
https://logback.qos.ch/

--

--

Seralahthan

Consultant - Integration & CIAM | ATL@WSO2 | BScEng(Hons) in Computer Engineering | Interested in BigData, ML & AI