Search This Blog

Showing posts with label Log4j. Show all posts
Showing posts with label Log4j. Show all posts

Sunday, September 27, 2015

How to configure Log4j

What is log4j ? How can we implement it? Explain log4j property ?

 

1. Log4j is logging framework developed by Apache
2. There are few steps to implement it into the project .

Step1 : download log4j.jar from apache web site and set into class path .


Step 2 : Create one log4j.properties and set into class path. 

log4j prop file should have following value with naming convention as shown below  :

log4j.rootLogger=ERROR, stdout, logfile // stdout to pring on console  , logfile to print log into file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=logs/sample.log
log4j.appender.logfile.MaxFileSize=5MB
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.logger.com.sample=DEBUG


That's it .. Here I have used two type of  appender  , console and file but there lot more also available to configure. Appenders are which helps to print log.

Converiosn patterns Layout decide the pattern of printing . d - date , p - type , c - class name , m - message , n - name

And last line is used for filtering and type according to your packages to make the decision of log printing.
Here under com.sample package all classes debug log message will print if log4j Logger has set into class as following :

private static Logger _log= Logger.getLogger("com.sample.tutorials.main.SpringMain");
_log.debug("test debug");

Note : XML (log4j.xml) is also another way to configure the log4j logger. 

Thursday, January 30, 2014

How to enable logging within CXF framework to trace SOAP inbound / outbound headers/requests and how it helped to figure out the problem of production incident

Recently surfaced with the problem in production with conflicts of web services calls , summarizing problem statement and troubleshooting approach :

Problem Statement :
Web service calls were not going through and giving 404 and reason was that something broken into header but enough information was not printing in log .

Troubleshooting Approach : Since we are using CXF framework hence more logging can be enabled which helped to figure out problem and we were able to fix problem with a tactical solution. Following are the steps which would help to trouble shoot Inbound and Outbound soap headers/requests of web services , It may help @ any point in time to discuss further with EIS team and to know the exact reason : Steps to enable logging :

Step 1 : Change in debug level ( in our case it is in local.propeties file) : Add following line in log4j section : log4j.logger.org.apache.cxf = INFO, FILE

Step 2 : Append highlighted (bold) JVM parameter . ( In our case it is in tomcat.generaloption property within properties file) :

tomcat.generaloptions=-Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger

Step 3 : Change in cxfclient-beans.xml file ( a bean file as part of application ) Add highlighted line into cxf:bus tag :


Step 4 : Do following acitons :

a) stop the server ( tomcat in our case )
b) run ant deploy ( to redeploy JVM option in our case , you may or may not have it if place dirctly into server)
c) start the server ( Tomcat )

it's all done :)

Cheers 

Kapil

Popular Posts