I want to create day dependent logfiles with log4j2
:
<RollingFile name="APP" fileName="application-%d{yyyy-MM-dd}.log" />
Resulting logfile name: application-%d{yyyy-MM-dd}.log
, the timestamp is not replaced. Why?
The pattern should not be given in the attribute "fileName" rather you have to specify the pattern in the attribute "filePattern" as like below.
<RollingFile name="RollingFile" fileName="${log-path}/filename.log"
filePattern="${log-path}/filename-%d{yyyy-MM-dd}-%i.log" >
...
...
</RollingFile>
The "%i" is the counter that will be automatically incremented in rollover.
Hope this will help you.
To append the filename with date, replace %d with below format, i was having the same problem and but got by doing so :
<RollingFile name="APP" fileName="application-${date:yyyy-MM-dd}.log" />
Greşanu Emanuel - Vasile
Try this:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Properties>
<Property name="log-path">D:/logs/</Property>
</Properties>
<Appenders>
<RollingFile name="DebuggerLogger" fileName="${log-path}CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" filePattern="${log-path}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="DebuggerLogger"/>
</Root>
</Loggers>
</Configuration>
来源:https://stackoverflow.com/questions/31670088/how-to-add-the-date-timestamp-to-log4j2-logfiles