log4j rolling appender issue for multiple log file creation - java

I am using log4j for my application for printing logs. Till now, I have only one log file(say X.log) and it is getting rolled after a fixed size, say, 4 MB. Now , I am trying to log some particular requests and responses to a different log file, Y. Log.
My requirement is to roll the Y.log file also, after 4 MB. I am using rolling file appender for both X and Y logs. Y.log will be created in the same location where X.log is available.
But it seems like now both the log files are not getting rolled over after 4 MB. It is keep on appending to the same logs and the size is increasing. Below is my log4j configuration.
<appender name="Xlogfile" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="file" value="/logs/X.log" />
<param name="MaxBackupIndex" value="200" />
<param name="MaxFileSize" value="4MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<appender name="Ylogfile" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="true" />
<param name="file" value="/logs/Y.log" />
<param name="MaxBackupIndex" value="200" />
<param name="MaxFileSize" value="4MB" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref=" Xlogfile " />
<appender-ref ref=" Ylogfile " />
</root>
Could anyone please provide me a solution for this. Thanks in advance.

Try adding a policy to your appender, like this:
<Policies>
<SizeBasedTriggeringPolicy size="4mb"/>
</Policies>

Related

Log4j: Logger stops working after load third party jar

Logger suddenly stops working after call a method of third party jar. I have checked code of jar, they are also using log4j with log4j.xml. I am configuring log4j by using the properties file.
My log4j configuration
log4j.rootLogger=DEBUG, stdout, R
#log4j.rootLogger=ERROR, stdout, R
log4j.appender.R.File=/logs/app_test.log
log4j.appender.R.MaxFileSize=10MB
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.MaxBackupIndex=175
log4j.appender.R.layout.ConversionPattern=%d %p %t %c - %m%n
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.R=org.apache.log4j.RollingFileAppender
Third party logs configuration (log4j.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!--Pattern used: Date Thread_Name Full_Class_Name.Method_Name Level:Line
no -->
<!-- %-23d{ISO8601} [THREAD:%t] [%-2c.%M():%L] [%p] - %m%n -->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-23d{ISO8601} [%-5p] [%-22t] [%-30C{1}:%4L] - %m%n" />
</layout>
</appender>
<appender name="errorAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${catalina.base}/logs/metascan/metsascanWrapperError.log" />
<param name="maxFileSize" value="10MB" />
<param name="Threshold" value="ERROR" />
<param name="maxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy MM dd HH:mm:ss:SSS} [%-40t] %5p - %m %n" />
</layout>
</appender>
<appender name="infoAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${catalina.base}/logs/metascan/metsascanWrapperInfo.log" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="2" />
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-23d{ISO8601} [%-5p] [%-22t] [%-30C{1}:%4L] - %m%n" />
</layout>
</appender>
<appender name="debugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File"
value="${catalina.base}/logs/metascan/metsascanWrapperDebug.log" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="2" />
<param name="Threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%-23d{ISO8601} [%-5p] [%-22t] [%-30C{1}:%4L] - %m%n" />
</layout>
</appender>
<category name="com.xerox.metascan" additivity="true">
<priority value="ERROR" />
<appender-ref ref="infoAppender" />
<appender-ref ref="debugAppender" />
<appender-ref ref="consoleAppender" />
</category>
<!-- <category name="org.springframework"> <priority value="error" /> <appender-ref
ref="infoAppender90" /> <appender-ref ref="errorAppender90" /> </category> -->
<root>
<priority value="error" />
<appender-ref ref="errorAppender" />
<appender-ref ref="consoleAppender" />
</root>
</log4j:configuration>
I have read the configuration on log4j website, preference will always go to log4j.xml over properties file. So, I don't want to change in the third party jar. I am assuming that they are overriding my log4j configuration but I don't know the exact issue because even system.out.println doesn't work.
So how we I resolve the issue, is there any way I can ignore the third party logs configuration?
Any suggestions will be helpful, Thanks.
No, You cannot ignore the third party logs, You can override them by loading externally log4j configuration.
PropertyConfigurator.configure("log4j.properties");
but ideally, this is not a good practice. You should tell them who provided you the jar and they should fix that because any third party jar should not affect your configuration.

How to solve Log4J No Such Property warning/error?

I have a Log4J xml config file with appenders in it.
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<param name="File" value="/tomcat/website/webapps/app/logs/appInfo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param nawebsite/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<param name="DatePattern" value="'.'yyyy-MM-dd'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
When I build my project, I get the following warnings/errors in the console which I am trying to get rid of:
log4j:WARN No such property [datePattern] in org.apache.log4j.RollingFileAppender.
log4j:WARN No such property [file] in org.apache.log4j.ConsoleAppender.
I did not make these log4j config xml files; they were already part of the project.
I've seen the DatePattern documentation online, and it seems to suggest exactly what is in the DatePattern value, so I have no idea why it's still showing me that warning. Same with file.
Did the person who made this before me just stick properties in these appenders that do not belong? I'm having a difficult time verifying this.
Every Appender has it's own configuration properties. file make sense for RollingFileAppender but doesn't make sense for ConsoleAppender.
If you switch one appender to another and do not update configuration properties, then you will get such warnings.
To fix them, you need to remove unused (or inapplicable) properties from your configuration:
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
<appender name="rolling" class="org.apache.log4j.RollingFileAppender">
<param name="FileName" value="/tomcat/website/webapps/app/logs/app.log" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM dd HH:mm:ss} %F: %m%n" />
</layout>
</appender>
Complete list of appenders and their parameters is available on the Apache Commons Log4j webpage:
https://logging.apache.org/log4j/2.x/manual/appenders.html

Logging different levels using same object to different files

My simple question is : If I have two statements like logger.info() and logger.error() in Java (using log4j), I want the results of these two lines to be printed in two separate files. That is, logger.info(...) should print to a file say myLog.info and logger.error(...) should print to myLog.error file. I am using rolling file appender for this task. Also I want just one logger object to do the task. Someone might suggest two or more different loggers one for each file, but that's not the case.
I tried searching a solution for the problem. One of the links Creating multiple log files of different content with log4j says about "threshold" and I even tried to add threshold in my xml configuration file. But what it actually is doing that : info log is being printed in myLog.info file but error log gets printed in both the files. Can it be done through xml configuration file alone or a separate properties file is needed? If xml file is sufficient, then what needs to be done?
I am preferring xml file over properties file. If there is a working solution using only xml configuration file, that would be sufficient. Thanks in advance.
You could use filters to deny any messages except those of the level you want. Here is an example of how to do this:
First a class to test our efforts:
package test;
import org.apache.log4j.Logger;
public class Main {
private static final Logger logger = Logger.getLogger(Main.class);
public static void main(String[] args) {
logger.debug("here's some debug");
logger.info("here's some info");
logger.warn("here's some warn");
logger.error("here's some error");
logger.fatal("here's some fatal");
}
}
Next a log4j.xml config file to set up the appenders and loggers:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />
</layout>
</appender>
<appender name="debugLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/debug.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="maxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="DEBUG" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<appender name="infoLog" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/info.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="5000KB" />
<param name="maxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="LevelToMatch" value="INFO" />
<param name="AcceptOnMatch" value="true" />
</filter>
<filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>
<logger name="test" additivity="false">
<level value="DEBUG" />
<appender-ref ref="consoleAppender" />
<appender-ref ref="debugLog" />
<appender-ref ref="infoLog" />
</logger>
</log4j:configuration>
This pattern will allow you to generate a separate log for each log level, just repeat the configuration that I have provided for either debug or info logging. Note that the console appender will accept all levels.
I was able to gain some insight from this post so I thought I should give credit.

Spring jdbcTemplate debug messages not getting printed using log4j

I have the below log4j.xml in placed in the src directory of a web application. Even after follwing many post examples, i cannot get the below configuration print jdbcTemplate internals to log file.
Config:
<appender name="INTERNALSLOG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/springDB.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10mb"/>
<param name="MaxBackupIndex" value="5"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %C{2} - %m%n"/>
</layout>
</appender>
<logger name="org.springframework.jdbc.core">
<level value="DEBUG" />
<appender-ref ref="INTERNALSLOG"/>
</logger>
Whats wrong with the configuration. Just want to get the sql's from jdbcTemplate printed.

log4j writing to rolled over file

We have set log4j rollover as 500mb upon 500 mb it is creating a file new file log.1 but still it is sometimes writing to log.1 and to log and sometimes only to log and some times to log.1.
I am using apache tomcat.
It is so inconsistent.
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p: %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="500MB" />
<param name="maxBackupIndex" value="5" />
<param name="Append" value="true"/>
<param name="File" value="c:/logs/web.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %p [%c] - %m%n" />
</layout>
</appender>
Could you please let me know any reason for this?
Regards,
Adam

Categories

Resources