捕获log4j输出 [英] Capture log4j output

查看:84
本文介绍了捕获log4j输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在系统中广泛使用log4j2,并使用log4j2.xml对其进行配置.

We are using log4j2 extensively in our system, and configures it with log4j2.xml.

现在,我需要一个运行jobs的新应用,并且我想分别捕获时间X和Y之间生成的所有日志,并将其放入数据库中.从我们的框架进行的正常日志记录应该照常发生(到文件或log4j2.xml指向的任何地方),但是从时间X到时间Y.

Now I need a new app that run jobs, and I want to separately capture all the logs resulted between time X and Y and put it in a database. Normal logging from our framework should occur as usual (to files or wherever log4j2.xml points to), But from time X to time Y.

我还希望捕获所有日志记录,最好是捕获到字符串列表或可以保存在数据库表中的内容.

I also want all the logging should be captured, preferably to a list of strings or something that can be saved in a database table.

我的想法是创建一个新的Appender(捕获所有日志输出)并动态添加/删除该附加程序以启动和停止日志?那行得通吗?我也可以在框架类中重新配置记录器吗?

My idea is to create a new Appender (that captures all logging output) and dynamically add/remove that appender to start and stop logging? Would that work? Can I reconfigure the loggers in the framework classes too?

推荐答案

有可能.您需要创建一个自定义附加程序.例如:

It is possible. You need to create a custom appender. e.g.:

public class CustomAppender extends AbstractAppender {

    private List<String> list = new ArrayList<>();

    public CustomAppender(String name, Filter filter, Layout<? extends Serializable> layout) {
        super(name, filter, layout);
    }

    public CustomAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions) {
        super(name, filter, layout, ignoreExceptions);
    }

    @Override
    public void append(LogEvent event) {
        byte[] data = getLayout().toByteArray(event);
        list.add(new String(data).trim()); // optional trim
    }

    @Override
    public void stop() {
        // Write to the database
        System.out.println(list);
    }

}

每个事件都转换为一个字符串,该字符串将添加到列表中. stop方法是在删除附加程序后自动执行的.

Each event is converted to a string, which is added to the list. The stop method is automatically executed after removing the appender.

以下代码示例了此附加器的使用.

The following code exemplifies the use of this appender.

public static void main(String[] args) {
    // Execute some jobs
    for (int n = 0; n < 10; n++) {
        dummyJob(n);
    }
}

private static void dummyJob(int n) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();

    // Create and add the appender
    CustomAppender appender = new CustomAppender("Custom", null, PatternLayout.createDefaultLayout());
    appender.start();
    config.addAppender(appender);

    // Create and add the logger
    AppenderRef[] refs = new AppenderRef[]{AppenderRef.createAppenderRef("Custom", null, null)};
    LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "com.company", "true", refs, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger("com.company", loggerConfig);
    ctx.updateLoggers();

    // Run the job
    Logger logger = LogManager.getLogger("com.company");
    logger.info("Job {}", n);
    logger.info("Hello, World!");
    logger.info("This is awesome!");
    logger.info("Hope it works!");
    logger.info("Hope it helps!");

    // Remove the logger and appender
    config.removeLogger("com.company");
    config.removeAppender("Custom");
    ctx.updateLoggers();

}

后者的输出:

[Job 0, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 1, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 2, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 3, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 4, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 5, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 6, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 7, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 8, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]
[Job 9, Hello, World!, This is awesome!, Hope it works!, Hope it helps!]

这篇关于捕获log4j输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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