How to maintain Multiple log4j.xml configuration file - java

How to maintain multiple log4j.xml configuration files in one webapplication.
Eg:- In Servlet:-
public void init(ServletConfig arg0) throws ServletException {
PropertyConfigurator.configure("/home/madhavib/Desktop/Nar/Raju/log4j.properties");
}
In Jsp:-
public void jspInit() {
DOMConfigurator.configure("/home/madhavib/Desktop/Nar/log4j.xml");
}
In another Jsp:-
public void jspInit() {
DOMConfigurator.configure("/home/madhavib/Desktop/Nar/log4j.xml");
}
**log4j configuration files (loaded in jsp 1):**
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender"
class="org.apache.log4j.FileAppender" >
<!-- <param name="threshold" value="debug" /> -->
<param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/infojsp333.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c{5}- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
<!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="org.apache.jsp">
<appender-ref ref="console"/>
<appender-ref ref="FileAppender"/>
</logger>
<!--<root>
<appender-ref ref="FileAppender"/>
</root> -->
<logger name="pe1.com1.nextel1">
<level value="info"/>
<appender-ref ref="FileAppender"/>
</logger>
</log4j:configuration>
**log4j confuguration (loaded in jsp 2) :-**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender"
class="org.apache.log4j.FileAppender" >
<!-- <param name="threshold" value="debug" /> -->
<param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/examplejsp.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c{5}- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
<!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="org.apache.jsp">
<appender-ref ref="console"/>
<appender-ref ref="FileAppender"/>
</logger>
<logger name="pe.com.nextel">
<level value="info"/>
<appender-ref ref="FileAppender"/>
</logger>
</log4j:configuration>
**log4jconfiguration file(loaded in servlet) :-**
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FileAppender"
class="org.apache.log4j.FileAppender" >
<!-- <param name="threshold" value="debug" /> -->
<param name="File" value="/home/madhavib/apache-tomcat-7.0.42/logs/EntelProject/examplejsp.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %c{5}- %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="debug" />
<param name="LevelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
</filter>
<!-- <MarkerFilter marker="FLOW" onMatch="ACCEPT" onMismatch="DENY"/> -->
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="org.apache.jsp">
<appender-ref ref="console"/>
<appender-ref ref="FileAppender"/>
</logger>
<logger name="pe.com.nextel">
<level value="info"/>
<appender-ref ref="FileAppender"/>
</logger>
</log4j:configuration>
How to maintain multiple log4j.xml configuration files in one webapplication.
i have given log4j configuration information files , please look into that and tell me the how to proper develop with multiple log4j files.

You can pass multiple log4j configuration files in some ways:
You can set System properties :
System.setProperty("log4j2.configurationFile", "log4j2-1.xml,log4j2-2.xml");
You can put configuration files as "log4j2.component.properties" under resource section and define system property as follows :
log4j2.configurationFile=log4j2-1.xml,log4j2-2.xml
Please check official documentation for system properties

Related

log4j issue: redirect logg to many files

Actually i am using log4j 1.2 with an xml configration file;
Is it possible to redirect the log of each class in a specified file; i want to logg in 3 files.
<appender name="file" class="org.apache.log4j.FileAppender" >
<!-- <param name="Threshold" value="INFO" /> -->
<param name="File" value="log/archive.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}] %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.FileAppender" >
<!-- <param name="Threshold" value="INFO" /> -->
<param name="File" value="log/check.log" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}] %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1}: %m [%L]%n" />
</layout>
</appender>
<logger name="org.apache.axis">
<level value="DEBUG"/>
</logger>
<root>
<priority value ="DEBUG"/>
<appender-ref ref="file" />
<appender-ref ref="console" />
</root>
you can file solution with log4j: Log output of a specific class to a specific appender

Log4j logging two times in logging file

Here is my log4j.xml
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="mylog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="myLogfile.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="com.mypackage1">
<level value="DEBUG" />
<appender-ref ref="mylog" />
<appender-ref ref="console" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
in myLogfile.log file i am getting the logging from "com.mypackage1" two times.
Please help put with the issue
If you are redirecting your console output to the same log file from shell, like you mentioned in the comment, then yes, your log file will have duplicate entries. One solution would be to use below log4j config in your program and redirect the console output to a different file - e.g. console.out, that will have your logs and std out separate. If you must have std out in the same file, then redirect std out to the same file.
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="mylog" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="myLogfile.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<logger name="com.mypackage1">
<level value="DEBUG" />
<appender-ref ref="mylog" />
<!-- <appender-ref ref="console" /> -->
</logger>
<root>
<level value="DEBUG" />
<!-- <appender-ref ref="console" /> -->
<appender-ref ref="myLog" />
</root>
</log4j:configuration>

Spring Batch: Logs not written in log file in AfterJob Method

I'm facing a problem with the logs when I'm launching the Job on Unix, actually the log file contains all the informations except those that I write in the log in a JobExecutionListener and exactly in the AfterJob Method.
When I'm using Eclipse I can see the report lines in the console.
Here is the configuration of log4j
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appenderConsole" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-6p [%-25c{1}] %m%n" />
</layout>
</appender>
<appender name="traceDefaultAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="log.tmp"/>
<param name="encoding" value="ISO-8859-1" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-6p [%-25c{1}] %m%n" />
</layout>
</appender>
<appender name="reportDefaultAppender" class="org.apache.log4j.FileAppender">
<param name="file" value="log.tmp"/>
<param name="encoding" value="ISO-8859-1" />
<param name="threshold" value="REPORT#com.cegedimactiv.fwb.core.business.log.LevelReport"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n" />
</layout>
</appender>
<logger name="defaultLogger">
<level value="INFO" />
<appender-ref ref="traceDefaultAppender" />
<appender-ref ref="reportDefaultAppender" />
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="appenderConsole" />
</root>
</log4j:configuration>
Any Idea?

How to get log4j to log my firms classes only

Below is a code of the log4j.xml file we are using at my firm. We been trying to change it to log only output from classes with made that is in org.xxxx. can someone let me know if this can be done and how to do it
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/disater_relief.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="rollinglogfile" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="logs/ennrollment.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="dailyrollinglogfile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="logs/ennrollment.log" />
<param name="immediateFlush" value="true" />
<param name="append" value="true" />
<param name="datePattern" value=" '.' yyyy-MM-dd "/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
</log4j:configuration>
Your root looger defines debug logging for all components. You should change it to warn (so you still see warnings) and add an extra logger for your own components:
<logger name="com.xxx.mycomponent">
<level value="trace"/>
<!-- appenders ... -->
</logger>
<root>
<priority value="warn" />
<appender-ref ref="console" />
<appender-ref ref="dailyrollinglogfile" />
</root>
Simply turn off the logging for the package you don't want to see in your logs...
<!-- Limit JBoss categories -->
<category name="org.jboss">
<priority value="OFF"/>
</category>

How to avoid root appender to appended to sub loggers

I have implement a log4j.xml file having two loggers.
Both EventLogger and ErrorLogger logged in to textfile. Also I create console appender and appended it to the root. My problem is when I use EventLogger or ErrorLogger it both write to the console and to the text file also. I need to avoid that. Can some one please help me.
Following I have showed you the xml file
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="ErrorAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./log/FIX5SvrError.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-4r [%t] %-5p %x - %m%n" />
</layout>
</appender>
<appender name="EventAPPENDER" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="./log/FIX5SvrEvent.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{HH:mm:ss,SSS} %-4r [%t] %-5p %x - %m%n" />
</layout>
</appender>
<appender name="ConsoleAPP" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
<logger name="ErrorLogger">
<level value="debug"/>
<appender-ref ref="ErrorAPPENDER" />
</logger>
<logger name="EventLogger">
<level value="debug"/>
<appender-ref ref="EventAPPENDER" />
</logger>
<root>
<priority value ="debug" />
<appender-ref ref="ConsoleAPP" />
</root>
</log4j:configuration>
For the loggers whose output you don't want percolating up, you need to set additivity = false.
<logger name="ErrorLogger" additivity="false">
<level value="debug"/>
<appender-ref ref="ErrorAPPENDER" />
</logger>
<logger name="EventLogger" additivity="false">
<level value="debug"/>
<appender-ref ref="EventAPPENDER" />
</logger>
Search for additivity in the manual for more details.

Categories

Resources