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

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.

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>

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 doesn't create backup files

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");
}

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?

Categories

Resources