Reputation: 31
Log4j2 createOnDemand="true"
does not allow creation of new file on a daily basis in-spite of using RollingFile Appenders
with TimeBasedTriggeringPolicy
.
Below is my log4j2.xml
file.
I have two appenders
, one is for all logs, another is for a custom purpose, which needs to be generated only on demand, but the createOnDemand
is overriding the Rolling nature of the log and it is not allowing to create new log file for the custom log.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<RollingFile name="App"
fileName="app.log"
filePattern="app.%d{yyyy-MM-dd}.log">
<PatternLayout pattern="[%t] %d{yyyy-MM-dd HH:mm:ss,SSS zzz} %-5p %l - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
<RollingFile name="custom"
fileName="appCustom.log"
filePattern="appCustom.%d{yyyy-MM-dd-HH-mm}.log"
createOnDemand="true">
<PatternLayout pattern="[%t] %d{yyyy-MM-dd HH:mm:ss,SSS zzz} %-5p %l - %m%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="AppLogger" level="info" additivity="false">
<AppenderRef ref="App"/>
</Logger>
<Logger name="customLogger" level="info" additivity="false">
<AppenderRef ref="custom"/>
</Logger>
<Root level="info">
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
Upvotes: 2
Views: 1324
Reputation: 8768
The file is also not created if fileName
contains one or more directories and one of them does not exists, e.g. <RollingFile fileName="logs/appCustom.log"/>
fails if logs
does not exist.
The directories are never created you need to take care of that yourself. Make sure you pay attention to the working directory where the directory is expected*. For example, it changes for every subproject of a Gradle project with multiple includes. Therefore you need logs
in every included project.
Make sure to exclude the logs
from git.
* I guess it's the current working directory. Nothing mentioned at the crucial points and I cannot check the complete 40 pages docs of appenders.
Upvotes: 0
Reputation: 31
I have found the fix for the above issue. This was an existing bug in lo4j2 which is fixed in the version - 2.13.1
Below are the links :
https://issues.apache.org/jira/browse/LOG4J2-2759
https://logging.apache.org/log4j/2.x/changes-report.html#a2.13.3
I was using 2.11.0
Upgrading resolved my issue.
Upvotes: 1