下面列出了怎么用org.apache.logging.log4j.core.impl.DefaultLogEventFactory的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Statement apply(final Statement base, final Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
System.setProperty(Constants.LOG4J_LOG_EVENT_FACTORY, TestLogEventFactory.class.getName());
resetLogEventFactory(new TestLogEventFactory());
try {
base.evaluate();
} finally {
System.clearProperty(Constants.LOG4J_LOG_EVENT_FACTORY);
resetLogEventFactory(new DefaultLogEventFactory());
}
}
private void resetLogEventFactory(final LogEventFactory logEventFactory) throws IllegalAccessException {
final Field field = FieldUtils.getField(LoggerConfig.class, "LOG_EVENT_FACTORY", true);
FieldUtils.removeFinalModifier(field, true);
FieldUtils.writeStaticField(field, logEventFactory, false);
}
};
}
/**
* Extension point - let's allow to customize e.g.: LogEvent type or any other param
*
* @param failedPayload payload to be handled
*/
protected void doDeliver(String failedPayload) {
appenderControl.callAppender(DefaultLogEventFactory.getInstance().createEvent(appenderRef.getRef(),
null,
getClass().getName(),
appenderRef.getLevel(),
new SimpleMessage(failedPayload),
null,
null));
}
private LogEvent createTestLogEvent() {
return DefaultLogEventFactory.getInstance().createEvent("testLogger",
null,
getClass().getName(),
Level.INFO,
new SimpleMessage("testMessage"),
null,
null);
}
@Test
public void testRoutingAppenderRoutes() {
final RoutingAppender routingAppender = getRoutingAppender();
assertEquals(expectBindingEntries, routingAppender.getDefaultRouteScript() != null);
assertEquals(expectBindingEntries, routingAppender.getDefaultRoute() != null);
final Routes routes = routingAppender.getRoutes();
Assert.assertNotNull(routes);
Assert.assertNotNull(routes.getPatternScript());
final LogEvent logEvent = DefaultLogEventFactory.getInstance().createEvent("", null, "", Level.ERROR, null,
null, null);
assertEquals("Service2", routes.getPattern(logEvent, new ConcurrentHashMap<>()));
}
@BeforeClass
public static void beforeClass() {
System.setProperty("log4j2.is.webapp", "false");
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigErrorOnFormat.xml");
// Log4jLogEvent.toString invokes message.toString
System.setProperty("log4j2.logEventFactory", DefaultLogEventFactory.class.getName());
}
private DefaultLogEventFactory getLogEventFactory() {
return DefaultLogEventFactory.getInstance();
}