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

Multi tool use
Multi tool use












0















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>









share|improve this question

























  • 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
















0















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>









share|improve this question

























  • 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














0












0








0








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>









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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



















  • 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

















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












1 Answer
1






active

oldest

votes


















0














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;
}
}
....





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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;
    }
    }
    ....





    share|improve this answer




























      0














      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;
      }
      }
      ....





      share|improve this answer


























        0












        0








        0







        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;
        }
        }
        ....





        share|improve this answer













        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;
        }
        }
        ....






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 1:04









        Арсен МирзаянАрсен Мирзаян

        1297




        1297






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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
            9S9p,b6yZQODQKDI7 BISlMWKAXOxNLd94v4yCDLd8F2DXGToJ

            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas