简体   繁体   中英

how to Create a folder for log file using log4j.xml

I just created log4j.xml file like ,

<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration>
    <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
        <param name="Threshold" value="ALL" />
        <param name="MaxFileSize" value="512KB" />
        <param name="MaxBackupIndex" value="10" />
        <param name="File" value="F:/Core_logs/application_log.log" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n"/>
        </layout>
    </appender>

    <!--sets the priority log level for org.springframework -->
    <logger name="org.springframework">
        <level value="info" />
    </logger>
    <!--sets the default priority log level -->
    <root>
        <priority value="all"></priority>
        <appender-ref ref="fileAppender" />
    </root>
</log4j:configuration>

But I have the exception as ,

java.io.FileNotFoundException: F:\Spring_Core_logs\pointel_Aop.log (The system cannot find the path specified)

If I created a folder Core_logs manually in the particular location means, it works fine and log file created.

How to create the folder , if the folder is not exist in a particular location?

EDIT:

This here could also help you/looks like the best solution for you: Configuring Java FileHandler Logging to create directories if they do not exist

It seems like log4j version 1.2.15 does it. Look for an answer below from Arun P Johny, he posted a piece of code from the log4j sourcecode. I overlooked it because it was not accepted as an answer.

Log4j.xml file for creating in your eclipse

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">

<appender name="fileAppender" class="org.apache.log4j.FileAppender">

<param name="Threshold" value="INFO" />

<param name="Append" value="true" />

<param name="File" value="logfile.log"/>

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d %-5p [%c{1}] %m %n" />

</layout>

</appender>

<root>

<level value="INFO"/>

<appender-ref ref="fileAppender"/>

</root>

</log4j:configuration>"UTF-8"?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM