Jakarta Commons Logging

Posted By : Sudhanshu Singh | 30-Sep-2022

Generally known as JCL which is an abbreviation for Jakarta Commons Logging that provides an interface and thin wrappers for logging systems. JCL act as a bridge for other underlying frameworks and also depends on the underlying frameworks like log4j, jdk14, jdkLumberjack, Logkit. This interface enables you to change the logging implementation( jdk14Logger, Simple logger) for a deployed application without any changes to the logging code in the application.

Firstly, Download the Apache Commons Logging Library from here.

Import these two classes:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Create a Log instance:

private Log log = LogFactory.getLog(CLASSNAME.class);

Since Logger has 6 levels that are:

  1. FATAL
  2. ERROR
  3. WARN
  4. INFO
  5. DEBUG
  6. TRACE

Each has method like

  1. log.fatal("");
  2. log.error("");
  3. log.warn("");
  4. log.info("");
  5. log.debug("");
  6. log.trace("");

respectively.

There would be two cases:
CASE-I
If you haven't specified any configuration file then the logger checks for default logging system configuration in which the level will be INFO so that all higher levels(warn, error, fatal)  are enabled.
CASE-II
If you have specified any configuration files then the logger first checks for the log4j Logger and implements its configuration. If not present, check for the jdk14 and java.util.logging classes will be used. Again failure, built-in Simple Log will be used. The order for logger configuration check is:

  1. log4jLogger
  2. jdk14Logger
  3. simpleLogger

 

Configuring the Logger
Log4j configuration(Log4J is the Log4J Logging API)

  1. Download the log4j library from here.
  2. create a file named with "log4j.properties" in any folder and paste the below code:
log4j.rootLogger=DEBUG, consoleAppender, fileAppender

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n

log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
log4j.appender.fileAppender.File=demoApplication.log

Jdk14 configuration(Jdk14Logger uses the Java 1.4+ java.util.logging classes)

  1. Simply create a file named with "commons-logging.properties" and paste the below code:
# commons-logging.properties
# jdk handlers
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# default log level
.level=INFO

# Specific logger level
MyClassLogger.level=FINE

# FileHandler options - can also be set to the ConsoleHandler
# FileHandler level can be set to override the global level:
#java.util.logging.FileHandler.level=WARN

# log file name for the File Handler
java.util.logging.FileHandler.pattern=javalog%u.log

# Specify the style of output (simple or xml)
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

# Optional - Limit the size of the file (in bytes)
java.util.logging.FileHandler.limit=50000

# Optional - The number of files to cycle through, by
# appending an integer to the base file name:
java.util.logging.FileHandler.count=1

SimpleLogger Configuration(SimpleLog sends all messages to System.err)

  1. Download the simpleLogger library from here.
  2. Create a file named with "commons-logging.properties" and paste the below code:
# Default logging level for all instances of SimpleLog.
# Must be either trace, debug, info, warn, error or fatal. Defaults to info.
org.apache.commons.logging.simplelog.defaultlog=warn

# Logging detail level for a SimpleLog instance named MyClassLogger.
# Must be either trace, debug, info, warn, error or fatal. Defaults to the
# above default if not set.
org.apache.commons.logging.simplelog.log.MyClassLogger=debug

# Show the log name in every message. Defaults to false.
org.apache.commons.logging.simplelog.showlogname=true

# Show the last component of the name to be output with every message. Defaults to true.
org.apache.commons.logging.simplelog.showShortLogname=true

# Show the date and time in every message. Defaults to false
org.apache.commons.logging.simplelog.showdatetime=true

At last, Right click on the Project>>Run Configuration>>Classpath>>click on the "User Entries" and then click on Advanced>>Add a folder in which the properties file created>> Apply and Run.

Let us know of any Queries.
Thanks!

About Author

Author Image
Sudhanshu Singh

Sudhanshu is an accomplished Backend Developer with a diverse skill set that includes Core Java, Git Hub, GitLab, MySQL, SQL, Data Structure, and Spring-boot. His profound understanding of back-end development is evident in his unwavering commitment to the progress of projects such as Oodles Dashboard and Oodles.com. Sudhanshu's drive for excellence fuels his continuous efforts to enhance his skills and deliver exceptional results. He actively stays up-to-date with the latest industry trends and advancements, ensuring that he can provide innovative solutions.

Request for Proposal

Name is required

Comment is required

Sending message..