我想将log4j.xml与log4j.xsd(xml架构)连接起来。项目没有任何警告或错误。但当我开始时,我有这样的控制台警告:
log4j:WARN可持续解析错误6和第66列。
log4j:WARN文档根元素“log4j:configuration”必须与DOCTYPE根“null”匹配。
log4j:WARN可持续解析错误6和第66列。
log4j:WARN文档无效:未找到语法。
我认为,架构位置有问题。但我不知道,如何正常写它。 希望你的意见。
我的log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
debug="false"
xsi:schemaLocation="http://www.example.org/log4j log4j.xsd ">
<appender name="logFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="E:/Codes/HorseRacing/logFile.log"/>
<param name="MaxFileSize" value="1MB"/>
<param name="MaxBackupIndex" value="5"/>
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.EnhancedPatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} [%-5p][%-16.16t][%40.40c] - %m%n"/>
</layout>
</appender>
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="Cp866"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} [%-5p][%-25.25l][%10.10c] - %m%n" />
</layout>
</appender>
<logger name="appLogger">
<level value="INFO"/>
<appender-ref ref="logFileAppender"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
</log4j:configuration>
我的log4j.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="configuration">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="appender">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="param">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="value" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="layout">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="param">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="value" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="class" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
<xsd:attribute name="class" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="logger">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="level">
<xsd:complexType>
<xsd:attribute name="value" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element maxOccurs="unbounded" name="appender-ref">
<xsd:complexType>
<xsd:attribute name="ref" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="debug" type="xsd:boolean" />
<xsd:attribute name="schemaLocation" type="xsd:string" />
</xsd:complexType>
</xsd:element>
</xsd:schema>
<logger name="appLogger">
<level value="INFO"/>
<appender-ref ref="logFileAppender"/>
<appender-ref ref="ConsoleAppender"/>
</logger>
</log4j:configuration>
P.S。对不起我的英文......
答案 0 :(得分:14)
在<xml>
元素后面的log4j.xml配置文件中添加以下行:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
Complete log4j Configuration Example from their wiki:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<root>
<priority value ="debug" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
答案 1 :(得分:5)
产生此消息的jar期望看到经过验证的DTD,而不是模式验证的配置。
检查您的类路径。您正在为此配置使用太旧版本的框架。很可能你的磁盘上有一个同名jar的多个版本,这种情况会引发你的问题并删除你真不想使用的非常旧的库。