java 当我尝试使用 log4j 写入文件时,为什么会出现此 FileNotFoundException?

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

Why I obtain this FileNotFoundException when I try to use log4j to write into a file?

javajakarta-eelogginglog4jfilenotfoundexception

提问by AndreaNobili

I am absolutly new using log4jand I have the following problem.

我绝对是使用log4j 的新手,但遇到以下问题。

I am trying to print the logging line into a file named log.out.

我正在尝试将日志记录行打印到名为log.out的文件中。

So I create the following log4j.propertiesconfiguration file:

所以我创建了以下log4j.properties配置文件:

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

The problem is that, when I perform my application and when it incurs in a logging operation, something like this:

问题是,当我执行我的应用程序并在日志操作中发生时,如下所示:

logger.debug("INTO main()");

I obtain the following exception into the console (the error message related to the log.outfile is access denied(in italian Accesso negato):

我在控制台中获得以下异常(与log.out文件相关的错误消息是访问被拒绝(意大利语 Accesso negato):

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: \log.out (Accesso negato)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at java.io.FileOutputStream.<init>(Unknown Source)
        at org.apache.log4j.FileAppender.setFile(FileAppender.java:289)
        at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:163)
        at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:2
56)
        at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.j
ava:132)
        at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.j
ava:96)
        at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigura
tor.java:654)

and don't write nothing into my log.outfile (that I have created manually). This log.outfile is at the same level of the performed jar file that represent my application.

并且不要在我的log.out文件(我手动创建的)中写入任何内容。此log.out文件与代表我的应用程序的已执行 jar 文件处于同一级别。

Why? What am I missing? How can I solve this issue?

为什么?我错过了什么?我该如何解决这个问题?

Tnx

田纳西州

采纳答案by Satya Nand kanodia

I think $log is empty and it's trying to create a file on root and you are running program as a normal user. give it a check.

我认为 $log 是空的,它试图在 root 上创建一个文件,而您正在以普通用户身份运行程序。给它一张支票。

回答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 xml config the syntex might be like the following

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

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

If you are using Tomcat as your application server, I hope it will work.

如果您使用 Tomcat 作为您的应用程序服务器,我希望它可以工作。