Log4j 1.x和Log4j 2.x均支持使用XML文件来指定日志记录配置 。 这篇文章探讨了与使用JAXB通过Java类处理这些XML配置文件相关的一些细微差别。 本文中的示例基于Apache Log4j 1.2.17 , Apache Log4j 2.6.2和Java 1.8.0_73(带有JAXB xjc 2.2.8-b130911.1802)。
 Log4j 1.x的XML语法由DTD而不是W3C XML Schema定义 。 幸运的是,JDK附带的JAXB实现提供了一个“实验性的,不受支持的”选项,用于将DTD用作生成Java类的输入。 以下命令可用于对log4j.dtd运行xjc命令行工具 。 
 xjc -p dustin.examples.l4j1 -d src -dtd log4j.dtd 
下一个屏幕快照对此进行了演示。
  
 运行上述命令并在屏幕快照中演示了该命令,这导致在src目录中的Java程序包(称为dustin.examples.l4fj1中生成Java类, dustin.examples.l4fj1允许从兼容log4j.dtd的XML解log4j.dtd并编入log4j.dtd兼容的XML。 
 Log4j 2.x的XML配置可以是“简洁”或“严格”的,在本文中,我需要使用“ strict ”,因为这是使用W3C XML Schema文件Log4j-config.xsd和I定义的语法的形式。 需要一个模式来使用JAXB生成Java类。 可以针对该XML模式运行以下命令,以生成表示Log4j2严格XML的Java类。 
 xjc -p dustin.examples.l4j2 -d src Log4j-config.xsd -b l4j2.jxb 
  
 运行上述命令会导致在src目录中的Java包中(称为dustin.examples.l4j2生成Java类, dustin.examples.l4j2允许从兼容Log4j-config.xsd的XML解dustin.examples.l4j2 ,并允许封装到Log4j-config.xsd XML 。 
 在前面的示例中,我包括一个带有选项-b的JAXB绑定文件 ,后跟该绑定文件的名称( -b l4j2.jxb )。 需要使用此绑定来避免错误,该错误阻止xjc生成带有错误消息“属性“值”的Log4j 2.x兼容Java类。 使用<jaxb:property>解决此冲突。” 已在百慕大的 “ 属性”值“属性”中的“ 英国人”中讨论了此问题及其解决方法。 用于解决此冲突 。 接下来显示我在这里使用的JAXB绑定文件的源。 
l4j2.jxb
<jxb:bindings version="2.0"xmlns:jxb="http://java.sun.com/xml/ns/jaxb"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><jxb:bindings schemaLocation="Log4j-config.xsd" node="/xsd:schema"><jxb:bindings node="//xsd:complexType[@name='KeyValuePairType']"><jxb:bindings node=".//xsd:attribute[@name='value']"><jxb:property name="pairValue"/></jxb:bindings></jxb:bindings></jxb:bindings>
</jxb:bindings> 刚刚显示的JAXB绑定文件允许xjc成功解析XSD并生成Java类。 要付出的一小笔代价(除了编写和引用绑定文件之外)是,将需要在Java类中将KeyValuePairType的“ value”属性作为名为pairValue而不是value的字段进行访问。 
解组
 使用Log4j 1.x的log4j.dtd和Log4j 2.x的Log-config.xsd JAXB生成的类的潜在用例是将 Log4j 1.x XML配置文件转换为Log4j 2.x “严格” XML配置文件 。 在这种情况下,将需要解组log4j.dtd Log4j 1.x和log4j.dtd的XML,并解组log4j.dtd Log4j 2.x和Log4j-config.xsd的XML。 
下面的代码清单演示了如何使用先前生成的JAXB类解组Log4j 1.x XML。
/*** Extract the contents of the Log4j 1.x XML configuration file* with the provided path/name.** @param log4j1XmlFileName Path/name of Log4j 1.x XML config file.* @return Contents of Log4j 1.x configuration file.* @throws RuntimeException Thrown if exception occurs that prevents*    extracting contents from XML with provided name.*/public Log4JConfiguration readLog4j1Config(final String log4j1XmlFileName)throws RuntimeException{Log4JConfiguration config;try{final File inputFile = new File(log4j1XmlFileName);if (!inputFile.isFile()){throw new RuntimeException(log4j1XmlFileName + " is NOT a parseable file.");}final SAXParserFactory spf = SAXParserFactory.newInstance();final SAXParser sp = spf.newSAXParser();final XMLReader xr = sp.getXMLReader();final JAXBContext jaxbContext = JAXBContext.newInstance("dustin.examples.l4j1");final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();final UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();xr.setContentHandler(unmarshallerHandler);final FileInputStream xmlStream = new FileInputStream(log4j1XmlFileName);final InputSource xmlSource = new InputSource(xmlStream);xr.parse(xmlSource);final Object unmarshalledObject = unmarshallerHandler.getResult();config = (Log4JConfiguration) unmarshalledObject;}catch (JAXBException | ParserConfigurationException | SAXException | IOException exception){throw new RuntimeException("Unable to read from file " + log4j1XmlFileName + " - " + exception,exception);}return config;} 由于log4j.dtd的名称空间处理的性质,因此将Log4j 1.x XML解组比某些XML解组要复杂一些。 在Gik的没有名称空间的Jaxb UnMarshall和Deepa S的 “ 如何指示JAXB忽略命名空间 ”中描述了这种处理皱纹的方法。 使用这种方法有助于避免错误消息: 
 
 UnmarshalException:意外元素(uri:“ http://jakarta.apache.org/log4j/”,local:“ configuration”)。 预期要素... 
 
 为了解组我在本例中引用log4j.dtd的Log4j 1.x,在使用Java 8运行此代码时,我需要为Java启动器提供一个特殊的Java系统属性。 
 -Djavax.xml.accessExternalDTD=all 
 为避免出现错误消息,“由于accessExternalDTD属性设置的限制,由于不允许'文件'访问,因此无法读取外部DTD。” 可以在NetBeans的FaqWSDLExternalSchema Wiki页面上找到有关此内容的其他详细信息。 
如下面的示例代码所示,使用JAXB生成的Java类编组Log4j 2.x XML非常简单。
/*** Write Log4j 2.x "strict" XML configuration to file with* provided name based on provided content.** @param log4j2Configuration Content to be written to Log4j 2.x*    XML configuration file.* @param log4j2XmlFile File to which Log4j 2.x "strict" XML*    configuration should be written.*/public void writeStrictLog4j2Config(final ConfigurationType log4j2Configuration,final String log4j2XmlFile){try (final OutputStream os = new FileOutputStream(log4j2XmlFile)){final JAXBContext jc = JAXBContext.newInstance("dustin.examples.l4j2");final Marshaller marshaller = jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(new ObjectFactory().createConfiguration(log4j2Configuration), os);}catch (JAXBException | IOException exception){throw new RuntimeException("Unable to write Log4 2.x XML configuration - " + exception,exception);}} 在这种编组情况下,有一个微妙之处在刚刚显示的代码清单中可能并不明显。 从Log4j-config.xsd生成的JAXB xjc的类缺少任何带有@XmlRootElement的类。 从Log4j 1.x log4j.dtd生成的JAXB类确实包含带有此@XmlRootElement批注的类。 由于基于Log4j 2.x Log4j-config.xsd的Java类没有此注释,因此在尝试直接封送ConfigurationType实例时会发生以下错误: 
MarshalException –带有链接的异常:[com.sun.istack.internal.SAXException2:由于缺少@XmlRootElement批注,因此无法将类型为“ dustin.examples.l4j2.ConfigurationType”的元素封送为元素]
 为避免此错误,我改编了上述代码清单的第18行new ObjectFactory().createConfiguration(ConfigurationType) ,将传入的ConfigurationType实例上调用new ObjectFactory().createConfiguration(ConfigurationType)的结果编组起来,现在已将其成功编组。 
结论
 JAXB可用于从Log4j 1.x的log4j.dtd和Log4j 2.x的Log4j-config.xsd生成Java类,但是与此过程相关联的一些细微差别可以成功地生成这些Java类并使用生成的Java封送和封送XML的类。 
翻译自: https://www.javacodegeeks.com/2016/07/jaxb-log4j-xml-configuration-files.html