从日志文件中解析Log4j布局 [英] Parsing Log4j Layouts from Log Files

查看:166
本文介绍了从日志文件中解析Log4j布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何开源工具可以读取所有 Apache Log4j 1.2.x 的布局实现为有意义的东西(例如,Log4j自己的 LogEvent 对象)?到目前为止,我还没有找到一个可以做到这一点的应用程序(包括Chainsaw)。

Is there any open source tool out there that can read all Layout implementations of Apache Log4j 1.2.x into something meaningful (e.g., Log4j's own LogEvent objects)? So far I haven't found an application (Chainsaw included) that can do that.

我正在寻找一些直接的东西,它可以从文本文件(纯文本或XML)转换),给定Java对象,给定文件名和模式参数(例如,PatternLayout中的格式模式)。如果它有一个清晰的API,可以很容易地集成到一个应用程序中,甚至更好,但这不是必需的。

I am looking for something straightforward, that converts from a text file (plain or XML), into a Java object, given the file name and the pattern parameters (e.g., the format pattern in PatternLayout). If it has a clear API that makes it easy to integrate into an application, even better, but this is not necessary.

推荐答案

以下是Chainsaw用于将patternlayouts转换为有用的代码的代码,仅使用log4j API。对于Chainsaw的最新开发人员快照,它用于直接从log4j xml或属性文件fileappender条目构建Chainsaw配置。

Here is the code Chainsaw uses to convert patternlayouts into something useful, using only log4j APIs. In the case of the latest developer snapshot of Chainsaw, it is used to build a Chainsaw config directly from log4j xml or properties file fileappender entries.

使用PatternParser #parse所填充的转换器和字段来执行您想要的操作。 PatternParser类包含在log4j'adts'伴侣中。

Use the converters and fields populated by PatternParser#parse to do what you want. The PatternParser class is included in the log4j 'extras' companion.

来自 http://svn.apache.org/repos/asf/logging/chainsaw/trunk/src/ main / java / org / apache / log4j / chainsaw / LogFilePatternLayoutBuilder.java

public static String getLogFormatFromPatternLayout(String patternLayout) {
    String input = OptionConverter.convertSpecialChars(patternLayout);
    List converters = new ArrayList();
    List fields = new ArrayList();
    Map converterRegistry = null;

    PatternParser.parse(input, converters, fields, converterRegistry, PatternParser.getPatternLayoutRules());
    return getFormatFromConverters(converters);
}

如果你真的想要读取符合特定PatternLayout的日志文件的东西并生成log4j LoggingEvents,请参阅LogFilePatternReceiver。 Chainsaw使用getLogFormatFromPatternLayout方法将patternLayout转换为接收者所需的LogFormat。

If you actually want something that will read a log file conforming to a specific PatternLayout and generate log4j LoggingEvents, see LogFilePatternReceiver. Chainsaw uses the getLogFormatFromPatternLayout method to convert the patternLayout into the LogFormat required by the receiver.

顺便说一句,Chainsaw的开发人员快照包含许多其他新功能 - 如果你想尝试一下,你可以在这里得到它:

By the way, the developer snapshot of Chainsaw contains a lot of additional new features - if you want to try it out, you can get it here:

http://people.apache.org/~sdeboy

这篇关于从日志文件中解析Log4j布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆