log4j自定义配置

时间:2014-01-15 17:05:48

标签: java apache logging log4j

我面临以下情况:我们有一个使用标准Apache log4j工具进行日志记录的应用程序。它包含两个类(让我们将它们命名为A& B)。

A.java:

package TestLog4j;

import org.apache.log4j.Logger;

public class A {
    private static Logger logger = Logger.getLogger(A.class);

    public static void main(String[] args) {
        logger.info("This is A");
    }
}

B.java:

package TestLog4j;

import org.apache.log4j.Logger;

public class B {
    private static Logger logger = Logger.getLogger(B.class);

    public static void main(String[] args) {
        logger.info("This is B");
    }
}

我的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="true">

  <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File" value="${app_log_dir}/${process_name}.log" />
    <param name="maxBackupIndex" value="1" />
    <param name="maxFileSize" value="50MB" />
    <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{DATE}] %5p %c{1} - %m%n" /> </layout>
  </appender>

  <logger name="TestLog4j.B">
    <level value="info"/>
    <appender-ref ref="fileAppender" />
  </logger>

  <root>
    <level value="${log4j_loglevel}"></level>
    <appender-ref ref="fileAppender" />
  </root>

</log4j:configuration>

log4j.properties:

app_log_dir=C:\Work\Java\log4j
log4j_loglevel=INFO

log4j.properties.custom

process_name=custom.log

两个类都在同一个jar中:TestLog4j.jar

我正在尝试使用以下命令运行这两个类:

  

java -Druntime_dir =。   -Dlog4j.custom.properties = log4j.properties,log4j.properties.custom -cp .; TestLog4j.jar; log4j-1.2.15.jar TestLog4j.B(或A)

A的输出是:

  

信息:这是A

B的输出是:

  

log4j:ERROR setFile(null,true)调用失败。   java.io.FileNotFoundException:.log(访问被拒绝)          at java.io.FileOutputStream.openAppend(Native Method)          在java.io.FileOutputStream。(未知来源)          在java.io.FileOutputStream。(未知来源)          在org.apache.log4j.FileAppender.setFile(FileAppender.java:290)          在org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:194)          在org.apache.log4j.FileAppender.activateOptions(FileAppender.java:164)          在org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:257)          在org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:285)          at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:171)          在org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:184)          at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:502)          在org.apache.log4j.xml.DOMConfigurator.parseCategory(DOMConfigurator.java:415)          在org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:919)          在org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:790)          在org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:696)          在org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:471)          在org.apache.log4j.LogManager。(LogManager.java:125)          在org.apache.log4j.Logger.getLogger(Logger.java:118)          在TestLog4j.B。(B.java:6)

似乎log4j无法识别第二类的-Dlog4j.custom.properties。为什么会这样?

2 个答案:

答案 0 :(得分:1)

问题已解决。我添加了log4j.custom.properties属性的解析,一切都很顺利。

答案 1 :(得分:0)

你确定INFO:这是A显示? 从上面log4j_loglevel = ERROR for root(包括TestLog4j.A)

由于您已将级别设为ERROR,我的理解是它会忽略 logger.info(“这是A”);

所以有可能它甚至找不到log4j.properties。