LOG4J2-1222 - Cause LoggerContext creation to fail if shutting down
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/37b594c4 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/37b594c4 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/37b594c4 Branch: refs/heads/master Commit: 37b594c4b6934df094dcc6c642f37c45eefb8990 Parents: 18f108d Author: Ralph Goers <[email protected]> Authored: Sat Jan 23 22:41:45 2016 -0700 Committer: Ralph Goers <[email protected]> Committed: Sun Jan 24 00:38:24 2016 -0700 ---------------------------------------------------------------------- .../log4j/core/impl/Log4jContextFactory.java | 5 +- .../util/DefaultShutdownCallbackRegistry.java | 3 + .../logging/log4j/core/Log4j1222Test.java | 63 ++++++++++++++++++++ .../org/apache/logging/slf4j/Log4j1222Test.java | 59 ++++++++++++++++++ 4 files changed, 129 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/37b594c4/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java index eaef598..c65a64f 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jContextFactory.java @@ -119,7 +119,10 @@ public class Log4jContextFactory implements LoggerContextFactory, ShutdownCallba if (SHUTDOWN_HOOK_ENABLED && this.shutdownCallbackRegistry instanceof LifeCycle) { try { ((LifeCycle) this.shutdownCallbackRegistry).start(); - } catch (final Exception e) { + } catch (final IllegalStateException e) { + LOGGER.error("Cannot start ShutdownCallbackRegistry, already shutting down."); + throw e; + } catch (final RuntimeException e) { LOGGER.error("There was an error starting the ShutdownCallbackRegistry.", e); } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/37b594c4/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java index cf1b0ba..196f2c8 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DefaultShutdownCallbackRegistry.java @@ -127,6 +127,9 @@ public class DefaultShutdownCallbackRegistry implements ShutdownCallbackRegistry try { addShutdownHook(threadFactory.newThread(this)); state.set(State.STARTED); + } catch (final IllegalStateException ex) { + state.set(State.STOPPED); + throw ex; } catch (final Exception e) { LOGGER.catching(e); state.set(State.STOPPED); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/37b594c4/log4j-core/src/test/java/org/apache/logging/log4j/core/Log4j1222Test.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/Log4j1222Test.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/Log4j1222Test.java new file mode 100644 index 0000000..7e88f77 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/Log4j1222Test.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.log4j.core; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.TestLogger; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * Tests logging during shutdown. + */ +public class Log4j1222Test +{ + + @Test + public void homepageRendersSuccessfully() + { + System.setProperty("log4j.configurationFile", "log4j2-console.xml"); + Runtime.getRuntime().addShutdownHook(new ShutdownHook()); + } + + private static class ShutdownHook extends Thread { + + private static class Holder { + private static final Logger LOGGER = LogManager.getLogger(Log4j1222Test.class); + } + + @Override + public void run() + { + super.run(); + trigger(); + } + + private void trigger() { + Holder.LOGGER.info("Attempt to trigger"); + assertTrue("Logger is of type " + Holder.LOGGER.getClass().getName(), Holder.LOGGER instanceof TestLogger); + if (((TestLogger) Holder.LOGGER).getEntries().size() == 0) { + System.out.println("Logger contains no messages"); + } + for (String msg : ((TestLogger) Holder.LOGGER).getEntries()) { + System.out.println(msg); + } + } + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/37b594c4/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java ---------------------------------------------------------------------- diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java new file mode 100644 index 0000000..019660e --- /dev/null +++ b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java @@ -0,0 +1,59 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache license, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the license for the specific language governing permissions and + * limitations under the license. + */ +package org.apache.logging.slf4j; + + +import org.apache.logging.log4j.TestLogger; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.junit.Assert.*; + +/** + * Tests logging during shutdown. + */ +public class Log4j1222Test +{ + + @Test + public void homepageRendersSuccessfully() + { + System.setProperty("log4j.configurationFile", "log4j2-console.xml"); + Runtime.getRuntime().addShutdownHook(new ShutdownHook()); + } + + private static class ShutdownHook extends Thread { + + private static class Holder { + private static final Logger LOGGER = LoggerFactory.getLogger(Log4j1222Test.class); + } + + @Override + public void run() + { + super.run(); + trigger(); + } + + private void trigger() { + Holder.LOGGER.info("Attempt to trigger"); + assertTrue("Logger is of type " + Holder.LOGGER.getClass().getName(), Holder.LOGGER instanceof Log4jLogger); + + } + } +}
