How can I craete a RollingFileAppender
in Logback that accepts the prudent flag and also allows me to specifity the location of the log files?
I tried the following but as I understand from the documentation logback doesn't support the file property. Is there another way to set the log file location?
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${MY_LOG_LOCATION_PROP}/logs/mylogfile.log</file>
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>mylogfile-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
If the file
property is missing, the currently active log file will be inferred from the value of fileNamePattern
. Thus, the file
property is not mandatory. Just as importantly, in prudent mode it must be left blank.
Here is the relevant quote from the documentation on fileNamePattern:
Note that the file property in RollingFileAppender (the parent of
TimeBasedRollingPolicy) can be either set or omitted. By setting the
file property of the containing FileAppender, you can decouple the
location of the active log file and the location of the archived log
files. The current logs will be always targeted at the file specified
by the file property. It follows that the name of the currently active
log file will not change over time. However, if you choose to omit the
file property, then the active file will be computed anew for each
period based on the value of fileNamePattern. The examples below
should clarify this point...
You can include the path in the fileNamePattern:
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/var/log/myapp/mylogfile-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>