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 Je...@manulife.com on 2003/02/07 17:46:17 UTC

Do we have an appender for this?




I'm searching for an appender that can let my batch program log to a file.
And every time I restart my program, it renames the log file to something
with a digit or date at end of it, cleanup the current file, and start
logging to it.

It seems that Rolling and DailyRolling appenders address some of these
needs but not all. Unless I haven't understood their capabilities, which is
quite possible based on current documents on the site.

Jeff Saremi
Manulife Financial
416-926-3000 x7005
jeff_saremi@manulife.com


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


Re: Do we have an appender for this?

Posted by Davor Cengija <da...@mail.inet.hr>.
Jeff_Saremi@manulife.com wrote:

> 
> 
> 
> 
> I'm searching for an appender that can let my batch program log to a file.
> And every time I restart my program, it renames the log file to something
> with a digit or date at end of it, cleanup the current file, and start
> logging to it.
> 
> It seems that Rolling and DailyRolling appenders address some of these
> needs but not all. Unless I haven't understood their capabilities, which
> is quite possible based on current documents on the site.
> 

Recently I had a similar request, except that I needed each time newly 
named file, e.g. log.<timestamp>.txt etc. So I created 
IncrementalFileAppender which extends FileAppender and overrides 
setFile(String file) only. It works good (although I don't know If I did it 
correctly, extending framework policy wise).

Here's the method:
,----[ 
/home/davor/wsad5-workspace/log4jtest/com/cengija/tools/log4j/IncrementalFileAppender.java 
]
| package com.cengija.tools.log4j;
| 
| import java.io.File;
| import java.io.IOException;
| 
| import org.apache.log4j.FileAppender;
| import org.apache.log4j.Layout;
| 
| /**
|  * @author Davor Cengija
|  * @version $Revision$
|  */
| public class IncrementalFileAppender extends FileAppender {
| 

        // constuctors snipped

| 
|     /**
|      * @see org.apache.log4j.FileAppender#setFile(String)
|      */
|     public void setFile(String file) {
|         String newFileName;
|         String append = "." + System.currentTimeMillis();
|         int lastDot = file.lastIndexOf('.');
|         if (lastDot == -1) {
|             // no extension
|             newFileName = file + append;
|         }
|         else {
|             newFileName = file.substring(0, lastDot) + append +
|             file.substring(lastDot);
|         }
|         super.setFile(newFileName);
|     }
| 
| }

`----

,----[ /home/davor/wsad5-workspace/log4jtest/log4j.properties ]
| log4j.rootLogger=DEBUG, FILE
| #log4j.appender.FILE=org.apache.log4j.FileAppender
| log4j.appender.FILE=com.cengija.tools.log4j.IncrementalFileAppender
| log4j.appender.FILE.File=/tmp/log4j.test
| log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
| log4j.appender.FILE.layout.ConversionPattern=%-4r %-5p %37c %3x - %m%n

`----

and finally

davor@lothar /tmp $ ls -l log4j*                 
-rw-rw-r--    1 davor    davor     148 Feb  9 12:00 log4j.1044788409048.test
-rw-rw-r--    1 davor    davor     148 Feb  9 12:00 log4j.1044788419590.test
-rw-rw-r--    1 davor    davor     148 Feb  9 12:00 log4j.1044788432518.test
-rw-rw-r--    1 davor    davor     148 Feb  9 12:00 log4j.1044788440455.test


No special configuration parameters, although in some more mature version 
you'll probably need it.

Also, you might want to rename the existing file (/tmp/log4j.test in my 
case) to log4j.<timestamp>.test and then to continue to log to log4j.test 
so that you log tracing tools need no reconfiguration.

Hope that helps,
Davor
-- 
davor.cengija@mail.inet.hr


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


Re: Do we have an appender for this?

Posted by Ceki Gülcü <ce...@qos.ch>.
Jeff,

No, I am afraid not. Would you mind entering your request as an enhancement 
in bugzilla? Thank you,

At 11:46 07.02.2003 -0500, you wrote:

>I'm searching for an appender that can let my batch program log to a file.
>And every time I restart my program, it renames the log file to something
>with a digit or date at end of it, cleanup the current file, and start
>logging to it.
>
>It seems that Rolling and DailyRolling appenders address some of these
>needs but not all. Unless I haven't understood their capabilities, which is
>quite possible based on current documents on the site.
>
>Jeff Saremi
>Manulife Financial
>416-926-3000 x7005
>jeff_saremi@manulife.com
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-user-help@jakarta.apache.org

--
Ceki 


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