Just re-checked our code, and it wasn't exactly behaving as described above.
I said "no renaming necessary" but our log-files are renamed exactly once:
a timestamp is added to the filename when rolling over.
The advantage is that the name of the 'current' log-file is predictable, so
you can do "tail -F mylogfile.log"
We created these two classes:
===================================
import org.apache.log4j.rolling.RollingPolicy;
import org.apache.log4j.rolling.RolloverDescription;
import org.apache.log4j.rolling.TimeBasedRollingPolicy;
/**
* Same as org.apache.log4j.rolling.TimeBasedRollingPolicy but acts only as
* RollingPolicy and NOT as TriggeringPolicy.
*
* This allows us to combine this class with a size-based triggering policy
* (decision to roll based on size, name of rolled files based on time)
*
*/
public class MyTimeBasedRollingPolicy implements RollingPolicy {
TimeBasedRollingPolicy timeBasedRollingPolicy = new
TimeBasedRollingPolicy();
/**
* Set file name pattern.
* @param fnp file name pattern.
*/
public void setFileNamePattern(String fnp) {
timeBasedRollingPolicy.setFileNamePattern(fnp);
}
/**
* Get file name pattern.
* @return file name pattern.
*/
public String getFileNamePattern() {
return timeBasedRollingPolicy.getFileNamePattern();
}
public RolloverDescription initialize(String file, boolean append) throws
SecurityException {
return timeBasedRollingPolicy.initialize(file, append);
}
public RolloverDescription rollover(String activeFile) throws
SecurityException {
return timeBasedRollingPolicy.rollover(activeFile);
}
public void activateOptions() {
timeBasedRollingPolicy.activateOptions();
}
}
===================================
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.Appender;
import org.apache.log4j.rolling.TriggeringPolicy;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.OptionHandler;
/**
* Copy of org.apache.log4j.rolling.SizeBasedTriggeringPolicy
* but able to accept a human-friendly value for maximumFileSize, eg. "10MB"
*
* Note that sub-classing SizeBasedTriggeringPolicy is not possible because
that class is final
*/
public class MySizeBasedTriggeringPolicy implements TriggeringPolicy,
OptionHandler {
/**
* Rollover threshold size in bytes.
*/
private long maximumFileSize = 10 * 1024 * 1024; // let 10 MB the default
max size
/**
Set the maximum size that the output file is allowed to reach
before being rolled over to backup files.
<p>In configuration files, the <b>MaxFileSize</b> option takes an
long integer in the range 0 - 2^63. You can specify the value
with the suffixes "KB", "MB" or "GB" so that the integer is
interpreted being expressed respectively in kilobytes, megabytes
or gigabytes. For example, the value "10KB" will be interpreted
as 10240.
* @param value the maximum size that the output file is allowed to reach
*/
public void setMaxFileSize(String value) {
maximumFileSize = OptionConverter.toFileSize(value, maximumFileSize +
1);
}
public long getMaximumFileSize() {
return maximumFileSize;
}
public void setMaximumFileSize(long maximumFileSize) {
this.maximumFileSize = maximumFileSize;
}
public void activateOptions() {
}
public boolean isTriggeringEvent(Appender appender, LoggingEvent event,
String filename, long fileLength) {
return (fileLength >= maximumFileSize);
}
}
===================================
And use it like this:
<appender class="org.apache.log4j.rolling.RollingFileAppender"
name="RollingFileAppender">
<param name="File" value="/home/app/log/mylogfile.log"/>
<param name="Append" value="true"/>
<rollingPolicy class="some.package.log4j.MyTimeBasedRollingPolicy">
<param name="FileNamePattern"
value="/home/app/log/mylogfile-%d{yyyy-MM-dd-HH_mm_ss}.log"/>
</rollingPolicy>
<triggeringPolicy
class="some.package.log4j.MySizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="6MB"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %30X{ip} %8X{agent}
%-5p %50.50c - %m%n"/>
</layout>
</appender>
Maarten
On Tue, Apr 13, 2010 at 2:45 PM, Curt Arnold <[email protected]> wrote:
>
> On Apr 13, 2010, at 2:25 AM, Mohan.Radhakrishnan wrote:
>
> >
> > 1. I had to copy the newer DTD from the Apache extras JAR to the log4j
> JAR.
> > 'rollingPolicy' was not recognized by the older DTD. Is this right ?
>
> rollingPolicy is in the DTD in log4j 1.2.16 which can be downloaded from
> http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.16/apache-log4j-1.2.16.tar.gz(doesn't
> work with Safari as it does not respect the content type of
> text/html and thinks it is the tarball itself and not an HTML page
> describing mirrors). http://logging.apache.org/log4j/1.2/download.htmlshould
> be updated shortly.
>
> >
> > 2. Put both JARS in the classpath.
> >
> > This warning is shown though.
> >
> > log4j:WARN Continuable parsing error 182 and column 23
> > log4j:WARN The content of element type "log4j:configuration" must match
> >
> "(renderer*,appender*,plugin*,(category|logger)*,root?,(categoryFactory|loggerFactory)?)".
>
> log4j.dtd specifies a specific order for the children of
> log4j:configuration however log4j is more permissive. This warning
> indicates that configuration doesn't list appenders first, loggers next and
> then the root logger.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>