One logfile per run with log4j

Using filePattern Property

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Properties>
        <property name="filePattern">${date:yyyy-MM-dd-HH_mm_ss}</property>
    </Properties>
    <Appenders>
        <File name="File" fileName="export/logs/app_${filePattern}.log" append="false">
            <PatternLayout
pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
            <AppenderRef ref="File" />
        </Root>
    </Loggers>
</Configuration>

I had troubles retrieving which Udo Klimaschewski's answer Udy was referring so I put here my solution. log4j.properties:

# Root logger option
log4j.rootLogger=INFO, fileout

# Direct log messages to file
log4j.appender.fileout=org.apache.log4j.FileAppender
log4j.appender.fileout.File=/logs/myapp_${current.date}.log
log4j.appender.fileout.ImmediateFlush=true
log4j.appender.fileout.Threshold=debug
log4j.appender.fileout.Append=false
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=%5p | %d | %m%n

Then put in the main class this block:

public class Starter {
    static{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hhmmss");
        System.setProperty("current.date", dateFormat.format(new Date()));
    }

To start a new log file on app startup you may want to use OnStartupTriggeringPolicy

Triggering Policies

Example of XML config:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Properties>
        <Property name="log-path">C:/Logs/</Property>
    </Properties>
    <Appenders>
        <appender name="console" class="org.apache.log4j.ConsoleAppender">
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
            </layout>
        </appender>
        <RollingFile name="RollingFile" fileName="${log-path}/app.log"
                     filePattern="${log-path}/app-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout>
                <pattern>%d{dd/MM/yyyy HH:mm:ss} [%-5p/%t]: %C{1}(%L): %m%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10MB"/>
                <OnStartupTriggeringPolicy />
            </Policies>
            <DefaultRolloverStrategy max="100"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="root" level="debug" additivity="false">
            <appender-ref ref="RollingFile" level="debug"/>
            <appender-ref ref="console" level="debug"/>
        </Logger>
        <Root level="debug" additivity="false">
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

Works well for me


Combine the answer of Udo Klimaschewski and the answer from this question in order to get the desired result.

  1. add the property append = false
  2. add a current.time system property and use it in the file name