3

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?

Alexey Grigorev
  • 2,415
  • 28
  • 47
Human Being
  • 8,269
  • 28
  • 93
  • 136
  • 1
    [Configuring Java FileHandler Logging to create directories if they do not exist](http://stackoverflow.com/questions/1263876/configuring-java-filehandler-logging-to-create-directories-if-they-do-not-exist) might help. Which version of log4j are you using? It looks like `1.2.15+` should already support it. – andyb Dec 11 '13 at 14:22
  • @andyb Thanks for your support.I used **1.2.9** .Now I have changes the version. – Human Being Dec 11 '13 at 15:30

2 Answers2

5

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.

Community
  • 1
  • 1
Ben
  • 1,157
  • 6
  • 11
  • 1
    In future, please try not to copy from other answers. Just vote to close a question since it has already been answered. If you cannot vote to close a question, add a comment indicating that it might be a duplicate question. Thanks! – andyb Dec 11 '13 at 16:25
-2

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"?>
scopchanov
  • 7,966
  • 10
  • 40
  • 68