简体   繁体   English

使用Java创建XML日志文件

[英]Creating an XML Log file in Java

For our J2SE application, we want to create an XML log file. 对于我们的J2SE应用程序,我们要创建一个XML日志文件。 Events will be logged as they happen. 事件将在发生时记录。 There will be perhaps four or five types of events that can happen (for example, key strokes, mouse clicks, and also more application-specific events), each with their own structure. 可能会发生四种或五种类型的事件(例如,击键,鼠标点击以及更多特定于应用程序的事件),每种事件都有自己的结构。 The only attribute that these different event types have in common is a time stamp. 这些不同事件类型的唯一属性是时间戳。

Is there a standard way of doing this? 有这样做的标准方法吗? (I considered using Log4j XMLLayout, but that seems to only record messages as text strings without a structured format.) One thing we are considering using is JAXB. (我考虑过使用Log4j XMLLayout,但这似乎只是将消息记录为没有结构化格式的文本字符串。)我们正在考虑使用的一件事是JAXB。

Would recommend Logback, which gives you full flexibility in formatting (eg via XML) your output. 会推荐使用Logback,它可以为您的输出提供格式化(例如通过XML)的完全灵活性。 I suspect this is the closest tool to what you need - I can't really imagine the XML you'd want to output in being coded in to some library since that's going to be very application-dependent. 我怀疑这是你需要的最接近的工具 - 我无法想象你想要输出的XML被编码到某个库中,因为这将非常依赖于应用程序。 But in any case it's a logging framework that has suited our needs better than log4j and is good to be familiar with. 但无论如何,它是一个日志框架,它比log4j更适合我们的需求,并且很熟悉。

Java includes a significant logging feature described here http://docs.oracle.com/javase/7/docs/technotes/guides/logging/index.html . Java包含一个重要的日志记录功能,如http://docs.oracle.com/javase/7/docs/technotes/guides/logging/index.html所述 You should be able to create a custom Formatter to take your custom LogRecord objects and turn them into appropriate XML. 您应该能够创建自定义Formatter来获取自定义LogRecord对象并将它们转换为适当的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM