Reputation: 1685
As the title of the question states, is it possible to avoid creating parent folders of log4j2 File appender log file if they don't exist?
Actually, when I pass the non-existing parent folder of the file appender log it will be created automatically by log4j2.
I have already tried to see if there is an attribute on the File
tag but there is nothing.
https://logging.apache.org/log4j/2.x/manual/appenders.html
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="File" fileName="${sys:app.home}/app.log"
ignoreExceptions="false">
<PatternLayout
pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
Upvotes: 2
Views: 1983
Reputation: 177
There is no such option. Here is source code of FileAppender where you can see creating of parent folder. If you want to avoid creating folder then you can create your own inheritor of FileAppender and override setFile method.
public synchronized void setFile(String fileName, boolean append, boolean bufferedIO, int bufferSize)
throws IOException {
....
FileOutputStream ostream = null;
try {
ostream = new FileOutputStream(fileName, append);
} catch(FileNotFoundException ex) {
String parentName = new File(fileName).getParent();
if (parentName != null) {
File parentDir = new File(parentName);
if(!parentDir.exists() && parentDir.mkdirs()) { //This is were parent folders are created
ostream = new FileOutputStream(fileName, append);
} else {
throw ex;
}
} else {
throw ex;
}
}
....
Upvotes: 1