Hi,

Many times while deploying our application on application servers we want to have a separate application level logging in a separate application log file, Also many times it is desired to have a separate log4j configuration file inside the application so that additional configurations will not be required in order to deploy that application on Server.

In this demonstration we will see the following things
Requirement-1). How can an application will have it’s own “MyLog4j.xml” file ?
Requirement-2).
How to apply log filtering some of the unwanted messages will not appear in the log?

Step1). Create a directory somewhere in your file system with some name like “C:/TestAppLevelLog.war”. Then create “WEB-INF/classes” directories inside “C:/TestAppLevelLog.war”

Step2). Write your won log4j configurations like “MyLog4j.xml” file inside “C:/TestAppLevelLog.war/WEB-INF/classes” directory as following:

<?xml version="1.0" encoding="UTF-8" ?>
   <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
     <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
           <appender name="appender" class="org.apache.log4j.FilAS6pender">
	       <param name="File" value="${jboss.server.log.dir}/MyAppLogs.log"/>
	       <param name="Append" value="true"/>
                <layout class="org.apache.log4j.PatternLayout">
	           <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
	        </layout>
	   </appender>

      <root>
          <priority value ="trace"/>
          <appender-ref ref="appender"/>
      </root>
    </log4j:configuration>

NOTE: Here application log file name is “MyAppLogs.log” which will be created insdside the JBoss’s “$PROFILE/log” directory of your server. It is good practicde to avoid hardcoding of absolute paths.

Step3). Now in order to write some messages in the log file we will write the following kind of code inside JSP/servlet, Suppose it is “index.jsp” which we are going to place inside “C:/TestAppLevelLog.war” directory.

<%@ page import="java.net.*,org.apache.log4j.xml.DOMConfigurator,org.apache.log4j.Logger" %>
<%
		java.net.URL url=Thread.currentThread().getContextClassLoader().getResource("MyLog4j.xml");
		DOMConfigurator.configure(url);

               //NOTE: If u use log4j.properties file (Not the XML file) then use  PropertyConfigurator
               //PropertyConfigurator.configure(url);

		Logger logger = Logger.getLogger(this.getClass());
		logger.info(" This is an information message");
		logger.error("This is an error message");
		logger.fatal("This is a fatal error message");
		logger.warn("I want this message should not appear in the app logs...SOME STRING");

		out.println("<h1>Please check your application log file...for above logging.</h1>");
		out.println("<h3>If logging is not happening then make sure that the MyLog4j.xml file is placed inside WEB-INF/classes.</h3>");
%>

Step4). Now place some “web.xml” file inside your applications “C:/TestAppLevelLog.war/WEB-INF” directory like following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

</web-app>

Step5). Now depoy your application “TestAppLevelLog.war” inside “$PROFILE/deploy” directory and then access the application. like http://localhost:8080/TestAppLevelLog/index.jsp and then check the log file “$PROFILE/log/MyAppLogs.log” you will see following kind of messages logged:

19:51:39,465 [http-127.0.0.1-8080-1] INFO -  This is an information message
19:51:39,466 [http-127.0.0.1-8080-1] ERROR - This is an error message
19:51:39,466 [http-127.0.0.1-8080-1] FATAL - This is a fatal error message
19:51:39,466 [http-127.0.0.1-8080-1] WARN - I want this message should not appear in the app logs...SOME STRING

NOTE: If you want to filter out some special lines containing some specific strings should to appear in the application logs in that case you can apply the “org.apache.log4j.varia.StringMatchFilter” filter inside your “MyLog4j.xml” file as following:

<?xml version="1.0" encoding="UTF-8" ?>
   <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
     <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
           <appender name="appender" class="org.apache.log4j.FilAS6pender">
	       <param name="File" value="${jboss.server.log.dir}/MyAppLogs.log"/>
	       <param name="Append" value="true"/>
                <layout class="org.apache.log4j.PatternLayout">
	           <param name="ConversionPattern" value="%d [%t] %p - %m%n"/>
	        </layout>

              <filter class="org.apache.log4j.varia.StringMatchFilter">
                  <param name="StringToMatch" value="SOME STRING" />
                  <param name="AcceptOnMatch" value="false" />
              </filter>

	   </appender>

      <root>
          <priority value ="trace"/>
          <appender-ref ref="appender"/>
      </root>
    </log4j:configuration>

Now you will see that the line which was containing “SOME STRING” string will not appear in the logs:

19:51:39,465 [http-127.0.0.1-8080-1] INFO -  This is an information message
19:51:39,466 [http-127.0.0.1-8080-1] ERROR - This is an error message
19:51:39,466 [http-127.0.0.1-8080-1] FATAL - This is a fatal error message

NOTE:

JBoss AS6 already provides “log4j.jar” so you need not to place it separately inside “C:/TestAppLevelLog.war/WEB-INF/lib” directory.

.
.
Thanks
Middleware Magic Team

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.