log4j doesn't create backup files - java

I have two jar files on the same classpath, so they are using the same log4j.xml. One of the projects where the xml file is in, rolls it's log file as I expected, but no the other project.
I have configured the xml to create 10 backup files with 1096K, but the backup files aren't being created and the main log file are being erased when the max size is reached.
log4j.xml:
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}][%-5p][%c.%M()]: %m %n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/arqGUI.log" />
<param name="MaxFileSize" value="4096KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}][%-5p][%c.%M()]: %m %n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<appender name="FILE_SERVICE" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/arqService.log" />
<param name="MaxFileSize" value="1096KB" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{ISO8601}][%-5p][%c.%M()]: %m %n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="FATAL" />
</filter>
</appender>
<logger name="com.emp.sin.orcmultemp.service" additivity="false" >
<level value="WARN" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE_SERVICE" />
</logger>
<logger name="org.springframework">
<level value="WARN" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<logger name="org.hibernate">
<level value="WARN" />
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</logger>
<root>
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
Use of log4j:
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class MainClass {
private static final Logger LOGGER = LogManager.getLogger(MainClass.class);
LOGGER.info("TEST");
}

Related

How to make logger.trace() and logger.info() work together

I have an issue logging TRACE function in distinct file from INFO function, i prepared a classic log4j.xml file containing all the required configuration.
logger.info() is working.
logger.trace() is not working.
<appender name="file" class="org.apache.log4j.FileAppender">
<param name="append" value="true" />
<param name="file" value="C:/logs/transaction.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="TRACE" />
<param name="LevelMax" value="TRACE" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="fileINFO" class="org.apache.log4j.FileAppender">
<param name="append" value="true" />
<param name="file" value="C:/logs/server.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<logger name="ma.prg.net">
<level value="trace" />
<appender-ref ref="file" />
</logger>
<logger name="ma.prg.net">
<level value="info" />
<appender-ref ref="fileINFO" />
</logger>
logger.trace() was working fine before i added fileINFO appender.
Thanks in advance.
You cannot configure the same logger twice. Just change your logger configuration to:
<logger name="ma.prg.net">
<level value="trace" />
<appender-ref ref="file" />
<appender-ref ref="fileINFO" />
</logger>
With this you send the logging message to both appenders. But as you already configured each appender individually with LevelMin and LevelMax, each filters out the messages it should print.

How to maintain Multiple log4j.xml configuration file

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

Log4j disabling console logging

I need to make logger that will only log to file, but when I set XML likie this:
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="5" />
<param name="file" value="${SERVER_HOME}/logs/mylog-log.txt" />
<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.packageforlogging" additivity="true">
<level value="DEBUG" />
<appender-ref ref="file" />
</logger>
It stills logs into console, because there are other loggers to which i don't have access. How I can do it?

Different Axis 1.4 Web Service Client log different logs file using log4j.xml

I am working on a financial sector project. The project has a number of web service client for different partner. Till now, the projects logs and the web service request response logs are been logged in different files, say a.log and b.log. Now, the partners wants the web service request and response logs are to be logged in different file partner wise i.e. I mean the b.log file is not required. Instead have partner1.log, partner2.log, partner3.log and so on... . And I am using log 4j.xml for all these purpose and this not a web application. The problem is I dont know to do this ?
Making the request and response logs from the web service client in different log file seems impossible for me using log4j.xml as I have two thing 1) different partner and 2) axis request and response logs at the same time.
I am using axis 1.4 for the client and have done good homework on this
Can any one suggest me out any solutions.
Thanks in advance.
The log4j.xml is the very same we use in general.
I HAVE ALSO ADDED IT NOW.
**<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="FILE_APPENDER"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${basedir}/logs/Server.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="FILE_APPENDER_PARTNER_1"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${basedir}/logs/Server_PARTNER_1.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="FILE_APPENDER_PARTNER_2"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${basedir}/logs/Server_PARTNER_2.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="FILE_APPENDER_PARTNER_3"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${basedir}/logs/Server_PARTNER_3.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="FILE_APPENDER_AXIS"
class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="${basedir}/logs/Server_AXIS.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<category name="org.apache.jcs">
<level value="WARN" />
</category>
<category name="org.hibernate.SQL">
<level value="WARN" />
</category>
<category name="org.hibernate">
<level value="WARN" />
</category>
<category name="org.slf4j.impl.JCLLoggerAdapter">
<level value="ERROR" />
</category>
<category name="com.util.PropertiesUtil">
<level value="ERROR" />
</category>
<!--NEWLY ADDED-->
<logger name="com.ws.stub.PARTNER_1">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_PARTNER_1" />
</logger>
<logger name="com.ws.stub.PARTNER_2">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_PARTNER_2" />
</logger>
<logger name="com.ws.stub.PARTNER_3">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_PARTNER_3" />
</logger>
<!--AXIS-->
<logger name="org.apache.axis.transport.http.HTTPSender">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.client.Call">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.client.AxisClient">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<!--my one-->
<logger name="org.apache.commons.httpclient">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.Handler">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.AxisFault">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.AxisEngine">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.handlers">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.client">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.client.Transport">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.MessageContext">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.handlers.BasicHandler">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.handlers.http.HTTPAuthHandler">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<logger name="org.apache.axis.handlers.http.HTTPActionHandler">
<level value="ALL" />
<appender-ref ref="FILE_APPENDER_AXIS" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="FILE_APPENDER" />
</root>
</log4j:configuration>**
can any one expert over here help me out with this issue?
Solved it successfully with a bit coding efforts with axis and the application.

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