如何将jaxb打印到记录器中的java对象
您好,通过下面的链接,我能够将JAXB转换为java对象,并使用下面的语句在控制台中打印它 但我想在logger中打印输出 例如,log.info(客户)或log.debug(客户) 我正在使用ApacheLog4J。有人知道下面一个可能的方法吗如何将jaxb打印到记录器中的java对象,java,apache,jakarta-ee,logging,jaxb,Java,Apache,Jakarta Ee,Logging,Jaxb,您好,通过下面的链接,我能够将JAXB转换为java对象,并使用下面的语句在控制台中打印它 但我想在logger中打印输出 例如,log.info(客户)或log.debug(客户) 我正在使用ApacheLog4J。有人知道下面一个可能的方法吗 Customer customer = new Customer(); //set customer attributes JAXBContext jc = JAXBContext.newInstance(Customer.class); Mars
Customer customer = new Customer();
//set customer attributes
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jc.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(customer, stringWriter );
log.info(stringWriter.toString());
将其写入
StringWriter
和日志StringWriter.toString()
。谢谢。它工作得很好。StringWriter中是否有漂亮的打印选项,因为它现在打印在一条水平行中。您应该添加此行来格式化输出marshaller.setProperty(marshaller.JAXB\u FORMATTED\u output,true)你太棒了。谢谢你。
Customer customer = new Customer();
//set customer attributes
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Marshaller marshaller = jc.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(customer, stringWriter );
log.info(stringWriter.toString());