Repository: logging-log4j2
Updated Branches:
  refs/heads/master b50a23787 -> e0260a7c9


LOG4J2-2091: Respect the configured "log4j2.is.webapp" property

Some applications run embedded servlet containers sharing
a single logging context from the root application.


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/b59819e4
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/b59819e4
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/b59819e4

Branch: refs/heads/master
Commit: b59819e47c06d9e928df7e90bdb75189fd9ce306
Parents: b50a237
Author: Carter Kozak <c4kof...@gmail.com>
Authored: Thu Oct 26 11:02:28 2017 -0400
Committer: Mikael Ståldal <mik...@staldal.nu>
Committed: Sun Oct 29 19:32:27 2017 +0100

----------------------------------------------------------------------
 .../log4j/core/impl/Log4jContextFactory.java    | 10 +++--
 .../logging/log4j/core/lookup/Interpolator.java |  3 +-
 .../main/resources/log4j2.component.properties  | 16 --------
 .../apache/logging/log4j/web/PropertyTest.java  | 43 ++++++++++++++++++++
 .../logging/log4j/web/ShutdownPropertyTest.java | 33 ---------------
 5 files changed, 52 insertions(+), 53 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b59819e4/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 f7b4a83..bfd6df2 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
@@ -46,7 +46,8 @@ public class Log4jContextFactory implements 
LoggerContextFactory, ShutdownCallba
 
     private static final StatusLogger LOGGER = StatusLogger.getLogger();
     private static final boolean SHUTDOWN_HOOK_ENABLED =
-        
PropertiesUtil.getProperties().getBooleanProperty(ShutdownCallbackRegistry.SHUTDOWN_HOOK_ENABLED,
 true);
+        
PropertiesUtil.getProperties().getBooleanProperty(ShutdownCallbackRegistry.SHUTDOWN_HOOK_ENABLED,
 true) &&
+                !Constants.IS_WEB_APP;
 
     private final ContextSelector selector;
     private final ShutdownCallbackRegistry shutdownCallbackRegistry;
@@ -120,7 +121,7 @@ public class Log4jContextFactory implements 
LoggerContextFactory, ShutdownCallba
     }
 
     private void initializeShutdownCallbackRegistry() {
-        if (SHUTDOWN_HOOK_ENABLED && this.shutdownCallbackRegistry instanceof 
LifeCycle) {
+        if (isShutdownHookEnabled() && this.shutdownCallbackRegistry 
instanceof LifeCycle) {
             try {
                 ((LifeCycle) this.shutdownCallbackRegistry).start();
             } catch (final IllegalStateException e) {
@@ -314,7 +315,10 @@ public class Log4jContextFactory implements 
LoggerContextFactory, ShutdownCallba
 
     @Override
     public Cancellable addShutdownCallback(final Runnable callback) {
-        return SHUTDOWN_HOOK_ENABLED ? 
shutdownCallbackRegistry.addShutdownCallback(callback) : null;
+        return isShutdownHookEnabled() ? 
shutdownCallbackRegistry.addShutdownCallback(callback) : null;
     }
 
+    public boolean isShutdownHookEnabled() {
+        return SHUTDOWN_HOOK_ENABLED;
+    }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b59819e4/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
index 5259237..84b5e7a 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
@@ -29,6 +29,7 @@ import 
org.apache.logging.log4j.core.config.plugins.util.PluginType;
 import org.apache.logging.log4j.core.util.Loader;
 import org.apache.logging.log4j.core.util.ReflectionUtil;
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.Constants;
 
 /**
  * Proxies all the other {@link StrLookup}s.
@@ -115,7 +116,7 @@ public class Interpolator extends 
AbstractConfigurationAwareLookup {
         }
         lookups.put("date", new DateLookup());
         lookups.put("ctx", new ContextMapLookup());
-        if (Loader.isClassAvailable("javax.servlet.ServletContext")) {
+        if (Constants.IS_WEB_APP) {
             try {
                 lookups.put(LOOKUP_KEY_WEB,
                     
Loader.newCheckedInstanceOf("org.apache.logging.log4j.web.WebLookup", 
StrLookup.class));

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b59819e4/log4j-web/src/main/resources/log4j2.component.properties
----------------------------------------------------------------------
diff --git a/log4j-web/src/main/resources/log4j2.component.properties 
b/log4j-web/src/main/resources/log4j2.component.properties
deleted file mode 100644
index 15884d0..0000000
--- a/log4j-web/src/main/resources/log4j2.component.properties
+++ /dev/null
@@ -1,16 +0,0 @@
-# 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.
-
-log4j.shutdownHookEnabled=false
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b59819e4/log4j-web/src/test/java/org/apache/logging/log4j/web/PropertyTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-web/src/test/java/org/apache/logging/log4j/web/PropertyTest.java 
b/log4j-web/src/test/java/org/apache/logging/log4j/web/PropertyTest.java
new file mode 100644
index 0000000..72fa3a5
--- /dev/null
+++ b/log4j-web/src/test/java/org/apache/logging/log4j/web/PropertyTest.java
@@ -0,0 +1,43 @@
+/*
+ * 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.web;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.impl.Log4jContextFactory;
+import org.apache.logging.log4j.util.Constants;
+import org.apache.logging.log4j.util.PropertiesUtil;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ *
+ */
+public class PropertyTest {
+
+    @Test
+    public void testShutdownHookDisabled() {
+        assertFalse(
+                "Shutdown hook should be disabled by default in web 
applications",
+                ((Log4jContextFactory) 
LogManager.getFactory()).isShutdownHookEnabled());
+    }
+
+    @Test
+    public void testIsWebApp() {
+        assertTrue("When servlet classes are available IS_WEB_APP should 
default to true", Constants.IS_WEB_APP);
+    }
+}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/b59819e4/log4j-web/src/test/java/org/apache/logging/log4j/web/ShutdownPropertyTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-web/src/test/java/org/apache/logging/log4j/web/ShutdownPropertyTest.java
 
b/log4j-web/src/test/java/org/apache/logging/log4j/web/ShutdownPropertyTest.java
deleted file mode 100644
index ec67ee8..0000000
--- 
a/log4j-web/src/test/java/org/apache/logging/log4j/web/ShutdownPropertyTest.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * 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.web;
-
-import org.apache.logging.log4j.util.PropertiesUtil;
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-/**
- *
- */
-public class ShutdownPropertyTest {
-
-    @Test
-    public void testWebProperty() {
-        
assertFalse(PropertiesUtil.getProperties().getBooleanProperty("log4j.shutdownHookEnabled",
 true));
-    }
-}

Reply via email to