Wednesday, August 25, 2010

Log4j in J2EE

1- Create log4j.properties with following example contents. Important part is bold, i.e. you are creating a logger which will act as root of all of your classes. In this case all you classes reside in com.abc.xyz package or its sub-packages.


log4j.logger.com.abc.xyz=DEBUG, CONSOLE

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d : [%-5p] - %m%n



2- Put log4j.properties in EarContent/APP-INF/classes or WebContent/WEB-INF/classes.


3- Any where in your startup code e.g. ServletContext or Servlet or EJB, configure log4j.

PropertyConfigurator.configure(this.getClass().getClassLoader().getResource("log4j.properties"));

No comments: