1

I am using Log4j for logging output of my application. The log4j.properties files contents the following:

    log4j.logger.DEFAULT_LOGGER=INFO,main_log, stdout
    log4j.additivity.DEFAULT_LOGGER = false

    # Direct log messages to a log file
    log4j.appender.main_log=org.apache.log4j.FileAppender
    log4j.appender.main_log.File=mainLog.log
    log4j.appender.main_log.layout=org.apache.log4j.PatternLayout
    log4j.appender.main_log.layout.ConversionPattern=%d{yyyy mm dd HH:mm:ss} %5p %c{1}:%L - %m%n

    # Direct log messages to stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy mm dd HH:mm:ss} %5p %c{1}:%L - %m%n

Currently the following happens. When I start my main method where I call

static Logger log = Logger.getLogger("DEFAULT_LOGGER");
log.fatal("Process Logger");

it prints the output to stdout and file.log, which is fine. But when I start my application a second time it adds the output to the existing file. But I want to overwrite the log file. Is this possible? (I dont want to use Java for deleting, also I dont want to delete it manually of course) Is there an option in Log4J to tell the logger is to overwrite or the add output?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Metalhead89
  • 1,740
  • 9
  • 30
  • 58

2 Answers2

1

You need to use RollingFileAppender for this.

Check following SO questions on similar lines:

Community
  • 1
  • 1
mtk
  • 13,221
  • 16
  • 72
  • 112
1

You can use FileAppender, add log4j.appender.fileAppender.Append=false property to it