Commons Logging library acts as a bridge
between different logging implementations (Ex: log4j, jdk14logger, simple
logger). By using commons-logging, you can change the underlying application
logging implementation without recompiling code. For example, assume you are
using jdk14 logger right now, you can switch to log4j without recompiling the
application.
HelloWorld.java
Hello World
Application
Step 1: Create new maven
project in Eclipse.
Open
Eclipse, right click on 'Project Explorer' -> New -> Other.
Search
for ‘Maven’.
Select
‘Maven Project’ and click on Next button.
Select
the check box ‘Create a simple project (skip archetype selection), and click on
Next button.
Give
the group id, artifact id as 'commonsLoggingDemo' and set the version to 1.
Click
on Finish button.
Project
structure is created like below.
Step 2: Update pom.xml with
maven dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>commonsLoggingDemo</groupId> <artifactId>commonsLoggingDemo</artifactId> <version>1</version> <dependencies> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
Step 3: Create a file
'commons-logging.properties' under src/main/resources.
commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger log4j.configuration=log4j.properties
Step 4: Create a file
'log4j.properties' under src/main/resources.
log4j.properties
log4j.rootLogger=DEBUG,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n
Step 5: Create a package
'com.sample.app' and define HelloWorld.java like below.
package com.sample.app; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class HelloWorld { private static final Log log = LogFactory.getLog(HelloWorld.class); public static void main(String... args) { log.debug("Debug Message"); log.info("Information Message"); log.warn("Warning Message"); log.error("Error Message"); log.fatal("Fatal Message"); } }
When
you ran HelloWorld.java application, you can able to see below messages in the
console.
DEBUG 2018-05-22 12:20:30,397 0 com.sample.app.HelloWorld [main] Debug Message INFO 2018-05-22 12:20:30,398 1 com.sample.app.HelloWorld [main] Information Message WARN 2018-05-22 12:20:30,398 1 com.sample.app.HelloWorld [main] Warning Message ERROR 2018-05-22 12:20:30,398 1 com.sample.app.HelloWorld [main] Error Message FATAL 2018-05-22 12:20:30,398 1 com.sample.app.HelloWorld [main] Fatal Message
No comments:
Post a Comment