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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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
keyurkarnikCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hanoirulesAuthor Commented:
keyurkarnik, thanks a lot for your great help! :))
0
keyurkarnikCommented:
Glad it helped :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.