1. 程式人生 > >log4j配置檔案定義輸出Appender和Layout

log4j配置檔案定義輸出Appender和Layout



log4j由三個重要的元件構成:

- 日誌資訊的輸出格式(佈局)Layout

- 日誌資訊的輸出目的地 Appender

- 日誌資訊的優先順序 Level

工程中使用配置檔案定義輸出格式Layout和輸出目的地Appender,log4j支援兩種方式的配置方式:

.properties檔案

- xml檔案 

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

先看一個實驗:

一、工程目錄

二、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");
	}
}

四、實驗結果


五、實驗可能的異常

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檔案無法解析。