Java Code Examples for org.apache.logging.log4j.core.config.AbstractConfiguration#removeAppender()

The following examples show how to use org.apache.logging.log4j.core.config.AbstractConfiguration#removeAppender() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransferTest.java    From logging-log4j-audit with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: TransferTest.java    From logging-log4j-audit with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 3
Source File: Log4J2Appender.java    From javamelody with Apache License 2.0 5 votes vote down vote up
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();
		}
	}
}
 
Example 4
Source File: MCRWebCLIContainer.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
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();
        }
    }
}