我们在系统中广泛使用log4j2
,并使用log4j2.xml
对其进行配置。
现在我需要一个运行jobs
的新应用程序,我想分别捕获在时间X和Y之间产生的所有日志并将其放入数据库中。我们框架中的正常日志记录应该像往常一样(到文件或log4j2.xml指向的任何地方),但是从时间X到时间Y.
我还希望捕获所有日志记录,最好是一个字符串列表或可以保存在数据库表中的东西。
我的想法是创建一个新的Appender(捕获所有日志记录输出)并动态添加/删除该appender以启动和停止记录?那会有用吗?我也可以在框架类中重新配置记录器吗?
答案 0 :(得分:5)
有可能。您需要创建自定义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);
}
}
每个事件都转换为一个字符串,并添加到列表中。删除appender后会自动执行stop
方法。
以下代码举例说明了这个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!]
答案 1 :(得分:0)
您可以使用log4j2 JDBC appender存储
http://logging.apache.org/log4j/2.x/manual/appenders.html#JDBCAppender
时间过滤器选择何时记录
http://logging.apache.org/log4j/2.x/manual/filters.html#TimeFilter