Prevent log4j2 from creating parent folder of a log file if it doesn't exist

Multi tool use
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>
java log4j2
|
show 1 more comment
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>
java log4j2
That seems like an odd goal - what would you have it do instead, fail? It shouldn't be hard to check for this situation before invoking it.
– Chris Stratton
Dec 30 '18 at 0:40
I would like it to fail if it doesn't find the parent folder, or simply do nothing.
– Jalal Sordo
Dec 30 '18 at 0:44
Are you specifying a logging.path flag somewhere in your program?
– Krishna
Dec 30 '18 at 0:51
@kirishna do you meanfileName="${sys:app.home}/app.log"
from above.
– Jalal Sordo
Dec 30 '18 at 0:54
No, apart from that. I use logback in spring boot and if I want it to fail, I specify the logging.path flag (which is in the properties file) with a folder that should exist. Do you use a framework?
– Krishna
Dec 30 '18 at 1:03
|
show 1 more comment
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>
java log4j2
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>
java log4j2
java log4j2
edited Dec 30 '18 at 0:52
Jalal Sordo
asked Dec 30 '18 at 0:38
Jalal SordoJalal Sordo
85932759
85932759
That seems like an odd goal - what would you have it do instead, fail? It shouldn't be hard to check for this situation before invoking it.
– Chris Stratton
Dec 30 '18 at 0:40
I would like it to fail if it doesn't find the parent folder, or simply do nothing.
– Jalal Sordo
Dec 30 '18 at 0:44
Are you specifying a logging.path flag somewhere in your program?
– Krishna
Dec 30 '18 at 0:51
@kirishna do you meanfileName="${sys:app.home}/app.log"
from above.
– Jalal Sordo
Dec 30 '18 at 0:54
No, apart from that. I use logback in spring boot and if I want it to fail, I specify the logging.path flag (which is in the properties file) with a folder that should exist. Do you use a framework?
– Krishna
Dec 30 '18 at 1:03
|
show 1 more comment
That seems like an odd goal - what would you have it do instead, fail? It shouldn't be hard to check for this situation before invoking it.
– Chris Stratton
Dec 30 '18 at 0:40
I would like it to fail if it doesn't find the parent folder, or simply do nothing.
– Jalal Sordo
Dec 30 '18 at 0:44
Are you specifying a logging.path flag somewhere in your program?
– Krishna
Dec 30 '18 at 0:51
@kirishna do you meanfileName="${sys:app.home}/app.log"
from above.
– Jalal Sordo
Dec 30 '18 at 0:54
No, apart from that. I use logback in spring boot and if I want it to fail, I specify the logging.path flag (which is in the properties file) with a folder that should exist. Do you use a framework?
– Krishna
Dec 30 '18 at 1:03
That seems like an odd goal - what would you have it do instead, fail? It shouldn't be hard to check for this situation before invoking it.
– Chris Stratton
Dec 30 '18 at 0:40
That seems like an odd goal - what would you have it do instead, fail? It shouldn't be hard to check for this situation before invoking it.
– Chris Stratton
Dec 30 '18 at 0:40
I would like it to fail if it doesn't find the parent folder, or simply do nothing.
– Jalal Sordo
Dec 30 '18 at 0:44
I would like it to fail if it doesn't find the parent folder, or simply do nothing.
– Jalal Sordo
Dec 30 '18 at 0:44
Are you specifying a logging.path flag somewhere in your program?
– Krishna
Dec 30 '18 at 0:51
Are you specifying a logging.path flag somewhere in your program?
– Krishna
Dec 30 '18 at 0:51
@kirishna do you mean
fileName="${sys:app.home}/app.log"
from above.– Jalal Sordo
Dec 30 '18 at 0:54
@kirishna do you mean
fileName="${sys:app.home}/app.log"
from above.– Jalal Sordo
Dec 30 '18 at 0:54
No, apart from that. I use logback in spring boot and if I want it to fail, I specify the logging.path flag (which is in the properties file) with a folder that should exist. Do you use a framework?
– Krishna
Dec 30 '18 at 1:03
No, apart from that. I use logback in spring boot and if I want it to fail, I specify the logging.path flag (which is in the properties file) with a folder that should exist. Do you use a framework?
– Krishna
Dec 30 '18 at 1:03
|
show 1 more comment
1 Answer
1
active
oldest
votes
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;
}
}
....
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974434%2fprevent-log4j2-from-creating-parent-folder-of-a-log-file-if-it-doesnt-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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;
}
}
....
add a comment |
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;
}
}
....
add a comment |
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;
}
}
....
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;
}
}
....
answered Dec 30 '18 at 1:04


Арсен МирзаянАрсен Мирзаян
1297
1297
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53974434%2fprevent-log4j2-from-creating-parent-folder-of-a-log-file-if-it-doesnt-exist%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
SYV9Q xlJr6uJeTe,8XZvzGx5Pa9gLYRvWQYHYFKig mHxFdC1W3qvtm1T9 N YLO26fWqLJ,i44O2BezSzwmfG
That seems like an odd goal - what would you have it do instead, fail? It shouldn't be hard to check for this situation before invoking it.
– Chris Stratton
Dec 30 '18 at 0:40
I would like it to fail if it doesn't find the parent folder, or simply do nothing.
– Jalal Sordo
Dec 30 '18 at 0:44
Are you specifying a logging.path flag somewhere in your program?
– Krishna
Dec 30 '18 at 0:51
@kirishna do you mean
fileName="${sys:app.home}/app.log"
from above.– Jalal Sordo
Dec 30 '18 at 0:54
No, apart from that. I use logback in spring boot and if I want it to fail, I specify the logging.path flag (which is in the properties file) with a folder that should exist. Do you use a framework?
– Krishna
Dec 30 '18 at 1:03