log4j配置文件定义输出Appender和Layout

时间:2022-11-22 19:56:22


log4j由三个重要的组件构成:

- 日志信息的输出格式(布局)Layout

- 日志信息的输出目的地 Appender

- 日志信息的优先级 Level


工程中使用配置文件定义输出格式Layout和输出目的地Appender,log4j支持两种方式的配置方式:

.properties文件

- xml文件 

但是工程中最常用的方式还是xml方式的配置。

先看一个实验:

一、工程目录

log4j配置文件定义输出Appender和Layout

二、conf/xmllog4jconfig1.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="appender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%p|%c{1}] %m%n" />
</layout>
</appender>

<root>
<level value="DEBUG" />
<appender-ref ref="appender" />
</root>

</log4j:configuration>

三、Log4jTest.java

package jijian.test;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class Log4jTest {
static Logger logger = Logger.getLogger(Log4jTest.class);
public static void main(String[] args) {

DOMConfigurator.configure("conf/xmllog4jconfig1.xml");
logger.debug("Here is some DEBUG");
logger.info("Here is some INFO");
logger.warn("Here is some WARN");
logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL");
}
}

四、实验结果

log4j配置文件定义输出Appender和Layout

五、实验可能的异常

1、配置文件的路径要正确,默认搜索路径限于Project1目录,不搜索其子目录

若java文件里写为 DOMConfigurator.configure("xmllog4jconfig1.xml"); 则异常如下

log4j:ERROR Could not parse file [xmllog4jconfig1.xml].
java.io.FileNotFoundException: D:\Workspace\Project1\xmllog4jconfig1.xml (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:871)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:896)
at jijian.test.Log4jTest.main(Log4jTest.java:10)
log4j:WARN No appenders could be found for logger (jijian.test.Log4jTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

2、xml文件格式要正确,开始 <?xml version="1.0" encoding="UTF-8" ?> 一定要顶行顶格写,前边不能有任何东西

若xml文件里写为

<!-- 此处不能有数据 -->
<?xml version="1.0" encoding="UTF-8" ?>

则异常如下

log4j:WARN Fatal parsing error 2 and column 6
log4j:WARN The processing instruction target matching "[xX][mM][lL]" is not allowed.
log4j:ERROR Could not parse file [conf/xmllog4jconfig1.xml].
org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:239)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:208)
at org.apache.log4j.xml.DOMConfigurator$1.parse(DOMConfigurator.java:749)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:871)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:755)
at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.java:896)
at jijian.test.Log4jTest.main(Log4jTest.java:10)
log4j:WARN No appenders could be found for logger (jijian.test.Log4jTest).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.


 可以清楚的看出出错指示是:指令格式不符合标准,导致xml文件无法解析。