You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by "Mohan.Radhakrishnan" <mo...@fss.co.in> on 2010/04/05 13:17:49 UTC

RE: RollingFileAppender not working consistently

We see the same problem. Sometimes several successive attempts to rename
fail.

Actually we also have multithreaded application and we think that one of the
threads could be writing when the file is being renamed.

We are thinking of using the DailyRollingAppender. Is that an alternative ?
-- 
View this message in context: http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28139063.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: RollingFileAppender not working consistently

Posted by Maarten Bosteels <mb...@gmail.com>.
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 <ca...@apache.org> 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: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: RollingFileAppender not working consistently

Posted by Curt Arnold <ca...@apache.org>.
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.html should 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: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


RE: RollingFileAppender not working consistently

Posted by "Mohan.Radhakrishnan" <mo...@fss.co.in>.
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 ?

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)?)".
Activating options
Activating options
-- 
View this message in context: http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28219427.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


RE: RollingFileAppender not working consistently

Posted by "Mohan.Radhakrishnan" <mo...@fss.co.in>.
This seems to be working now. There is no need for renaming a file unless I
am missing something !

I get one timestamped file for a day.

Now the next step could to combine with a SizeBasedTriggeringPolicy



Mohan.Radhakrishnan wrote:
> 
> TimeBasedRollingPolicy seems to be the solution. Correct me if I am wrong.
> 
> Extract from the doc.
> 
> During November 23rd, 2004, logging output will go to
>  *       the file <code>/wombat/foo.2004-11-23</code>. At midnight and for
>  *       the rest of the 24th, logging output will be directed to
>  *       <code>/wombat/foo.2004-11-24
> 

-- 
View this message in context: http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28216608.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


RE: RollingFileAppender not working consistently

Posted by "Mohan.Radhakrishnan" <mo...@fss.co.in>.
TimeBasedRollingPolicy seems to be the solution. Correct me if I am wrong.

Extract from the doc.

During November 23rd, 2004, logging output will go to
 *       the file <code>/wombat/foo.2004-11-23</code>. At midnight and for
 *       the rest of the 24th, logging output will be directed to
 *       <code>/wombat/foo.2004-11-24
-- 
View this message in context: http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28215329.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


RE: RollingFileAppender not working consistently

Posted by "Robinson, Brian L (IS)" <Br...@ngc.com>.
That sounds like it could work.

Is there an existing appender that does that or did you write your own?  Did it require modifying the log4j source code or were you able to extend using the existing binaries?

-----Original Message-----
From: Maarten Bosteels [mailto:mbosteels.dns@gmail.com] 
Sent: Saturday, April 10, 2010 7:01 AM
To: Log4J Users List
Subject: Re: RollingFileAppender not working consistently

On Fri, Apr 9, 2010 at 7:15 AM, Mohan.Radhakrishnan <mo...@fss.co.in>wrote:

>
> I have looked at the source here and I have a question.
>
>
> Since the log4j extras has other methods of rolling over that are time
> based
> I thought if we schedule the rename for a lean period when there is not
> much
> log file updates then there would be no problem. Am I on the right track ?
>
> What about appenders that don't roll over but create a file the first time
> with a timestamp ? Does this solve the roll over issue ?
>
>
yes.
That's what we do. Our appender's decision to roll is based on fileSize, the
name of the file is based on timestamp. No renaming needed.

Maarten


>
> --
> View this message in context:
> http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28187724.html
> Sent from the Log4j - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: RollingFileAppender not working consistently

Posted by Maarten Bosteels <mb...@gmail.com>.
On Fri, Apr 9, 2010 at 7:15 AM, Mohan.Radhakrishnan <mo...@fss.co.in>wrote:

>
> I have looked at the source here and I have a question.
>
>
> Since the log4j extras has other methods of rolling over that are time
> based
> I thought if we schedule the rename for a lean period when there is not
> much
> log file updates then there would be no problem. Am I on the right track ?
>
> What about appenders that don't roll over but create a file the first time
> with a timestamp ? Does this solve the roll over issue ?
>
>
yes.
That's what we do. Our appender's decision to roll is based on fileSize, the
name of the file is based on timestamp. No renaming needed.

Maarten


>
> --
> View this message in context:
> http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28187724.html
> Sent from the Log4j - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

RE: RollingFileAppender not working consistently

Posted by "Mohan.Radhakrishnan" <mo...@fss.co.in>.
I have looked at the source here and I have a question.


Since the log4j extras has other methods of rolling over that are time based
I thought if we schedule the rename for a lean period when there is not much
log file updates then there would be no problem. Am I on the right track ?

What about appenders that don't roll over but create a file the first time
with a timestamp ? Does this solve the roll over issue ?


-- 
View this message in context: http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28187724.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


RE: RollingFileAppender not working consistently

Posted by "Mohan.Radhakrishnan" <mo...@fss.co.in>.
Don't mean to hijack this thread.

We have a pressing need to move to DailyRollingFileAppender due to the same
problems mentioned in this thread.

Since midnight is not suitable due to heavy logging we want to use
DailyRollingFileAppender at midday or some other time. Is that possible ?

I refer
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html
-- 
View this message in context: http://old.nabble.com/RollingFileAppender-not-working-consistently-tp28087307p28147954.html
Sent from the Log4j - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org