Ulrich Scholz
Ulrich Scholz

Reputation: 2111

log4j, clear logs while they are in use (not just at startup)

How to clear log4j log files while they are in use (not just at startup)? My first idea would be

How do I make log4j clear a log at startup? suggests to force a roll-over

Is there a better way?

Some details: I'm improving a tool that can be seen as two applications: An "inner" application logging with log4j and an "outer" one displaying this log via a gui. The inner and outer applications share their jvm. The inner one can be started via the gui several times. I now want to clear the log such that it shows only the output of the last run.

I am resticted to small changes only, so letting the inner application run in its own jvm is not an option. Neither is the frequent restart of the whole tool.

Upvotes: 1

Views: 2751

Answers (2)

Coder
Coder

Reputation: 3262

Use the rolling file appended in log4j...

It destroys the log file for every run.

Thanks and regards, Hari

Upvotes: 0

sferrazjr
sferrazjr

Reputation: 315

Try using MaxBackupIndex, see below:

log4j.appender.myproject=org.apache.log4j.RollingFileAppender
log4j.appender.myproject.File=/export/logs/myproject.log
log4j.appender.myproject.MaxFileSize=10000KB
log4j.appender.myproject.MaxBackupIndex=0
log4j.appender.myproject.layout=org.apache.log4j.PatternLayout

When the file size reachs 10MB the log will start over again.

Upvotes: 1

Related Questions