Repository: logging-log4j2
Updated Branches:
  refs/heads/LOG4J2-599-LambdaSupport [created] cf3ced443


LOG4J2-599 initial changes to support succinct lazy logging

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

Branch: refs/heads/LOG4J2-599-LambdaSupport
Commit: bccedde109dc8be865856e6bbbf0f0bf91247e4c
Parents: 27caceb
Author: rpopma <[email protected]>
Authored: Thu Aug 6 08:35:24 2015 +0900
Committer: rpopma <[email protected]>
Committed: Thu Aug 6 08:35:24 2015 +0900

----------------------------------------------------------------------
 .../java/org/apache/logging/log4j/Logger.java   | 16 +++++
 .../logging/log4j/spi/AbstractLogger.java       | 40 +++++++++++++
 .../logging/log4j/spi/ExtendedLogger.java       | 26 ++++++++
 .../apache/logging/log4j/util/LambdaUtil.java   | 62 ++++++++++++++++++++
 4 files changed, 144 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bccedde1/log4j-api/src/main/java/org/apache/logging/log4j/Logger.java
----------------------------------------------------------------------
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/Logger.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/Logger.java
index 8f9c837..85364b3 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/Logger.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/Logger.java
@@ -16,6 +16,8 @@
  */
 package org.apache.logging.log4j;
 
+import java.util.concurrent.Callable;
+
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.MessageFactory;
 
@@ -183,6 +185,20 @@ public interface Logger {
     void debug(String message, Throwable t);
 
     /**
+     * Logs a message which is only to be constructed if the logging level is 
the {@link Level#DEBUG DEBUG} level.
+     *
+     * @param msgSupplier A function, which when called, produces the desired 
log message.
+     */
+    void debug(Callable<?> msgSupplier);
+
+    /**
+     * Logs a message which is only to be constructed if the logging level is 
the {@link Level#DEBUG DEBUG} level.
+     *
+     * @param paramSupplier An array of functions, which when called, produce 
the desired log message parameters.
+     */
+    void debug(String message, Callable<?>... paramSupplier);
+
+    /**
      * Logs entry to a method. Used when the method in question has no 
parameters or when the parameters should not be
      * logged.
      */

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bccedde1/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
----------------------------------------------------------------------
diff --git 
a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
index d7e4be4..4eb4a50 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/AbstractLogger.java
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.spi;
 
 import java.io.Serializable;
+import java.util.concurrent.Callable;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Marker;
@@ -26,6 +27,7 @@ import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.message.ParameterizedMessageFactory;
 import org.apache.logging.log4j.message.StringFormattedMessage;
 import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.LambdaUtil;
 
 /**
  * Base implementation of a Logger. It is highly recommended that any Logger 
implementation extend this class.
@@ -218,6 +220,16 @@ public abstract class AbstractLogger implements 
ExtendedLogger, Serializable {
     }
 
     @Override
+    public void debug(Callable<?> msgSupplier) {
+        logIfEnabled(FQCN, Level.DEBUG, null, msgSupplier, null);
+    }
+
+    @Override
+    public void debug(String message, Callable<?>... paramSupplier) {
+        logIfEnabled(FQCN, Level.DEBUG, null, message, paramSupplier);
+    }
+
+    @Override
     public void debug(final Object message) {
         logIfEnabled(FQCN, Level.DEBUG, null, message, null);
     }
@@ -695,6 +707,14 @@ public abstract class AbstractLogger implements 
ExtendedLogger, Serializable {
     }
 
     @Override
+    public void logIfEnabled(final String fqcn, final Level level, final 
Marker marker, final Callable<?> msgSupplier,
+            final Throwable t) {
+        if (isEnabled(level, marker, msgSupplier, t)) {
+            logMessage(fqcn, level, marker, msgSupplier, t);
+        }
+    }
+
+    @Override
     public void logIfEnabled(final String fqcn, final Level level, final 
Marker marker, final String message) {
         if (isEnabled(level, marker, message)) {
             logMessage(fqcn, level, marker, message);
@@ -703,6 +723,14 @@ public abstract class AbstractLogger implements 
ExtendedLogger, Serializable {
 
     @Override
     public void logIfEnabled(final String fqcn, final Level level, final 
Marker marker, final String message,
+            final Callable<?>... paramSuppliers) {
+        if (isEnabled(level, marker, message)) {
+            logMessage(fqcn, level, marker, message, paramSuppliers);
+        }
+    }
+
+    @Override
+    public void logIfEnabled(final String fqcn, final Level level, final 
Marker marker, final String message,
             final Object... params) {
         if (isEnabled(level, marker, message, params)) {
             logMessage(fqcn, level, marker, message, params);
@@ -722,6 +750,12 @@ public abstract class AbstractLogger implements 
ExtendedLogger, Serializable {
         logMessage(fqcn, level, marker, messageFactory.newMessage(message), t);
     }
 
+    protected void logMessage(final String fqcn, final Level level, final 
Marker marker, final Callable<?> msgSupplier,
+            final Throwable t) {
+        Object message = LambdaUtil.call(msgSupplier);
+        logMessage(fqcn, level, marker, messageFactory.newMessage(message), t);
+    }
+
     protected void logMessage(final String fqcn, final Level level, final 
Marker marker, final String message,
             final Throwable t) {
         logMessage(fqcn, level, marker, messageFactory.newMessage(message), t);
@@ -738,6 +772,12 @@ public abstract class AbstractLogger implements 
ExtendedLogger, Serializable {
         logMessage(fqcn, level, marker, msg, msg.getThrowable());
     }
 
+    protected void logMessage(final String fqcn, final Level level, final 
Marker marker, final String message,
+            final Callable<?>... paramSuppliers) {
+        final Message msg = messageFactory.newMessage(message, 
LambdaUtil.callAll(paramSuppliers));
+        logMessage(fqcn, level, marker, msg, msg.getThrowable());
+    }
+
     @Override
     public void printf(final Level level, final Marker marker, final String 
format, final Object... params) {
         if (isEnabled(level, marker, format, params)) {

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bccedde1/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger.java
----------------------------------------------------------------------
diff --git 
a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger.java
index 8a36c57..1771f37 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger.java
@@ -16,6 +16,8 @@
  */
 package org.apache.logging.log4j.spi;
 
+import java.util.concurrent.Callable;
+
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.Marker;
@@ -94,6 +96,18 @@ public interface ExtendedLogger extends Logger {
     void logIfEnabled(String fqcn, Level level, Marker marker, Message 
message, Throwable t);
 
     /**
+     * Logs a message which is only to be constructed if the specified level 
is active.
+     * 
+     * @param fqcn The fully qualified class name of the logger entry point, 
used to determine the caller class and
+     *            method when location information needs to be logged.
+     * @param level The logging Level to check.
+     * @param marker A Marker or null.
+     * @param msgSupplier A function, which when called, produces the desired 
log message.
+     * @param t the exception to log, including its stack trace.
+     */
+    void logIfEnabled(String fqcn, Level level, Marker marker, Callable<?> 
msgSupplier, Throwable t);
+
+    /**
      * Logs a message if the specified level is active.
      * 
      * @param fqcn The fully qualified class name of the logger entry point, 
used to determine the caller class and
@@ -141,6 +155,18 @@ public interface ExtendedLogger extends Logger {
     void logIfEnabled(String fqcn, Level level, Marker marker, String message, 
Object... params);
 
     /**
+     * Logs a message which is only to be constructed if the specified level 
is active.
+     * 
+     * @param fqcn The fully qualified class name of the logger entry point, 
used to determine the caller class and
+     *            method when location information needs to be logged.
+     * @param level The logging Level to check.
+     * @param marker A Marker or null.
+     * @param message The message format.
+     * @param paramSupplier An array of functions, which when called, produce 
the desired log message parameters.
+     */
+    void logIfEnabled(String fqcn, Level level, Marker marker, String message, 
Callable<?>... paramSuppliers);
+
+    /**
      * Always logs a message at the specified level. It is the responsibility 
of the caller to ensure the specified
      * level is enabled.
      * 

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/bccedde1/log4j-api/src/main/java/org/apache/logging/log4j/util/LambdaUtil.java
----------------------------------------------------------------------
diff --git 
a/log4j-api/src/main/java/org/apache/logging/log4j/util/LambdaUtil.java 
b/log4j-api/src/main/java/org/apache/logging/log4j/util/LambdaUtil.java
new file mode 100644
index 0000000..9ba491b
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/LambdaUtil.java
@@ -0,0 +1,62 @@
+/*
+ * 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.util;
+
+import java.util.concurrent.Callable;
+
+/**
+ * Utility class for lambda support.
+ */
+public class LambdaUtil {
+    /**
+     * Converts an array of lambda expressions into an array of their 
evaluation results.
+     * 
+     * @param suppliers an array of lambda expressions or {@code null}
+     * @return an array containing the results of evaluating the lambda 
expressions (or {@code null} if the suppliers
+     *         array was {@code null}
+     */
+    public static Object[] callAll(Callable<?>... suppliers) {
+        if (suppliers == null) {
+            return (Object[]) null;
+        }
+        final Object[] result = new Object[suppliers.length];
+        for (int i = 0; i < result.length; i++) {
+            result[i] = call(suppliers[i]);
+        }
+        return result;
+    }
+
+    /**
+     * Returns the result of evaluating the specified function.
+     * @param supplier a lambda expression or {@code null}
+     * @return the results of evaluating the lambda expression (or {@code 
null} if the supplier
+     *         was {@code null}
+     */
+    public static Object call(Callable<?> supplier) {
+        if (supplier == null) {
+            return null;
+        }
+        Object result = null;
+        try {
+            result = supplier.call();
+        } catch (Exception ex) {
+            result = ex;
+        }
+        return result;
+    }
+}

Reply via email to