logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rgo...@apache.org
Subject svn commit: r1496554 - in /logging/log4j/log4j2/trunk: api/src/main/java/org/apache/logging/log4j/ api/src/test/java/org/apache/logging/log4j/ src/site/xdoc/manual/
Date Tue, 25 Jun 2013 17:38:12 GMT
Author: rgoers
Date: Tue Jun 25 17:38:12 2013
New Revision: 1496554

URL: http://svn.apache.org/r1496554
Log:
LOG4J2-294 - LogManager.getLogger can now be called without a logger name or with a null logger
name.

Added:
    logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/LogManagerTest.java
Modified:
    logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java
    logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/TestLoggerContext.java
    logging/log4j/log4j2/trunk/src/site/xdoc/manual/api.xml

Modified: logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java?rev=1496554&r1=1496553&r2=1496554&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/main/java/org/apache/logging/log4j/LogManager.java
Tue Jun 25 17:38:12 2013
@@ -247,7 +247,10 @@ public class LogManager {
      * @see StringFormatterMessageFactory
      */
     public static Logger getFormatterLogger(final Class<?> clazz) {
-        return getLogger(clazz, StringFormatterMessageFactory.INSTANCE);
+        if (clazz != null) {
+            return getLogger(clazz, StringFormatterMessageFactory.INSTANCE);
+        }
+        return getLogger(getClassName(2), StringFormatterMessageFactory.INSTANCE);
     }
 
     /**
@@ -277,7 +280,10 @@ public class LogManager {
      * @see StringFormatterMessageFactory
      */
     public static Logger getFormatterLogger(final Object value) {
-        return getLogger(value, StringFormatterMessageFactory.INSTANCE);
+        if (value != null) {
+            return getLogger(value, StringFormatterMessageFactory.INSTANCE);
+        }
+        return getLogger(getClassName(2), StringFormatterMessageFactory.INSTANCE);
     }
 
     /**
@@ -307,7 +313,10 @@ public class LogManager {
      * @see StringFormatterMessageFactory
      */
     public static Logger getFormatterLogger(final String name) {
-        return getLogger(name, StringFormatterMessageFactory.INSTANCE);
+        if (name != null) {
+            return getLogger(name, StringFormatterMessageFactory.INSTANCE);
+        }
+        return getLogger(getClassName(2), StringFormatterMessageFactory.INSTANCE);
     }
 
     /**
@@ -316,7 +325,7 @@ public class LogManager {
      * @return The Logger.
      */
     public static Logger getLogger(final Class<?> clazz) {
-        return getLogger(clazz != null ? clazz.getName() : null);
+        return getLogger(clazz != null ? clazz.getName() : getClassName(2));
     }
 
     /**
@@ -327,7 +336,7 @@ public class LogManager {
      * @return The Logger.
      */
     public static Logger getLogger(final Class<?> clazz, final MessageFactory messageFactory)
{
-        return getLogger(clazz != null ? clazz.getName() : null, messageFactory);
+        return getLogger(clazz != null ? clazz.getName() : getClassName(2), messageFactory);
     }
 
     /**
@@ -336,7 +345,10 @@ public class LogManager {
      * @return The Logger.
      */
     public static Logger getLogger(final Object value) {
-        return getLogger(value != null ? value.getClass() : null);
+        if (value != null) {
+            return getLogger(value.getClass().getName());
+        }
+        return getLogger(getClassName(2));
     }
 
     /**
@@ -347,7 +359,10 @@ public class LogManager {
      * @return The Logger.
      */
     public static Logger getLogger(final Object value, final MessageFactory messageFactory)
{
-        return getLogger(value != null ? value.getClass() : null, messageFactory);
+        if (value != null) {
+            return getLogger(value.getClass().getName(), messageFactory);
+        }
+        return getLogger(getClassName(2), messageFactory);
     }
 
     /**
@@ -357,7 +372,10 @@ public class LogManager {
      * @return The Logger.
      */
     public static Logger getLogger(final String name) {
-        return factory.getContext(LogManager.class.getName(), null, false).getLogger(name);
+        if (name != null) {
+            return factory.getContext(LogManager.class.getName(), null, false).getLogger(name);
+        }
+        return getLogger(getClassName(2));
     }
 
     /**
@@ -369,7 +387,19 @@ public class LogManager {
      * @return The Logger.
      */
     public static Logger getLogger(final String name, final MessageFactory messageFactory)
{
-        return factory.getContext(LogManager.class.getName(), null, false).getLogger(name,
messageFactory);
+        if (name != null) {
+            return factory.getContext(LogManager.class.getName(), null, false).getLogger(name,
messageFactory);
+        }
+        return getLogger(getClassName(2), messageFactory);
+    }
+
+    public static Logger getLogger() {
+        return getLogger(getClassName(2));
+    }
+
+
+    public static Logger getLogger(final MessageFactory messageFactory) {
+        return getLogger(getClassName(2), messageFactory);
     }
 
     /**
@@ -382,4 +412,8 @@ public class LogManager {
     protected static Logger getLogger(final String fqcn, final String name) {
         return factory.getContext(fqcn, null, false).getLogger(name);
     }
+
+    private static String getClassName(int depth) {
+        return new Throwable().getStackTrace()[depth].getClassName();
+    }
 }

Added: logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/LogManagerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/LogManagerTest.java?rev=1496554&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/LogManagerTest.java
(added)
+++ logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/LogManagerTest.java
Tue Jun 25 17:38:12 2013
@@ -0,0 +1,57 @@
+/*
+ * 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;
+
+import org.apache.logging.log4j.message.ParameterizedMessageFactory;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ *
+ */
+public class LogManagerTest {
+
+    @Test
+    public void testGetLogger() {
+        Logger logger = LogManager.getLogger();
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+        logger = LogManager.getLogger(ParameterizedMessageFactory.INSTANCE);
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+        logger = LogManager.getLogger((Class) null);
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+        logger = LogManager.getLogger((Class) null, ParameterizedMessageFactory.INSTANCE);
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+        logger = LogManager.getLogger((String) null);
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+        logger = LogManager.getLogger((String) null, ParameterizedMessageFactory.INSTANCE);
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+        logger = LogManager.getLogger((Object) null);
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+        logger = LogManager.getLogger((Object) null, ParameterizedMessageFactory.INSTANCE);
+        assertNotNull("No Logger returned", logger);
+        assertTrue("Incorrect Logger name: " + logger.getName(),LogManagerTest.class.getName().equals(logger.getName()));
+    }
+}

Modified: logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/TestLoggerContext.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/TestLoggerContext.java?rev=1496554&r1=1496553&r2=1496554&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/TestLoggerContext.java
(original)
+++ logging/log4j/log4j2/trunk/api/src/test/java/org/apache/logging/log4j/TestLoggerContext.java
Tue Jun 25 17:38:12 2013
@@ -19,14 +19,22 @@ package org.apache.logging.log4j;
 import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.spi.LoggerContext;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  *
  */
 public class TestLoggerContext implements LoggerContext {
-    private final Logger logger = new TestLogger();
+    private Map<String, Logger> map = new HashMap<String, Logger>();
 
     @Override
     public Logger getLogger(final String name) {
+        if (map.containsKey(name)) {
+            return map.get(name);
+        }
+        Logger logger = new TestLogger(name);
+        map.put(name, logger);
         return logger;
     }
 

Modified: logging/log4j/log4j2/trunk/src/site/xdoc/manual/api.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/src/site/xdoc/manual/api.xml?rev=1496554&r1=1496553&r2=1496554&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/src/site/xdoc/manual/api.xml (original)
+++ logging/log4j/log4j2/trunk/src/site/xdoc/manual/api.xml Tue Jun 25 17:38:12 2013
@@ -37,9 +37,9 @@
             <h4>Hello World!</h4>
             <p>
               No introduction would be complete without the customary Hello, World example.
Here is ours. First,
-              a Logger with the name "HelloWorld" is obtained from the 
-              <a href="../log4j-api/apidocs/org/apache/logging/log4j/LogManager.html">LogManager</a>.

-              Next, the logger is used to write the "Hello, World!" message, however the
message will be written 
+              a Logger with the name "HelloWorld" is obtained from the
+              <a href="../log4j-api/apidocs/org/apache/logging/log4j/LogManager.html">LogManager</a>.
+              Next, the logger is used to write the "Hello, World!" message, however the
message will be written
               only if the Logger is configured to allow informational messages.
             </p>
             <pre class="prettyprint linenums">import org.apache.logging.log4j.LogManager;
@@ -77,12 +77,12 @@ public class HelloWorld {
             <h4>Formatting Parameters</h4>
             <p>
               Substituting parameters leaves formatting up to you if <code>toString()</code>
is not what you want.
-              To facilitate formatting, you can use the same format strings as Java's 
+              To facilitate formatting, you can use the same format strings as Java's
               <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Formatter.html#syntax">Formatter</a>.
               For example:
             </p>
             <pre class="prettyprint linenums">public static Logger logger = LogManager.getFormatterLogger("Foo");
-            
+
 logger.debug("Logging in user %s with birthday %s", user.getName(), user.getBirthdayCalendar());
 logger.debug("Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
 logger.debug("Integer.MAX_VALUE = %,d", Integer.MAX_VALUE);
@@ -98,6 +98,29 @@ logger.debug("Long.MAX_VALUE = %,d", Lon
 2012-12-12 11:56:19,643 [main] DEBUG: Integer.MAX_VALUE = 2,147,483,647
 2012-12-12 11:56:19,643 [main] DEBUG: Long.MAX_VALUE = 9,223,372,036,854,775,807
 </pre>
+          <h4>Logger NAmes</h4>
+            <p>
+              Most logging implementations use a hierarchical scheme for matching logger
names with logging
+              configuration. In this scheme the logger name hierarchy is represented by '.'
characters in the
+              logger name, in a fashion very similar to the hierarchy used for Java package
names. For example,
+              org.apache.logging.appender and org.apache.logging.filter both have org.apache.logging
as their parent.
+              In most cases, applications name their loggers by passing the current class's
name to
+              LogManager.getLogger.  Because this usage is so common, Log4j 2 provides that
as the default when
+              the logger name parameter is either omitted or is null. For example, in both
examples below the Logger will
+              have a name of "org.apache.test.MyTest".
+            </p>
+            <pre class="prettyprint linenums">package org.apache.test;
+
+public class MyTest {
+    private static Logger logger = LogManager.getLogger(MyTest.class.getName());
+}
+            </pre>
+            <pre class="prettyprint linenums">package org.apache.test;
+
+              public class MyTest {
+              private static Logger logger = LogManager.getLogger();
+              }
+            </pre>
           </subsection>
         </section>
     </body>



Mime
View raw message