You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rp...@apache.org on 2015/09/10 20:14:23 UTC

logging-log4j2 git commit: LOG4J2-1118 Lambda support for custom log levels: update Logger wrapper generator tool

Repository: logging-log4j2
Updated Branches:
  refs/heads/master 9180ad1c5 -> 278015d7d


LOG4J2-1118 Lambda support for custom log levels: update Logger wrapper
generator tool

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

Branch: refs/heads/master
Commit: 278015d7d9f8ab85fa25508abd64d994958c9eb2
Parents: 9180ad1
Author: rpopma <rp...@apache.org>
Authored: Fri Sep 11 03:14:16 2015 +0900
Committer: rpopma <rp...@apache.org>
Committed: Fri Sep 11 03:14:16 2015 +0900

----------------------------------------------------------------------
 .../logging/log4j/core/tools/Generate.java      | 140 +++++++++++++++++++
 .../core/tools/GenerateCustomLoggerTest.java    |  15 +-
 .../core/tools/GenerateExtendedLoggerTest.java  |  14 ++
 src/changes/changes.xml                         |   3 +
 src/site/xdoc/manual/customloglevels.xml.vm     |  15 +-
 5 files changed, 184 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278015d7/log4j-core/src/main/java/org/apache/logging/log4j/core/tools/Generate.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/tools/Generate.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/tools/Generate.java
index 6185562..ac62c51 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/tools/Generate.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/tools/Generate.java
@@ -61,6 +61,8 @@ public final class Generate {
                         + "import org.apache.logging.log4j.message.MessageFactory;%n" //
                         + "import org.apache.logging.log4j.spi.AbstractLogger;%n" //
                         + "import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;%n" //
+                        + "import org.apache.logging.log4j.util.MessageSupplier;%n" //
+                        + "import org.apache.logging.log4j.util.Supplier;%n"
                         + "%n";
             }
 
@@ -104,6 +106,8 @@ public final class Generate {
                         + "import org.apache.logging.log4j.message.MessageFactory;%n" //
                         + "import org.apache.logging.log4j.spi.AbstractLogger;%n" //
                         + "import org.apache.logging.log4j.spi.ExtendedLoggerWrapper;%n" //
+                        + "import org.apache.logging.log4j.util.MessageSupplier;%n" //
+                        + "import org.apache.logging.log4j.util.Supplier;%n"
                         + "%n";
             }
 
@@ -396,6 +400,142 @@ public final class Generate {
             + "     */%n" //
             + "    public void methodName(final String message, final Throwable t) {%n" //
             + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, t);%n" //
+            + "    }%n"
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message which is only to be constructed if the logging level is the {@code CUSTOM_LEVEL}"//
+            + "level.%n" //
+            + "     *%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message;%n" //
+            + "     *            the format depends on the message factory.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final Supplier<?> msgSupplier) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, (Throwable) null);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" //
+            + "     * level) including the stack trace of the {@link Throwable} <code>t</code> passed as parameter.%n"//
+            + "     *%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message;%n" //
+            + "     *            the format depends on the message factory.%n" //
+            + "     * @param t the exception to log, including its stack trace.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final Supplier<?> msgSupplier, final Throwable t) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, t);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message which is only to be constructed if the logging level is the%n" //
+            + "     * {@code CUSTOM_LEVEL} level with the specified Marker.%n" //
+            + "     *%n" //
+            + "     * @param marker the marker data specific to this log statement%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message;%n" //
+            + "     *            the format depends on the message factory.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final Marker marker, final Supplier<?> msgSupplier) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, (Throwable) null);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message with parameters which are only to be constructed if the logging level is the%n" //
+            + "     * {@code CUSTOM_LEVEL} level.%n" //
+            + "     *%n" //
+            + "     * @param marker the marker data specific to this log statement%n" //
+            + "     * @param message the message to log; the format depends on the message factory.%n" //
+            + "     * @param paramSuppliers An array of functions, which when called, produce the desired log" //
+            + " message parameters.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final Marker marker, final String message, final Supplier<?>..." //
+            + " paramSuppliers) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, message, paramSuppliers);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" //
+            + "     * level) with the specified Marker and including the stack trace of the {@link Throwable}%n" //
+            + "     * <code>t</code> passed as parameter.%n"
+            + "     *%n" //
+            + "     * @param marker the marker data specific to this log statement%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message;%n" //
+            + "     *            the format depends on the message factory.%n" //
+            + "     * @param t A Throwable or null.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, t);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message with parameters which are only to be constructed if the logging level is%n" //
+            + "     * the {@code CUSTOM_LEVEL} level.%n" //
+            + "     *%n" //
+            + "     * @param message the message to log; the format depends on the message factory.%n" //
+            + "     * @param paramSuppliers An array of functions, which when called, produce the desired log" //
+            + " message parameters.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final String message, final Supplier<?>... paramSuppliers) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, message, paramSuppliers);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message which is only to be constructed if the logging level is the%n" //
+            + "     * {@code CUSTOM_LEVEL} level with the specified Marker. The {@code MessageSupplier} may or may%n" //
+            + "     * not use the {@link MessageFactory} to construct the {@code Message}.%n" //
+            + "     *%n" //
+            + "     * @param marker the marker data specific to this log statement%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final Marker marker, final MessageSupplier msgSupplier) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, (Throwable) null);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" //
+            + "     * level) with the specified Marker and including the stack trace of the {@link Throwable}%n" //
+            + "     * <code>t</code> passed as parameter. The {@code MessageSupplier} may or may not use the%n" //
+            + "     * {@link MessageFactory} to construct the {@code Message}.%n"
+            + "     *%n" //
+            + "     * @param marker the marker data specific to this log statement%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message.%n" //
+            + "     * @param t A Throwable or null.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final Marker marker, final MessageSupplier msgSupplier, final " //
+            + "Throwable t) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, marker, msgSupplier, t);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message which is only to be constructed if the logging level is the%n" //
+            + "     * {@code CUSTOM_LEVEL} level. The {@code MessageSupplier} may or may not use the%n" //
+            + "     * {@link MessageFactory} to construct the {@code Message}.%n"
+            + "     *%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final MessageSupplier msgSupplier) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, (Throwable) null);%n" //
+            + "    }%n" //
+            + "%n" //
+            + "    /**%n" //
+            + "     * Logs a message (only to be constructed if the logging level is the {@code CUSTOM_LEVEL}%n" //
+            + "     * level) including the stack trace of the {@link Throwable} <code>t</code> passed as parameter.%n"//
+            + "     * The {@code MessageSupplier} may or may not use the {@link MessageFactory} to construct the%n" //
+            + "     * {@code Message}.%n"
+            + "     *%n" //
+            + "     * @param msgSupplier A function, which when called, produces the desired log message.%n" //
+            + "     * @param t the exception to log, including its stack trace.%n" //
+            + "     * @since 2.4%n" //
+            + "     */%n" //
+            + "    public void methodName(final MessageSupplier msgSupplier, final Throwable t) {%n" //
+            + "        logger.logIfEnabled(FQCN, CUSTOM_LEVEL, null, msgSupplier, t);%n" //
             + "    }%n";
 
     private Generate() {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278015d7/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateCustomLoggerTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateCustomLoggerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateCustomLoggerTest.java
index 5f6bf4d..0642253 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateCustomLoggerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateCustomLoggerTest.java
@@ -37,9 +37,10 @@ import javax.tools.ToolProvider;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Marker;
 import org.apache.logging.log4j.TestLogger;
-import org.apache.logging.log4j.core.tools.Generate;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.MessageFactory;
+import org.apache.logging.log4j.util.MessageSupplier;
+import org.apache.logging.log4j.util.Supplier;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -117,6 +118,18 @@ public class GenerateCustomLoggerTest {
             cls.getDeclaredMethod(name, String.class, Throwable.class);
             cls.getDeclaredMethod(name, String.class, Object[].class);
             cls.getDeclaredMethod(name, Marker.class, String.class, Object[].class);
+
+            // 2.4 lambda support
+            cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class);
+            cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class, Throwable.class);
+            cls.getDeclaredMethod(name, Marker.class, String.class, Supplier[].class);
+            cls.getDeclaredMethod(name, Marker.class, Supplier.class);
+            cls.getDeclaredMethod(name, Marker.class, Supplier.class, Throwable.class);
+            cls.getDeclaredMethod(name, MessageSupplier.class);
+            cls.getDeclaredMethod(name, MessageSupplier.class, Throwable.class);
+            cls.getDeclaredMethod(name, String.class, Supplier[].class);
+            cls.getDeclaredMethod(name, Supplier.class);
+            cls.getDeclaredMethod(name, Supplier.class, Throwable.class);
         }
 
         // now see if it actually works...

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278015d7/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateExtendedLoggerTest.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateExtendedLoggerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateExtendedLoggerTest.java
index a893c78..ccf12d0 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateExtendedLoggerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/tools/GenerateExtendedLoggerTest.java
@@ -41,6 +41,8 @@ import org.apache.logging.log4j.core.tools.Generate;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.spi.ExtendedLogger;
+import org.apache.logging.log4j.util.MessageSupplier;
+import org.apache.logging.log4j.util.Supplier;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -117,6 +119,18 @@ public class GenerateExtendedLoggerTest {
             cls.getDeclaredMethod(name, String.class, Throwable.class);
             cls.getDeclaredMethod(name, String.class, Object[].class);
             cls.getDeclaredMethod(name, Marker.class, String.class, Object[].class);
+
+            // 2.4 lambda support
+            cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class);
+            cls.getDeclaredMethod(name, Marker.class, MessageSupplier.class, Throwable.class);
+            cls.getDeclaredMethod(name, Marker.class, String.class, Supplier[].class);
+            cls.getDeclaredMethod(name, Marker.class, Supplier.class);
+            cls.getDeclaredMethod(name, Marker.class, Supplier.class, Throwable.class);
+            cls.getDeclaredMethod(name, MessageSupplier.class);
+            cls.getDeclaredMethod(name, MessageSupplier.class, Throwable.class);
+            cls.getDeclaredMethod(name, String.class, Supplier[].class);
+            cls.getDeclaredMethod(name, Supplier.class);
+            cls.getDeclaredMethod(name, Supplier.class, Throwable.class);
         }
 
         // now see if it actually works...

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278015d7/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6d1d57e..f693302 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -37,6 +37,9 @@
         Added support for Java 8 lambda expressions to lazily construct a log message only if
               the requested log level is enabled.
       </action>
+      <action issue="LOG4J2-1118" dev="rpopma" type="add">
+        Updated Logger wrapper generator tool to add Java 8 lambda support for custom log levels.
+      </action>
       <action issue="LOG4J2-1107" dev="ggregory" type="add" due-to="Mikael Ståldal">
         New Appender for Apache Kafka.
       </action>

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/278015d7/src/site/xdoc/manual/customloglevels.xml.vm
----------------------------------------------------------------------
diff --git a/src/site/xdoc/manual/customloglevels.xml.vm b/src/site/xdoc/manual/customloglevels.xml.vm
index a08ca3e..00f6668 100644
--- a/src/site/xdoc/manual/customloglevels.xml.vm
+++ b/src/site/xdoc/manual/customloglevels.xml.vm
@@ -169,7 +169,7 @@ logger.log(Level.getLevel("FORGOT_TO_DEFINE"), "some message"); // throws except
             <p>
               The built-in log levels have a set of convenience methods on the Logger
               interface that makes them easier to use. For example, the Logger interface
-              has fourteen <tt>debug()</tt> methods that support the DEBUG level:
+              has 24 <tt>debug()</tt> methods that support the DEBUG level:
             </p>
             <pre class="prettyprint">
 // convenience methods for the built-in DEBUG level
@@ -186,7 +186,18 @@ debug(Object)
 debug(Object, Throwable)
 debug(String)
 debug(String, Object...)
-debug(String, Throwable)</pre>
+debug(String, Throwable)
+// lambda support methods added in 2.4
+debug(Marker, MessageSupplier)
+debug(Marker, MessageSupplier, Throwable)
+debug(Marker, String, Supplier<?>...)
+debug(Marker, Supplier<?>)
+debug(Marker, Supplier<?>, Throwable)
+debug(MessageSupplier)
+debug(MessageSupplier, Throwable)
+debug(String, Supplier<?>...)
+debug(Supplier<?>)
+debug(Supplier<?>, Throwable)</pre>
             <p>
               Similar methods exist for the other built-in levels.
               Custom levels, in contrast, need to pass in the log level as an extra parameter.