下面列出了org.apache.logging.log4j.core.config.AbstractConfiguration#removeAppender ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testCustomExceptionHandlerIsPassedToEvent() {
AbstractConfiguration config = setUpFailingAppender();
MutableBoolean exceptionHandled = new MutableBoolean(false);
LogEventFactory.setDefaultHandler((message, ex) -> {
assertThat(ex, instanceOf(LoggingException.class));
exceptionHandled.setTrue();
});
Transfer transfer = setUpMinimumEvent();
transfer.logEvent();
assertTrue("Exception was not handled through the custom handler", exceptionHandled.isTrue());
config.removeAppender(failingAppenderName);
}
@Test
public void testDefaultExceptionHandlerIsInvokedOnEventLogFailure() {
AbstractConfiguration config = setUpFailingAppender();
exception.expect(AuditException.class);
exception.expectCause(isA(LoggingException.class));
exception.expectMessage("Error logging event transfer");
Transfer transfer = setUpMinimumEvent();
try {
transfer.logEvent();
} finally {
config.removeAppender(failingAppenderName);
}
}
void deregister() {
if (LogManager.getContext(false) instanceof LoggerContext) {
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
if (ctx.getConfiguration() instanceof AbstractConfiguration) {
final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
final Appender appender = getSingleton();
appender.stop();
config.removeAppender(appender.getName());
final Logger rootLogger = LogManager.getRootLogger();
final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName());
loggerConfig.removeAppender(appender.getName());
ctx.updateLoggers();
}
}
}
protected boolean processCommands() throws IOException {
final LoggerContext logCtx = (LoggerContext) LogManager.getContext(false);
final AbstractConfiguration logConf = (AbstractConfiguration) logCtx.getConfiguration();
LinkedList<String> failedQueue = new LinkedList<>();
logGrabber.grabCurrentThread();
// start grabbing logs of this thread
logConf.getRootLogger().addAppender(logGrabber, logConf.getRootLogger().getLevel(), null);
// register session to MCRSessionMgr
MCRSessionMgr.setCurrentSession(session);
Optional<HttpSession> httpSession = Optional
.ofNullable((HttpSession) webSocketSession.getUserProperties()
.get(MCRWebsocketDefaultConfigurator.HTTP_SESSION));
int sessionTime = httpSession.map(HttpSession::getMaxInactiveInterval).orElse(-1);
httpSession.ifPresent(s -> s.setMaxInactiveInterval(-1));
try {
while (!commands.isEmpty()) {
String command = commands.remove(0);
cmdListPublisher.submit(commands);
if (!processCommand(command)) {
if (!continueIfOneFails) {
return false;
}
failedQueue.add(command);
}
}
if (failedQueue.isEmpty()) {
setCurrentCommand("");
return true;
} else {
saveQueue(null, failedQueue);
return false;
}
} finally {
// stop grabbing logs of this thread
logGrabber.stop();
logConf.removeAppender(logGrabber.getName());
try {
if (webSocketSession.isOpen()) {
LogManager.getLogger().info("Close session {}", webSocketSession::getId);
webSocketSession.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Done"));
}
} finally {
httpSession.ifPresent(s -> s.setMaxInactiveInterval(sessionTime));
// release session
MCRSessionMgr.releaseCurrentSession();
}
}
}