捕获 log4j 输出

Capture log4j output

我们在我们的系统中广泛使用 log4j2,并将其配置为 log4j2.xml

现在我需要一个 运行 jobs 的新应用程序,我想单独捕获时间 X 和 Y 之间产生的所有日志,并将其放入数据库中。来自我们框架的正常日志记录应该像往常一样发生(到文件或任何 log4j2.xml 指向的地方),但是从时间 X 到时间 Y.

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

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

可以使用log4j2 JDBC appender来存储

http://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender

以及时间过滤器select何时记录

http://logging.apache.org/log4j/2.x/manual/filters.html#TimeFilter

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

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方法移除appender后自动执行

下面的代码举例说明了这个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!]