Question

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?

Was it helpful?

Solution

You need to use RollingFileAppender for this.

Check following SO questions on similar lines:

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top