错误:使用 log4j 时“setFile(null,false) 调用失败”

2022-09-01 04:39:43

我已经在项目的源文件夹中添加了文件,但我仍然收到log4j:错误。log4j.properties

这是我的Log4j.properties文件:

    .rootCategory=DEBUG, R, O
    # Stdout
    log4j.appender.O=org.apache.log4j.ConsoleAppender
    log4j.appender.O=log44j.log
    # File
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=log4j.log

    # Control the maximum log file size
    log4j.appender.R.MaxFileSize=100KB

    # Archive log files (one backup file here)
    log4j.appender.R.MaxBackupIndex=1

    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.O.layout=org.apache.log4j.PatternLayout

    log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
    log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n

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

    # Define the file appender
    log4j.appender.FILE=org.apache.log4j.FileAppender
    log4j.appender.FILE.File=logs/${file.name}
    log4j.appender.FILE.Append=false

    # Define the layout for file appender
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
    log4j.appender.FILE.layout.conversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

这是我得到的Java异常:

log4j:ERROR setFile(null,false) call failed.
java.io.FileNotFoundException: logs (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:194)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
    at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
    at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:307)
    at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:172)
    at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:104)
    at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:809)
    at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:735)
    at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:615)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:502)
    at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:547)
    at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
    at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
    at org.apache.log4j.Logger.getLogger(Logger.java:104)
    at lib.Dashboard.Reports.<init>(Reports.java:34)
    at testcases.AmazonDashboard.TC_DB17.main(TC_DB17.java:54)
AmazonDashboardTC_DB17Exception in thread "main" java.lang.NullPointerException
    at testcases.AmazonDashboard.TC_DB17.main(TC_DB17.java:131)

请让我知道,如何解决这个异常,因为我已经尝试将我的属性文件放在根文件夹中,现在我已经放在源文件夹中,但在这两种情况下,我都得到了上述异常。


答案 1

我怀疑变量没有被正确替换。因此,的值变为 。因此,Java 会尝试创建一个名为 的日志文件,但它可能是一个现有目录,因此您会收到异常。${file.name}log4j.appender.FILE.Filelogs/logs/

作为快速补救措施,请将设置更改为按绝对路径指向文件,例如 。您不应遇到异常。log4j.appender.FILE.File/tmp/mytest.log

之后,您可以继续调试为什么在运行时环境中未正确替换。${file.name}


答案 2

我有完全相同的问题。以下是对我有用的解决方案:只需以这种方式将您的属性文件路径放在cmd行中即可:

-Dlog4j.configuration=<FILE_PATH>  (ex: log4j.properties)

希望这对您有所帮助