Java 如何使用 log4j.xml 为日志文件创建文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20521556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to Create a folder for log file using log4j.xml
提问by Human Being
I just created log4j.xml file like ,
我刚刚创建了 log4j.xml 文件,例如,
<?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_logsmanually in the particular location means, it works fine and log file created.
如果我在特定位置手动创建了一个文件夹Core_logs意味着它可以正常工作并创建日志文件。
How to create the folder , if the folder is not exist in a particular location?
如果文件夹不存在于特定位置,如何创建文件夹?
采纳答案by Ben
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
这在这里也可以帮助您/看起来是最适合您的解决方案: 如果目录不存在,则配置 Java FileHandler Logging 以创建目录
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 版本 1.2.15 做到了。在下面从 Arun P Johny 那里寻找答案,他从 log4j 源代码中发布了一段代码。我忽略了它,因为它没有被接受为答案。
回答by kola swathi
Log4j.xml file for creating in your eclipse
用于在 Eclipse 中创建的 Log4j.xml 文件
<?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"?>