logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1557479 - in /logging/log4j/log4j2/trunk: log4j-core/src/main/java/org/apache/logging/log4j/core/ log4j-core/src/main/java/org/apache/logging/log4j/core/config/ log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/ log4j-c...
Date Sun, 12 Jan 2014 01:49:26 GMT
Author: rgoers
Date: Sun Jan 12 01:49:25 2014
New Revision: 1557479

URL: http://svn.apache.org/r1557479
Log:
LOG4J2-470 - hostName property was not being set until after the first configuration element.

Added:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/HostNameTest.java
Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
    logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
    logging/log4j/log4j2/trunk/log4j-core/src/test/resources/log4j-test2.xml
    logging/log4j/log4j2/trunk/src/changes/changes.xml

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java
Sun Jan 12 01:49:25 2014
@@ -334,10 +334,9 @@ public class LoggerContext implements or
         }
         final Configuration prev = this.config;
         config.addListener(this);
-        final Map<String, String> map = new HashMap<String, String>();
-        map.put("hostName", NetUtils.getLocalHostname());
-        map.put("contextName", name);
-        config.addComponent(Configuration.CONTEXT_PROPERTIES, map);
+        final ConcurrentMap<String, String> map = config.getComponent(Configuration.CONTEXT_PROPERTIES);
+        map.putIfAbsent("hostName", NetUtils.getLocalHostname());
+        map.putIfAbsent("contextName", name);
         config.start();
         this.config = config;
         updateLoggers();

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
Sun Jan 12 01:49:25 2014
@@ -104,7 +104,9 @@ public class BaseConfiguration extends A
 
     private ConcurrentMap<String, LoggerConfig> loggers = new ConcurrentHashMap<String,
LoggerConfig>();
 
-    private final StrLookup tempLookup = new Interpolator();
+    private ConcurrentMap<String, String> properties = new ConcurrentHashMap<String,
String>();
+
+    private final StrLookup tempLookup = new Interpolator(properties);
 
     private final StrSubstitutor subst = new StrSubstitutor(tempLookup);
 
@@ -120,6 +122,7 @@ public class BaseConfiguration extends A
      * Constructor.
      */
     protected BaseConfiguration() {
+        componentMap.put(Configuration.CONTEXT_PROPERTIES, properties);
         pluginManager = new PluginManager("Core");
         rootNode = new Node();
     }
@@ -127,7 +130,7 @@ public class BaseConfiguration extends A
     @Override
     @SuppressWarnings("unchecked")
     public Map<String, String> getProperties() {
-        return (Map<String, String>) componentMap.get(CONTEXT_PROPERTIES);
+        return properties;
     }
 
     /**

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/config/plugins/PropertiesPlugin.java
Sun Jan 12 01:49:25 2014
@@ -44,7 +44,7 @@ public final class PropertiesPlugin {
     public static StrLookup configureSubstitutor(@PluginElement("Properties") final Property[]
properties,
                                                  @PluginConfiguration final Configuration
config) {
         if (properties == null) {
-            return new Interpolator(null);
+            return new Interpolator(config.getProperties());
         }
         final Map<String, String> map = new HashMap<String, String>(config.getProperties());
 

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
Sun Jan 12 01:49:25 2014
@@ -60,7 +60,14 @@ public class Interpolator implements Str
      * Create the default Interpolator using only Lookups that work without an event.
      */
     public Interpolator() {
-        this.defaultLookup = new MapLookup(new HashMap<String, String>());
+        this((Map<String, String>) null);
+    }
+
+    /**
+     * Create the dInterpolator using only Lookups that work without an event and initial
properties.
+     */
+    public Interpolator(Map<String, String> properties) {
+        this.defaultLookup = new MapLookup(properties == null ? new HashMap<String, String>()
: properties);
         lookups.put("sys", new SystemPropertiesLookup());
         lookups.put("env", new EnvironmentLookup());
         lookups.put("jndi", new JndiLookup());

Modified: logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/main/java/org/apache/logging/log4j/core/web/Log4jWebInitializerImpl.java
Sun Jan 12 01:49:25 2014
@@ -17,12 +17,15 @@
 package org.apache.logging.log4j.core.web;
 
 import java.net.URI;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 import javax.servlet.ServletContext;
 import javax.servlet.UnavailableException;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.helpers.NetUtils;
 import org.apache.logging.log4j.core.impl.ContextAnchor;
 import org.apache.logging.log4j.core.impl.Log4jContextFactory;
 import org.apache.logging.log4j.core.lookup.Interpolator;
@@ -48,7 +51,8 @@ final class Log4jWebInitializerImpl impl
         }
     }
 
-    private final StrSubstitutor substitutor = new StrSubstitutor(new Interpolator());
+    private final Map<String, String> map = new ConcurrentHashMap<String, String>();
+    private final StrSubstitutor substitutor = new StrSubstitutor(new Interpolator(map));
     private final ServletContext servletContext;
 
     private String name;
@@ -60,6 +64,7 @@ final class Log4jWebInitializerImpl impl
 
     private Log4jWebInitializerImpl(final ServletContext servletContext) {
         this.servletContext = servletContext;
+        map.put("hostName", NetUtils.getLocalHostname());
     }
 
     @Override

Added: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/HostNameTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/HostNameTest.java?rev=1557479&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/HostNameTest.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/HostNameTest.java
Sun Jan 12 01:49:25 2014
@@ -0,0 +1,102 @@
+/*
+ * 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.core.appender.RollingFileAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.core.helpers.Constants;
+import org.apache.logging.log4j.core.helpers.NetUtils;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.test.appender.ListAppender;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ *
+ */
+public class HostNameTest {
+
+    private static final String CONFIG = "log4j-test2.xml";
+    private static Configuration config;
+    private static ListAppender app;
+    private static ListAppender host;
+    private static RollingFileAppender hostFile;
+    private static LoggerContext ctx;
+
+    @BeforeClass
+    public static void setupClass() {
+        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
+        ctx = (LoggerContext) LogManager.getContext(false);
+    }
+
+    @AfterClass
+    public static void cleanupClass() {
+        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
+        ctx.reconfigure();
+        StatusLogger.getLogger().reset();
+    }
+
+    @Before
+    public void before() {
+        config = ctx.getConfiguration();
+        for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet())
{
+            if (entry.getKey().equals("List")) {
+                app = (ListAppender) entry.getValue();
+            } else if (entry.getKey().equals("HostTest")) {
+                host = (ListAppender) entry.getValue();
+            } else if (entry.getKey().equals("HostFile")) {
+                hostFile = (RollingFileAppender) entry.getValue();
+            }
+        }
+        assertNotNull("No Host FileAppender", hostFile);
+        app.clear();
+        host.clear();
+    }
+
+
+    @Test
+    public void testHostname() {
+        final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.hosttest");
+        testLogger.debug("Hello, {}", "World");
+        final List<String> msgs = host.getMessages();
+        assertTrue("Incorrect number of events. Expected 1, actual " + msgs.size(), msgs.size()
== 1);
+        String expected = NetUtils.getLocalHostname() + Constants.LINE_SEP;
+        assertTrue("Incorrect hostname - expected " + expected + " actual - " + msgs.get(0),
+            msgs.get(0).endsWith(expected));
+        assertNotNull("No Host FileAppender file name", hostFile.getFileName());
+        expected = "target/" + NetUtils.getLocalHostname() + ".log";
+        String name = hostFile.getFileName();
+        assertTrue("Incorrect HostFile FileAppender file name - expected " + expected + "
actual - " + name,
+            name.equals(expected));
+        name = hostFile.getFilePattern();
+        assertNotNull("No file pattern", name);
+        expected = "target/" + NetUtils.getLocalHostname() + "-%d{MM-dd-yyyy}-%i.log";
+        assertTrue("Incorrect HostFile FileAppender file pattern - expected " + expected
+ " actual - " + name,
+            name.equals(expected));
+
+    }
+}

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
Sun Jan 12 01:49:25 2014
@@ -54,7 +54,6 @@ public class LoggerTest {
     private static Configuration config;
     private static ListAppender app;
     private static ListAppender host;
-    private static RollingFileAppender hostFile;
     private static ListAppender noThrown;
     private static LoggerContext ctx;
 
@@ -81,13 +80,10 @@ public class LoggerTest {
                 host = (ListAppender) entry.getValue();
             } else if (entry.getKey().equals("NoThrowable")) {
                 noThrown = (ListAppender) entry.getValue();
-            } else if (entry.getKey().equals("HostFile")) {
-                hostFile = (RollingFileAppender) entry.getValue();
             }
         }
         assertNotNull("No Appender", app);
         assertNotNull("No Host Appender", host);
-        assertNotNull("No Host FileAppender", hostFile);
         app.clear();
         host.clear();
     }
@@ -186,28 +182,6 @@ public class LoggerTest {
     }
 
     @Test
-    public void testHostname() {
-        final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.hosttest");
-        testLogger.debug("Hello, {}", "World");
-        final List<String> msgs = host.getMessages();
-        assertTrue("Incorrect number of events. Expected 1, actual " + msgs.size(), msgs.size()
== 1);
-        String expected = NetUtils.getLocalHostname() + Constants.LINE_SEP;
-        assertTrue("Incorrect hostname - expected " + expected + " actual - " + msgs.get(0),
-            msgs.get(0).endsWith(expected));
-        assertNotNull("No Host FileAppender file name", hostFile.getFileName());
-        expected = "target/" + NetUtils.getLocalHostname() + ".log";
-        String name = hostFile.getFileName();
-        assertTrue("Incorrect HostFile FileAppender file name - expected " + expected + "
actual - " + name,
-            name.equals(expected));
-        name = hostFile.getFilePattern();
-        assertNotNull("No file pattern", name);
-        expected = "target/" + NetUtils.getLocalHostname() + "-%d{MM-dd-yyyy}-%i.log";
-        assertTrue("Incorrect HostFile FileAppender file pattern - expected " + expected
+ " actual - " + name,
-            name.equals(expected));
-
-    }
-
-    @Test
     public void testImpliedThrowable() {
         final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.hosttest");
         testLogger.debug("This is a test", new Throwable("Testing"));

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/log4j-test2.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/resources/log4j-test2.xml?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/resources/log4j-test2.xml (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/resources/log4j-test2.xml Sun Jan 12 01:49:25
2014
@@ -17,11 +17,6 @@
 
 -->
 <Configuration status="ERROR" name="XMLConfigTest" monitorInterval="5" packages="org.apache.logging.log4j.test">
-  <Properties>
-    <Property name="filename">target/test.log</Property>
-  </Properties>
-  <ThresholdFilter level="trace"/>
-
   <Appenders>
     <RollingFile name="HostFile" fileName="target/${hostName}.log" filePattern="target/${hostName}-%d{MM-dd-yyyy}-%i.log">
       <PatternLayout>
@@ -46,7 +41,7 @@
         <MarkerFilter marker="EXCEPTION" onMatch="ACCEPT" onMismatch="DENY"/>
       </filters>
     </Console>
-    <File name="File" fileName="${filename}">
+    <File name="File" fileName="target/test.log">
       <PatternLayout>
         <Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
       </PatternLayout>

Modified: logging/log4j/log4j2/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/changes/changes.xml?rev=1557479&r1=1557478&r2=1557479&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/changes/changes.xml (original)
+++ logging/log4j/log4j2/trunk/src/changes/changes.xml Sun Jan 12 01:49:25 2014
@@ -21,6 +21,9 @@
   </properties>
   <body>
     <release version="2.0-RC1" date="2013-MM-DD" description="Bug fixes and enhancements">
+      <action issue="LOG4J2-470" dev="rgoers" type="fix">
+        hostName property was not being set until after the first configuration element.
+      </action>
       <action issue="LOG4J2-492" dev="rpopma" type="fix" due-to="Shaddy Baddah">
         (JMX) Fixed MalformedObjectNameException if context name contains '=' character.
       </action>



Mime
View raw message