i try to use second log file with my default log file in servlet
i have this log4j.xml configuration .
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" ERROR="false">
<appender name="FILE_error" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="maxFileSize" value="10240KB" />
<param name="maxBackupIndex" value="5" />
<param name="File" value="logs/engine.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %t %-5p [%c{1}] %m%n "/>
</layout>
</appender>
<appender name="CONSOLE_error" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %t %-5p [%c{1}] %m%n "/>
</layout>
</appender>
<category name="errorLogger" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE_error"/>
<appender-ref ref="FILE_error"/>
</category>
<appender name="FILE" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="maxFileSize" value="10240KB" />
<param name="maxBackupIndex" value="5" />
<param name="File" value="logs/error_info.log"/>
<param name="Append" value="true"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %t %-5p [%c{1}] %m%n "/>
</layout>
</appender>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %t %-5p [%c{1}] %m%n "/>
</layout>
</appender>
<category name="com.foo" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</category>
<category name="org.apache" additivity="false">
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</category>
<root>
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="CONSOLE_error"/>
<appender-ref ref="FILE_error"/>
</root>
</log4j:configuration>
now in the servlet i set the logger like this :
public class PaymentConfirmation extends HttpServlet implements Constants{
private static final Logger errorLog= Logger.getLogger("errorLogger");
but when running :
errorLog.debug("Starting app");
i got :
log4j:WARN No appenders could be found for logger (errorLogger).
log4j:WARN Please initialize the log4j system properly.
log4j and log4j.xml are in class path
what do i do wrong here ?
I was also getting the same error. Can you please try running your java program with -Dlog4j.debug java option. It will give some insight into what is failing and you would be able to resolve that then.
Related
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>
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
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>
In a external file I would like to log the output of my com.foo.test class to a specific file but whats happening is its appending the console log as well as file log. Below is my xml configuration,
<appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/server.log"/>
<param name="Append" value="false"/>
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
</layout>
</appender>
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<appender name="MYTESTAPPENDER" class="org.jboss.logging.appender.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.home.dir}/log/mytest.log"/>
<param name="Append" value="false"/>
<param name="MaxFileSize" value="3MB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<logger name="testsplitlogger" additivity="false">
<level value="info"/>
<appender-ref ref="MYTESTAPPENDER"/>
</logger>
<root>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
<appender-ref ref="MYTESTAPPENDER"/>
</root>
And My Java code is,
private static final Logger logger = Logger.getLogger("testsplitlogger");
Update:
The problem is its logging all the console output and other appenders output to mytest.log file instead I want to append only the info messages from my testsplitlogger for specific class and other logs from console and file should be written to server.log.
Am I doing anything wrong?
Please see the <root> xml config. You are allowing the log to be directed on all three different files.
CONSOLE (system.out)
FILE(in this case server.log) and
MYTESTAPPENDER(mytest.log)
To your requirement you need to define like this.
<root>
<priority value="info"/>
<!--<appender-ref ref="CONSOLE"/>-->
<!--<appender-ref ref="FILE"/>-->
<appender-ref ref="MYTESTAPPENDER"/>
</root>
EDIT:
It seems you have updated the question since I last posted... So my following suggestion has already been taken care of by you.
The testsplitlogger must contain the appender reference as well.
<logger name="testsplitlogger" additivity="false">
<level value="info" />
<appender-ref ref="MYTESTAPPENDER" />
</logger>
Here is a good link you can refer to http://wiki.apache.org/logging-log4j/Log4jXmlFormat
You need to create a Category for that:
<category name="my.company.package" additivity="false">
<priority value="info" />
<appender-ref ref="MYTESTAPPENDER" />
</category>
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.