Java log4j:ERROR setFile(null,false) 调用失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21140168/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:51:55  来源:igfitidea点击:

log4j:ERROR setFile(null,false) call failed

javamavenlog4jlog4net

提问by user3128668

Iam facing some strange error as below

我面临一些奇怪的错误如下

log4j:ERROR setFile(null,false) call failed.
java.io.FileNotFoundException: \debug.log (Access is denied)

src/main/dev/environment/dev/config.properties

src/main/dev/environment/dev/config.properties

logging.folder=D:/logs

log4j.xml

日志文件

<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
      <param name="append" value="false"/>
      <param name="file" value="${logging.folder}/debug.log"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{1}] %m%n"/>
      </layout>
   </appender>

maven.xml (not complete)

maven.xml(不完整)

<filters>
            <filter>src/main/environment/dev/config.properties</filter>
        </filters>

when iam directly using <param name="file" value="D:/logs/debug.log"/>, its working fine, BUT when am using <param name="file" value="${logging.folder}/debug.log"/>iam getting above exception..

当我直接使用时 <param name="file" value="D:/logs/debug.log"/>,它工作正常,但是当我使用我时会出现<param name="file" value="${logging.folder}/debug.log"/>异常..

Iam pretty sure values from property file able to read, since one of my java file able to read values from this property file only.. then why can't this log4j.xml file able to read?

我很确定属性文件中的值可以读取,因为我的一个 java 文件只能从这个属性文件中读取值..那么为什么这个 log4j.xml 文件不能读取?

please suggest me what went wrong?

请建议我出了什么问题?

回答by chandra pandey

Use forward slash ('/') instead of back slash ('\') where ever you are defining this path ${logging.folder}. Thats what error i see up there:

在定义此路径 ${logging.folder} 的任何地方使用正斜杠 ('/') 而不是反斜杠 ('\')。这就是我在那里看到的错误:

you are using back slash here:

你在这里使用反斜杠:

java.io.FileNotFoundException: \debug.log (Access is denied)

java.io.FileNotFoundException: \debug.log(访问被拒绝)

so your path should be something like: logging.folder = D:/logs/

所以你的路径应该是这样的:logging.folder = D:/logs/

回答by Ade

In addition to Chandra's answer, I experienced the same exception in Eclipse on Windows 7:

除了钱德拉的回答,我在 Windows 7 上的 Eclipse 中也遇到了同样的异常:

C:/myfile.log                     // access denied...

But it worked OK when I created a folder with permissions to my userid:

但是当我创建一个对我的用户 ID 具有权限的文件夹时它工作正常:

 C:/myLogs/myfile.log             // it wasn't necessary to create the file

I also noticed that the path must be specified with forward slashes.

我还注意到必须用正斜杠指定路径。

回答by Md Moin Uddin

For logging I use properties file and I use the following config for this same problem. As I use tomcat server, I use ${catalina.base}to get the tomcat base directory. log4j.appender.FILE.File=${catalina.base}/logs/debug.log

对于日志记录,我使用属性文件,并使用以下配置解决同样的问题。当我使用tomcat server 时,我${catalina.base}用来获取 tomcat 基本目录。 log4j.appender.FILE.File=${catalina.base}/logs/debug.log

For your xml config the syntex might be like the following

对于您的 xml 配置,syntex 可能如下所示

<param name="file" value="${catalina.base}/debug.log"/>

If you are using Tomcatthen hopefully It will work.

如果您使用的是Tomcat,那么希望它会起作用。

回答by Anton

This issue is nothing related to slashes people. This line of error:

这个问题与斜线人无关。这行错误:

java.io.FileNotFoundException: \debug.log (Access is denied)

java.io.FileNotFoundException: \debug.log(访问被拒绝)

is telling us that it is trying to create debug.log file in the root(\ represents root) and obviously it won't have access so access denied.

告诉我们它正在尝试在 root(\ 代表 root)中创建 debug.log 文件,显然它没有访问权限,因此访问被拒绝。

In my case, I solved this by adding VM arguments before starting the server. That's it.

就我而言,我通过在启动服务器之前添加 VM 参数解决了这个问题。就是这样。

Example : (In my case I'm using Wildfly, change the path to your server location)

示例:(在我的情况下,我使用的Wildfly是更改服务器位置的路径)

"-Dlogging.folder=C:\Program Files\wildfly-14.0.1.Final\standalone\log"

-Dlogging.folder=C:\Program Files\wildfly-14.0.1.Final\standalone\log