Log4j daily and rolling

Hi, I need to configure my log4j logging module in such a way to be able to manage daily and rolling features.
What I need is to use a name pattern as
filename-%d(yyyyMMdd).%i.log
that rolls when a certain dimension is raised and when the date changes.
Any clues?
Thanks in advance
hanoirulesAsked:
Who is Participating?
 
keyurkarnikConnect With a Mentor Commented:
Here is a complete solution.

You need to write your own triggering policy as well as your rolling policy.

Here is an excerpt from an example that i often refer to :

> 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);
>  }
>
> }
> =================
> 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();
>  }
>
> }
>
> =================
>  <appender class="org.apache.log4j.rolling.RollingFileAppender"
> name="InfoRollingFileAppender">
>    <param name="Threshold" value="info"/>
>    <param name="File" value="/home/app/log/info.log"/>
>    <param name="Append" value="true"/>
>    <rollingPolicy class="your.package.MyTimeBasedRollingPolicy">
>      <param name="FileNamePattern"
> value="/home/app/log/info-%d{yyyy-MM-dd-HH_mm_ss}.log"/>
>    </rollingPolicy>
>    <triggeringPolicy class="your.package.MySizeBasedTriggeringPolicy">
>      <param name="MaxFileSize" value="6MB"/>
>    </triggeringPolicy>
>    <layout class="org.apache.log4j.PatternLayout">
>      <param name="ConversionPattern" value="%d [%t] %x %-5p %50.50c -
> %m%n"/>
>    </layout>
>  </appender>



This is not my own code. I will try to find the link to it to give the author due credit.. someone named Martin or Marten if I am not mistaken
0
 
ksivananthCommented:
try this,

        <appender name="file"
                class="org.apache.log4j.DailyRollingFileAppender">
                <param name="Threshold" value="DEBUG" />
                <param name="File" value="${app.log.home}/filename" />
                <param name="DatePattern" value="'.'yyyyMMdd'.log'" />
                <param name="BufferedIO" value="false" />
                <param name="Append" value="true" />
                <layout class="org.apache.log4j.PatternLayout">
                        <param name="ConversionPattern" value="%d{ISO8601} [%t] %-5p %c - %m%n" />
                </layout>
        </appender>
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
hanoirulesAuthor Commented:
Hi! thanks for your quick responses! let me explain better the scenario.
What I need is to have both feature working together on the same file:
- I want to configure a log file name as filename-%d(yyyyMMdd).%i.log
- this file must roll on the date change AND on raising a certain dimension with maxFileBackupIndex of RollingFileAppender
Thanks in advance
0
 
gordon_vt02Commented:
Take a look at the Apache extras for log4j, specifically the classes in the org.apache.log4j.rolling package.  You can probably use the RollingFileAppender and write your own TriggerPolicy that triggers a rollover on either the date change or when a certain file size is reached.  You can then return an appropriate RolloverDescription that creates the filename you want.

Extras Home:
http://logging.apache.org/log4j/companions/extras/index.html

Extras API:
http://logging.apache.org/log4j/companions/extras/apidocs/index.html
0
 
hanoirulesAuthor Commented:
keyurkarnik, thanks a lot for your great help! :))
0
 
keyurkarnikCommented:
Glad it helped :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.