Thursday, August 18, 2016

Log4j Java Tutorial with Example (For Intermediate Level!)

Project Structure (I chose to work on this project structure because I had already done this project. You can start with a simple hello world project):
Maven Project Structure to Test Java Log4j 
First of all, change the pom.xml file:
Add the following log4j dependencies:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.18</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.18</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>

Whenever you change the pom file, it's always a good practice to update the maven project. 
Just go to project properties >> Maven >> and update the project

Copy Paste the log4j-1.2.14.jar, slf4j-api-1.6.1.jar and slf4j-log4j12-1.6.1.jar files in the following directory:
C:\glassfish4\glassfish\domains\domain1\lib

Copy paste log4j.xml in the following directory
C:\glassfish4\glassfish\domains\domain1\lib\classes directory

Now, edit log4j.xml file with text editor and change the appender file values:
Copy the glassfish server.log location (C:\glassfish4\glassfish\domains\domain1\logs)
C://glassfish4//glassfish//domains//domain1//logs//service.log or you can simple put short url like C://mytest//spring.log
C://glassfish4//glassfish//domains//domain1//logs//dao.log
C://glassfish4//glassfish//domains//domain1//logs//sprig.log

Also, change the threshold level to DEBUG.
<param name="Threshold" value="DEBUG"/>
other threshold levels are TRACE | DEBUG | INFO | WARN | ERROR | FATAL | ALL

Make sure to match the Appender name to Appender-ref name

Also, make sure you type the package name correctly
for example: <logger name="com.cubic.rest">

Now, go to the java class file where you want to implent the logger,
 for example: PersonServiceImpl.java
Inside class, initialize the log4j logging library as
private final static Logger logger = LoggerFactory.getLogger(PersonServiceImpl.class);

Now, use the log4j logger:
logger.debug("Entering PersonServiceImpl.savePerson"); //debug message goes here
logger.info("Person ID= {}, FirstName = {}",vo.getPk(),vo.getFirstName()); //info message goes here

Now, deploy the project in glassfish. Note: You should build the project successfully first.
#####IMPORTANT: Once you deploy the project successfully after a successful build (mvn clean install),
if you get any error because of issues in log4j.xml or any library related issues, you don't need to redeploy the project.
But, you need to stop the glassfish server and start it again.
asadmin start-domain
asadmin stop-domain



Now, Open Advance REST Client
Enter the url, example: http://desktop-39b14eu:9080/product-rest/ps/create
method: POST
Content-Type: application/json
Raw payload:
{
  "firstName":"hariom",
  "lastName":"Pandey"
}
Once you click send button and see the log file, you will see the log message like this:
2016-08-18 15:55:33,456 DEBUG http-listener-1(1) [com.cubic.service.PersonServiceImpl     ]  - Entering PersonServiceImpl.savePerson
2016-08-18 15:55:33,456 INFO  http-listener-1(1) [com.cubic.service.PersonServiceImpl     ]  - Person ID= null, FirstName = hariom
2016-08-18 15:55:33,701 DEBUG http-listener-1(1) [com.cubic.service.PersonServiceImpl     ]  - Person Details = PersonVO [pk=45, firstName=hariom, lastName=Pandey]
2016-08-18 15:55:33,701 DEBUG http-listener-1(1) [com.cubic.service.PersonServiceImpl     ]  - Exiting PersonServiceImpl.savePerson

Now you are done! Let me know in comment section if you get any errors or need assistance in setting up log4j in simple project. Happy Learning. :)



**************************************
inside log4j.xml file:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="SERVICELOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C://mytest//service.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="3000KB"/>
<param name="MaxBackupIndex" value="50" />
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<appender name="DAOLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C://mytest//dao.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="3000KB"/>
<param name="MaxBackupIndex" value="50" />
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<appender name="SPRINGLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C://mytest//spring.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="3000KB"/>
<param name="MaxBackupIndex" value="50" />
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %t [%-40.40c] %x - %m%n"/>
</layout>
</appender>
<logger name="com.cubic.rest">
<appender-ref ref="DAOLOG"/>
 <level value="DEBUG"/>
</logger>
<logger name="com.cubic.service">
<appender-ref ref="SERVICELOG"/>
 <level value="DEBUG"/>
</logger>
<logger name="org.springframework">
<appender-ref ref="SPRINGLOG"/>
 <level value="DEBUG"/>
</logger>
<root>
  <level value="DEBUG"/>
</root>
</log4j:configuration>

**************************************
Log4j dependencies for Maven, add the following code inside pom.xml file:
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.18</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.18</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
<scope>compile</scope>

</dependency>
********************************************************

No comments:

Post a Comment