log4j xml配置错误

时间:2013-11-01 18:33:29

标签: java xml configuration log4j

每次我用maven编译时都会遇到以下异常。

log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: /logs/jump_page.log (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:136)
    at org.apache.log4j.FileAppender.setFile(FileAppender.java:294)
    at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:165)
    at org.apache.log4j.DailyRollingFileAppender.activateOptions(DailyRollingFileAppender.java:223)
    at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:307)
    at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:295)
    at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:176)
    at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:191)
    at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:523)
    at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:492)
    at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:1006)
    at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:872)
    at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:778)
    at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:526)
    at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
    at org.apache.log4j.Logger.getLogger(Logger.java:104)
    at org.apache.commons.logging.impl.Log4JLogger.getLogger(Log4JLogger.java:289)
    at org.apache.commons.logging.impl.Log4JLogger.<init>(Log4JLogger.java:109)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.apache.commons.logging.impl.LogFactoryImpl.createLogFromClass(LogFactoryImpl.java:1116)
    at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:914)
    at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:604)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:336)
    at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:310)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:91)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.<init>(JUnit4TestSet.java:45)
    at org.apache.maven.surefire.junit4.JUnit4DirectoryTestSuite.createTestSet(JUnit4DirectoryTestSuite.java:56)
    at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.locateTestSets(AbstractDirectoryTestSuite.java:96)
    at org.apache.maven.surefire.Surefire.createSuiteFromDefinition(Surefire.java:209)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:156)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
    at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
log4j:ERROR Either File or DatePattern options are not set for appender [FILE].

我在log4j.xml中有这个配置

<?xml version="1.0" encoding="UTF-8"?>
<!-- TOMCAT SERVER FILE -->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
    debug="false">

    <!-- ============================== -->
    <!-- APPENDERS                      -->
    <!-- ============================== -->

    <appender name="FILE"
        class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="${catalina.base}/logs/jump_page.log" />
        <param name="Append" value="true" />
        <param name="Threshold" value="DEBUG" />
        <param name="DatePattern" value="'.'yyyy-MM-dd" />
        <layout class="org.apache.log4j.PatternLayout">
            <!-- The default pattern: Date Priority [Category] Message\n -->
            <param name="ConversionPattern"
                value="%d %-5p [%c{1}] [%t] [%x] %m%n" />
        </layout>
    </appender>

    <!-- ================================ -->
    <!-- CATEGORIES - APPLICATION LOGGING -->
    <!-- ================================ -->

    <!-- Limit the org.apache.commons category to INFO as its DEBUG is verbose -->
    <category name="net">
        <priority value="WARN" />
    </category>

    <category name="org.apache.commons">
        <priority value="WARN" />
    </category>

    <category name="org.springframework">
        <priority value="WARN" />
    </category>

    <!-- Setup the Travelocity categories -->
    <category name="com.travelocity">
        <priority value="INFO" />
    </category>





    <!-- ============================== -->
    <!-- ROOT CATEGORY                  -->
    <!-- ============================== -->
    <root>
        <priority value="INFO" />
        <appender-ref ref="FILE" />
    </root>

</log4j:configuration>

经过大量搜索,我总能找到关于权限的相同答案,但在这种情况下没有用。

当我使用绝对路径时它会起作用。

Plase,help!

谢谢!

编辑:

我正在使用tomcat 6.0

-bash-3.00$ /opt/tomcat/instances/tomcat-8080/bin/startupDev.sh version
Using CATALINA_BASE:   /opt/tomcat/instances/tomcat-8080
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/instances/tomcat-8080/temp
Using JRE_HOME:       /usr/java

2 个答案:

答案 0 :(得分:2)

${catalina.base}正在解析为空字符串。尝试使用另一个环境变量或绝对路径:

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

答案 1 :(得分:0)

请尝试以下操作进一步诊断此问题,并在问题中报告。告诉我们你在哪个平台上也会有所帮助。

启用调试输出

catalina.properties文件(位于Tomcat安装的conf文件夹或Linux的/etc/tomcat?)中,请附加此属性以启用log4j调试输出。

log4j.debug=true

这应该会给你更多关于出了什么问题的提示。

使用静态和完整路径配置测试文件路径

此外,虽然应该在Tomcat的环境中设置catalina.base系统属性,但log4j是否可能实际尝试从文件系统根目录访问/logs文件夹?如同,不会设置或考虑catalina.base系统属性?如果在配置中放置完整的静态路径,会发生什么?