Log4j RollingFileAppender overwrites the old files - log4j

My appender is defined as below:
<appender name="M_FILE" class="org.jboss.logging.appender.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.home.dir}/log/m_ser.log"/>
<param name="Append" value="false" />
<param name="MaxFileSize" value="5MB"/>
<!--param name="MaxBackupIndex" value="25"/-->
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<appender-ref ref="M_FILE"/>
</root>
With this, I get m_ser.log upto 5MB, then it created m_ser.log.1, then it it created m_ser.log.2 and m_ser.log.1 file is missing. After a while, m_ser.log.3 got created and m_se.log.2 is missing.
It looks like the log4j is overwriting the backup files or it is unable to keep the old file.
This is log4j 1.2, Windows 7 system and JBoss 4.1

The MaxBackupIndex parameter should define how many backup files (.1,.2,.3, etc) are kept before it starts overwriting them.

Related

log4j dont want write to consol and file with system variable in path

I have a problem.
Log4j dont want write to console and to file with system variable in path. Only write to file with simple path.
Configuration of my log4j.
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<!-- <param name="file" value="d:/lv-098_JAVA/logs/log.log" /> -->
<param name="file" value="${LV098_JAVA}/src/main/resources/logs/log.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p (%F:%L:%M) %c{1} %m%n" />
</layout>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="file1" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
You are declaring
appender name="file" class="org.apache.log4j.DailyRollingFileAppender"
and in root tag you write
appender-ref ref="file1"
As there is no such file1. Due to this log4j is not working for you.
I found where problem is. It is in Jboss, because jboss have his own logger and it doesn't allow to write to console. You must change configuration of Jboss logger. But I don't know why it does not allow to use system variable.

org.apache.log4j.rolling.RollingFileAppender not working in linux

I am using org.apache.log4j.rolling.RollingFileAppender to generate daily logs.
It generates daily logs in windows, but it is not generating logs in linux.
Help is greatly appreciated!
Below is the log4j.xml file:
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="D:/source/logs/passports.log.%d{yyyy-MM-dd}.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<!-- The log message pattern -->
<param name="ConversionPattern" value="%d [%t] %-5p {%c} %m%n" />
</layout>
<root>
<priority value="info,debug,error" />
<appender-ref ref="ROLL" />
</root>
Your FileNamePattern needs to reflect the location of the file, specific to the server/workstation the application is running in.
In the snippet you gave, I see that the value for FileNamePattern is D:/source/logs/passports.log.%d{yyyy-MM-dd}.log.
The "D:" suggests to me that this is for your windows box.
Replace this value with the location that your linux box would hold the log file, for example: /var/source/logs/passports.log.%d{yyyy-MM-dd}.log

Configure common log4j xml for multiple sub projects

I am trying to use a common log4j xml for subprojects in tomcat. There is a Parent project deployed already and part of the parent project are three other projects. Two projects A and B already exist and the logging works fine. I am adding a new project C and updated the log4j like below. I do see the ProjectC.log file being created (which is happening when tomcat starts up), but there are no Project C related log statements in this file (or any other file). This is my current log4j xml :
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
<appender name="rootAppender" 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="ProjectAAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/projectA.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="100000KB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %x - %m%n"/>
</layout>
</appender>
<appender name="ProjectBAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/ProjectB.csv"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout"/>
</appender>
<appender name="ProjectCAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="${catalina.base}/logs/ProjectC.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="3"/>
<layout class="org.apache.log4j.PatternLayout"/>
</appender>
<logger name="projA" additivity="true">
<level value ="DEBUG" />
<appender-ref ref="ProjectAAppender"/>
</logger>
<logger name="projA.Performance" additivity="true">
<level value ="INFO" />
<appender-ref ref="ProjectBAppender"/>
</logger>
<logger name="projC" additivity="true">
<level value ="DEBUG" />
<appender-ref ref="ProjectCAppender"/>
</logger>
<root>
<priority value ="INFO" />
<appender-ref ref="rootAppender"/>
</root>
The way I get my log4j instance is using the slf4j LoggerFactory :
LoggerFactory.getLogger(clazz)
I have declared dependencies for log4j(1.2.14), slf4j-log4j12(1.4.1) jar files in my pom.
This setup works fine when I execute Project C independently (when running junit test cases).
How can I make logging work for project C ? Any changes that I should be making to my log4j xml ? Thank you.
Turns out I was using the wrong properties file. In the actual log4j.properties file all I had to do was to create a new appender named Project C and added this line instead of registering with the rootCategory :
log4j.logger.com.projectC.related.package=DEBUG, ProjectCAppender

log4J: Failure in post-close rollover action using TimeBasedRollingPolicy

I have setup TimeBasedRollingPolicy to rollout the file every minute (for test purpose) and the problem I am facing is a warning and no zip or gz file is being created. Warning is:
log4j:WARN Failure in post-close rollover action
I attached the source to figure-out the problem but have no success yet. Am I missing any configuration in my log4j.xml?
<appender name="errorAppender" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="File" value="C:/error.log"/>
<param name="Append" value="true"/>
<param name="BufferedIO" value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern" value="C:/error.%d{ddMMMyyyy HH:mm:ss}.log.gz" />
<param name="ActiveFileName" value="C:/error.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C (line:%L) - %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMax" value="error"/>
<param name="LevelMin" value="error"/>
<param name="AcceptOnMatch" value="true"/>
</filter>
</appender>
I am using log4j-1.2.17 and apache-log4j-extras-1.1. Has anybody seen this problem or have any clue about it?
Problem with "log4j:WARN Failure in post-close rollover action" message is that in windows-based systems you can not create a file name with the ":" char, so the FileNamePattern specified should not contain any one of these: \, /, :, *, ?, ", <, >, |
Here it is a log4j.xml for my application that works fine using a rolling file appender. For testing purposes I made the rolling to create a new file every second:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="consola" class="org.apache.log4j.ConsoleAppender">
<param name="target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="[%d{yyyyMMdd HH:mm:ss:mm,SSS}]%-5p [%t] [%c{1}-%M:%L] - %m%n"/>
</layout>
</appender>
<appender name="desarr" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="Append" value="false"/>
<rollingPolicy name="desarr" class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="fileNamePattern" value="C:/workspace/Probador/log/backups/importacion222.log_%d{mmss_mm}"/>
<param name="activeFileName" value="C:/workspace/Probador/log/importacion222.log"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="[%d{yyyyMMdd HH:mm:ss:mm,SSS}]%-5p [%t] [%c{1}-%M] - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="consola" />
<appender-ref ref="desarr"/>
</root>
</log4j:configuration>
Special attention to:
<param name="fileNamePattern" value="C:/workspace/Probador/log/backups/importacion222.log_%d{mmss_mm}"/>
Try this before attempting to zip the file.
I hit the same issue in log4j with WARN message - "log4j:WARN Failure in post-close rollover action" and the log file was not rolling over. It was root caused to insufficient permission issue on the directory into which log file was getting written. In this case, Java's File.renameTo() method was failing silently (just returns a boolean false). Took lot of time to figure out the issue :(
I am using log4j-1.2.17 and apache-log4j-extras-1.1. Has anybody seen
this problem and have any clue about it?
I have also observed this problem using log4j-1.2.16 and apache-log4j-extras-1.1. The exact same message.
I have tried various tweaks to no avail. The only time when rollingPolicy->FileNamePattern seems to be observed is when it is used without the appender->File parameter and rollingPolicy->ActiveFileName parameter. But even still I have not seen it rollover successfully nor gz or zip previous files.
I also get the same messages:
log4j: setFile called: somepath/somefile.log, true
log4j: setFile ended
log4j:WARN Failure in post-close rollover action
Very frustrating.
For me the solution was to create manually the directory for archived files.
I Also had the same problem,but in my case it was because of the fact that the 'fileNamePattern' path folder did not exist. Rectifying that worked for me and the rollover files were being created then.
If you are using the org.apache.log4j.rolling.TimeBasedRollingPolicy rollingPolicy, then the directory must exist prior to log4j being able to rotate.
For example, the following rollover will only work if /var/log/blah/archive/YYYY/MM directory exists; create it in a nightly cronjob should do the trick. And, as mentioned previously, this will also occur when there is not enough permission to create the log file.
<appender name="infoFile"
class="org.apache.log4j.rolling.RollingFileAppender">
<param name="threshold"
value="INFO"/>
<param name="append"
value="true"/>
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="ActiveFileName"
value="/var/log/blah/file.log"/>
<!-- IMPORTANT the archive folder must already exist, or log4j cannot
put the rotated log there, and will keep using the old one -->
<param name="FileNamePattern"
value="/var/log/blah/archive/%d{yyyy}/%d{MM}/file.log.%d{yyyy-MM-dd}.gz"/>
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%5p | %-40c{2} | %-4L | %d{yyyy-MM-dd}T%d{HH:mm:ss} | %m%n"/>
</layout>
</appender>

Logging to temporary directory in a cross-platform manner

I have an application for which log4j logging is configured in a log4j.properties file. Currently, this application runs on UNIX and creates a log file in /tmp. This application needs to run on Windows, and on that platform I would like for it to select the correct temporary directory, which I believe is C:\temp.
How can I change my log4j.properties file to make this happen? Do I need to switch to using an XML configuration file?
I think you would just use ${java.io.tmpdir} in place of a hard-coded path.
As of Log4J v1.2.14, I was able to use this in both a log4j.xml file as well as a log4j.properties file. There was some discussion on the web that variables wouldn't parse in the DOMReader, but they do as of this version of log4j.
<appender name="rolling_file_appender_ourapp" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${user.home}/.mycompany/OurApp.log" />
<param name="Append" value="false" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="3" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d | %-5p | %c | %m | %t | %x %n" />
</layout>
</appender>
or
log4j.appender.rfile=org.apache.log4j.FileAppender
log4j.appender.rfile.layout=org.apache.log4j.PatternLayout
log4j.appender.rfile.Append=false
log4j.appender.rfile.layout.ConversionPattern=%d [%p] %c %m%n
log4j.appender.rfile.File=${user.home}/.mycompany/OurApp.log

Resources