Repository: logging-log4j2 Updated Branches: refs/heads/LOG4J2-599-LambdaSupport 63a325d97 -> 12be6d867
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/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 95696af..641e36b 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,7 +17,6 @@ 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; @@ -28,11 +27,13 @@ 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; +import org.apache.logging.log4j.util.MessageSupplier; +import org.apache.logging.log4j.util.Supplier; /** * Base implementation of a Logger. It is highly recommended that any Logger implementation extend this class. */ -public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializable { +public abstract class AbstractLogger implements ExtendedLogger2, Serializable { private static final long serialVersionUID = 2L; @@ -245,36 +246,56 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void debug(final Callable<?> msgSupplier) { + public void debug(final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.DEBUG, null, msgSupplier, (Throwable) null); } @Override - public void debug(final Callable<?> msgSupplier, final Throwable t) { + public void debug(final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.DEBUG, null, msgSupplier, t); } @Override - public void debug(final Marker marker, final Callable<?> msgSupplier) { + public void debug(final Marker marker, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.DEBUG, marker, msgSupplier, (Throwable) null); } @Override - public void debug(final Marker marker, final String message, final Callable<?>... paramSuppliers) { + public void debug(final Marker marker, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.DEBUG, marker, message, paramSuppliers); } @Override - public void debug(final Marker marker, final Callable<?> msgSupplier, final Throwable t) { + public void debug(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.DEBUG, marker, msgSupplier, t); } @Override - public void debug(final String message, final Callable<?>... paramSuppliers) { + public void debug(final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.DEBUG, null, message, paramSuppliers); } @Override + public void debug(final Marker marker, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.DEBUG, marker, msgSupplier, (Throwable) null); + } + + @Override + public void debug(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.DEBUG, marker, msgSupplier, t); + } + + @Override + public void debug(final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.DEBUG, null, msgSupplier, (Throwable) null); + } + + @Override + public void debug(final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.DEBUG, null, msgSupplier, t); + } + + @Override public void entry() { entry(FQCN); } @@ -387,36 +408,56 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void error(final Callable<?> msgSupplier) { + public void error(final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.ERROR, null, msgSupplier, (Throwable) null); } @Override - public void error(final Callable<?> msgSupplier, final Throwable t) { + public void error(final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.ERROR, null, msgSupplier, t); } @Override - public void error(final Marker marker, final Callable<?> msgSupplier) { + public void error(final Marker marker, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.ERROR, marker, msgSupplier, (Throwable) null); } @Override - public void error(final Marker marker, final String message, final Callable<?>... paramSuppliers) { + public void error(final Marker marker, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.ERROR, marker, message, paramSuppliers); } @Override - public void error(final Marker marker, final Callable<?> msgSupplier, final Throwable t) { + public void error(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.ERROR, marker, msgSupplier, t); } @Override - public void error(final String message, final Callable<?>... paramSuppliers) { + public void error(final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.ERROR, null, message, paramSuppliers); } @Override + public void error(final Marker marker, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.ERROR, marker, msgSupplier, (Throwable) null); + } + + @Override + public void error(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.ERROR, marker, msgSupplier, t); + } + + @Override + public void error(final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.ERROR, null, msgSupplier, (Throwable) null); + } + + @Override + public void error(final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.ERROR, null, msgSupplier, t); + } + + @Override public void exit() { exit(FQCN, null); } @@ -519,36 +560,56 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void fatal(final Callable<?> msgSupplier) { + public void fatal(final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.FATAL, null, msgSupplier, (Throwable) null); } @Override - public void fatal(final Callable<?> msgSupplier, final Throwable t) { + public void fatal(final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.FATAL, null, msgSupplier, t); } @Override - public void fatal(final Marker marker, final Callable<?> msgSupplier) { + public void fatal(final Marker marker, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.FATAL, marker, msgSupplier, (Throwable) null); } @Override - public void fatal(final Marker marker, final String message, final Callable<?>... paramSuppliers) { + public void fatal(final Marker marker, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.FATAL, marker, message, paramSuppliers); } @Override - public void fatal(final Marker marker, final Callable<?> msgSupplier, final Throwable t) { + public void fatal(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.FATAL, marker, msgSupplier, t); } @Override - public void fatal(final String message, final Callable<?>... paramSuppliers) { + public void fatal(final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.FATAL, null, message, paramSuppliers); } @Override + public void fatal(final Marker marker, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.FATAL, marker, msgSupplier, (Throwable) null); + } + + @Override + public void fatal(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.FATAL, marker, msgSupplier, t); + } + + @Override + public void fatal(final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.FATAL, null, msgSupplier, (Throwable) null); + } + + @Override + public void fatal(final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.FATAL, null, msgSupplier, t); + } + + @Override public MessageFactory getMessageFactory() { return messageFactory; } @@ -629,36 +690,56 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void info(final Callable<?> msgSupplier) { + public void info(final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.INFO, null, msgSupplier, (Throwable) null); } @Override - public void info(final Callable<?> msgSupplier, final Throwable t) { + public void info(final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.INFO, null, msgSupplier, t); } @Override - public void info(final Marker marker, final Callable<?> msgSupplier) { + public void info(final Marker marker, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.INFO, marker, msgSupplier, (Throwable) null); } @Override - public void info(final Marker marker, final String message, final Callable<?>... paramSuppliers) { + public void info(final Marker marker, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.INFO, marker, message, paramSuppliers); } @Override - public void info(final Marker marker, final Callable<?> msgSupplier, final Throwable t) { + public void info(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.INFO, marker, msgSupplier, t); } @Override - public void info(final String message, final Callable<?>... paramSuppliers) { + public void info(final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.INFO, null, message, paramSuppliers); } @Override + public void info(final Marker marker, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.INFO, marker, msgSupplier, (Throwable) null); + } + + @Override + public void info(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.INFO, marker, msgSupplier, t); + } + + @Override + public void info(final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.INFO, null, msgSupplier, (Throwable) null); + } + + @Override + public void info(final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.INFO, null, msgSupplier, t); + } + + @Override public boolean isDebugEnabled() { return isEnabled(Level.DEBUG, null, null); } @@ -801,36 +882,56 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void log(final Level level, final Callable<?> msgSupplier) { + public void log(final Level level, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, level, null, msgSupplier, (Throwable) null); } @Override - public void log(final Level level, final Callable<?> msgSupplier, final Throwable t) { + public void log(final Level level, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, level, null, msgSupplier, t); } @Override - public void log(final Level level, final Marker marker, final Callable<?> msgSupplier) { + public void log(final Level level, final Marker marker, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, level, marker, msgSupplier, (Throwable) null); } @Override - public void log(final Level level, final Marker marker, final String message, final Callable<?>... paramSuppliers) { + public void log(final Level level, final Marker marker, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, level, marker, message, paramSuppliers); } @Override - public void log(final Level level, final Marker marker, final Callable<?> msgSupplier, final Throwable t) { + public void log(final Level level, final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, level, marker, msgSupplier, t); } @Override - public void log(final Level level, final String message, final Callable<?>... paramSuppliers) { + public void log(final Level level, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, level, null, message, paramSuppliers); } @Override + public void log(final Level level, final Marker marker, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, level, marker, msgSupplier, (Throwable) null); + } + + @Override + public void log(final Level level, final Marker marker, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, level, marker, msgSupplier, t); + } + + @Override + public void log(final Level level, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, level, null, msgSupplier, (Throwable) null); + } + + @Override + public void log(final Level level, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, level, null, msgSupplier, t); + } + + @Override public void logIfEnabled(final String fqcn, final Level level, final Marker marker, final Message msg, final Throwable t) { if (isEnabled(level, marker, msg, t)) { @@ -839,6 +940,14 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override + public void logIfEnabled(final String fqcn, final Level level, final Marker marker, + final MessageSupplier 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 Object message, final Throwable t) { if (isEnabled(level, marker, message, t)) { @@ -847,7 +956,7 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void logIfEnabled(final String fqcn, final Level level, final Marker marker, final Callable<?> msgSupplier, + public void logIfEnabled(final String fqcn, final Level level, final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { if (isEnabled(level, marker, msgSupplier, t)) { logMessage(fqcn, level, marker, msgSupplier, t); @@ -863,7 +972,7 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab @Override public void logIfEnabled(final String fqcn, final Level level, final Marker marker, final String message, - final Callable<?>... paramSuppliers) { + final Supplier<?>... paramSuppliers) { if (isEnabled(level, marker, message)) { logMessage(fqcn, level, marker, message, paramSuppliers); } @@ -890,9 +999,15 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab logMessage(fqcn, level, marker, messageFactory.newMessage(message), t); } - protected void logMessage(final String fqcn, final Level level, final Marker marker, final Callable<?> msgSupplier, + protected void logMessage(final String fqcn, final Level level, final Marker marker, + final MessageSupplier msgSupplier, final Throwable t) { + Message message = LambdaUtil.get(msgSupplier); + logMessage(fqcn, level, marker, message, t); + } + + protected void logMessage(final String fqcn, final Level level, final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { - Object message = LambdaUtil.call(msgSupplier); + Object message = LambdaUtil.get(msgSupplier); logMessage(fqcn, level, marker, messageFactory.newMessage(message), t); } @@ -913,8 +1028,8 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } 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)); + final Supplier<?>... paramSuppliers) { + final Message msg = messageFactory.newMessage(message, LambdaUtil.getAll(paramSuppliers)); logMessage(fqcn, level, marker, msg, msg.getThrowable()); } @@ -1035,41 +1150,61 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void warn(final Marker marker, final Message msg) { - logIfEnabled(FQCN, Level.WARN, marker, msg, null); - } - - @Override - public void trace(final Callable<?> msgSupplier) { + public void trace(final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.TRACE, null, msgSupplier, (Throwable) null); } @Override - public void trace(final Callable<?> msgSupplier, final Throwable t) { + public void trace(final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.TRACE, null, msgSupplier, t); } @Override - public void trace(final Marker marker, final Callable<?> msgSupplier) { + public void trace(final Marker marker, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.TRACE, marker, msgSupplier, (Throwable) null); } @Override - public void trace(final Marker marker, final String message, final Callable<?>... paramSuppliers) { + public void trace(final Marker marker, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.TRACE, marker, message, paramSuppliers); } @Override - public void trace(final Marker marker, final Callable<?> msgSupplier, final Throwable t) { + public void trace(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.TRACE, marker, msgSupplier, t); } @Override - public void trace(final String message, final Callable<?>... paramSuppliers) { + public void trace(final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.TRACE, null, message, paramSuppliers); } @Override + public void trace(final Marker marker, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.TRACE, marker, msgSupplier, (Throwable) null); + } + + @Override + public void trace(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.TRACE, marker, msgSupplier, t); + } + + @Override + public void trace(final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.TRACE, null, msgSupplier, (Throwable) null); + } + + @Override + public void trace(final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.TRACE, null, msgSupplier, t); + } + + @Override + public void warn(final Marker marker, final Message msg) { + logIfEnabled(FQCN, Level.WARN, marker, msg, null); + } + + @Override public void warn(final Marker marker, final Message msg, final Throwable t) { logIfEnabled(FQCN, Level.WARN, marker, msg, t); } @@ -1079,11 +1214,6 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab logIfEnabled(FQCN, Level.WARN, marker, message, null); } - /* -- FIXME: this comment looks lost - * Instead of one single method with Object... declared the following methods explicitly specify parameters because - * they perform dramatically better than having the JVM convert them to an array. - */ - @Override public void warn(final Marker marker, final Object message, final Throwable t) { logIfEnabled(FQCN, Level.WARN, marker, message, t); @@ -1140,32 +1270,52 @@ public abstract class AbstractLogger implements ExtendedLambdaLogger, Serializab } @Override - public void warn(final Callable<?> msgSupplier) { + public void warn(final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.WARN, null, msgSupplier, (Throwable) null); } @Override - public void warn(final Callable<?> msgSupplier, final Throwable t) { + public void warn(final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.WARN, null, msgSupplier, t); } @Override - public void warn(final Marker marker, final Callable<?> msgSupplier) { + public void warn(final Marker marker, final Supplier<?> msgSupplier) { logIfEnabled(FQCN, Level.WARN, marker, msgSupplier, (Throwable) null); } @Override - public void warn(final Marker marker, final String message, final Callable<?>... paramSuppliers) { + public void warn(final Marker marker, final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.WARN, marker, message, paramSuppliers); } @Override - public void warn(final Marker marker, final Callable<?> msgSupplier, final Throwable t) { + public void warn(final Marker marker, final Supplier<?> msgSupplier, final Throwable t) { logIfEnabled(FQCN, Level.WARN, marker, msgSupplier, t); } @Override - public void warn(final String message, final Callable<?>... paramSuppliers) { + public void warn(final String message, final Supplier<?>... paramSuppliers) { logIfEnabled(FQCN, Level.WARN, null, message, paramSuppliers); } + + @Override + public void warn(final Marker marker, final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.WARN, marker, msgSupplier, (Throwable) null); + } + + @Override + public void warn(final Marker marker, final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.WARN, marker, msgSupplier, t); + } + + @Override + public void warn(final MessageSupplier msgSupplier) { + logIfEnabled(FQCN, Level.WARN, null, msgSupplier, (Throwable) null); + } + + @Override + public void warn(final MessageSupplier msgSupplier, final Throwable t) { + logIfEnabled(FQCN, Level.WARN, null, msgSupplier, t); + } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLambdaLogger.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLambdaLogger.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLambdaLogger.java deleted file mode 100644 index 5e61f62..0000000 --- a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLambdaLogger.java +++ /dev/null @@ -1,56 +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.spi; - -import java.util.concurrent.Callable; - -import org.apache.logging.log4j.LambdaLogger; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Marker; - -/** - * Extends the {@code LambdaLogger} interface with methods that facilitate implementing or extending - * {@code LambdaLogger}s. Users should not need to use this interface. - */ -public interface ExtendedLambdaLogger extends ExtendedLogger, LambdaLogger { - - /** - * Logs a message whose parameters are 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 paramSuppliers 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); - - /** - * 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); - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger2.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger2.java b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger2.java new file mode 100644 index 0000000..85ce529 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/spi/ExtendedLogger2.java @@ -0,0 +1,68 @@ +/* + * 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.spi; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.Logger2; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.util.MessageSupplier; +import org.apache.logging.log4j.util.Supplier; + +/** + * Extends the {@code Logger2} interface with methods that facilitate implementing or extending + * {@code Logger2}s. Users should not need to use this interface. + */ +public interface ExtendedLogger2 extends ExtendedLogger, Logger2 { + + /** + * 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, MessageSupplier msgSupplier, Throwable t); + + /** + * Logs a message whose parameters are 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 paramSuppliers An array of functions, which when called, produce the desired log message parameters. + */ + void logIfEnabled(String fqcn, Level level, Marker marker, String message, Supplier<?>... paramSuppliers); + + /** + * 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, Supplier<?> msgSupplier, Throwable t); + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/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 index 9ba491b..954886b 100644 --- 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 @@ -17,7 +17,8 @@ package org.apache.logging.log4j.util; -import java.util.concurrent.Callable; +import org.apache.logging.log4j.message.Message; + /** * Utility class for lambda support. @@ -30,13 +31,13 @@ public class LambdaUtil { * @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) { + public static Object[] getAll(Supplier<?>... 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]); + result[i] = get(suppliers[i]); } return result; } @@ -47,16 +48,29 @@ public class LambdaUtil { * @return the results of evaluating the lambda expression (or {@code null} if the supplier * was {@code null} */ - public static Object call(Callable<?> supplier) { + public static Object get(Supplier<?> supplier) { if (supplier == null) { return null; } Object result = null; try { - result = supplier.call(); + result = supplier.get(); } catch (Exception ex) { result = ex; } return result; } + + /** + * Returns the Message supplied by the specified function. + * @param supplier a lambda expression or {@code null} + * @return the Message resulting from evaluating the lambda expression (or {@code null} if the supplier was + * {@code null} + */ + public static Message get(MessageSupplier supplier) { + if (supplier == null) { + return null; + } + return supplier.get(); + } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/log4j-api/src/main/java/org/apache/logging/log4j/util/MessageSupplier.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/MessageSupplier.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/MessageSupplier.java new file mode 100644 index 0000000..1d268cf --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/MessageSupplier.java @@ -0,0 +1,40 @@ +/* + * 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 org.apache.logging.log4j.message.Message; + +/** + * Classes implementing this interface know how to supply {@link Message}s. + * + * <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">functional + * interface</a> intended to support lambda expressions in log4j 2. + * + * <p>Implementors are free to cache values or return a new or distinct value each time the supplier is invoked. + * + * @since log4j-2.4 + */ +public interface MessageSupplier { + + /** + * Gets a Message. + * + * @return a Message + */ + Message get(); +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/log4j-api/src/main/java/org/apache/logging/log4j/util/Supplier.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/Supplier.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/Supplier.java new file mode 100644 index 0000000..51421f3 --- /dev/null +++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/Supplier.java @@ -0,0 +1,40 @@ +/* + * 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; + +/** + * Classes implementing this interface know how to supply a value. + * + * <p>This is a <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html">functional + * interface</a> intended to support lambda expressions in log4j 2. + * + * <p>Implementors are free to cache values or return a new or distinct value each time the supplier is invoked. + * + * @param <T> the type of values returned by this supplier + * + * @since log4j-2.4 + */ +public interface Supplier<T> { + + /** + * Gets a value. + * + * @return a value + */ + T get(); +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/log4j-api/src/test/java/org/apache/logging/log4j/Logger2Test.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/Logger2Test.java b/log4j-api/src/test/java/org/apache/logging/log4j/Logger2Test.java new file mode 100644 index 0000000..14dd36b --- /dev/null +++ b/log4j-api/src/test/java/org/apache/logging/log4j/Logger2Test.java @@ -0,0 +1,190 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +import org.apache.logging.log4j.message.Message; +import org.apache.logging.log4j.message.SimpleMessage; +import org.apache.logging.log4j.spi.AbstractLogger; +import org.apache.logging.log4j.util.MessageSupplier; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the AbstractLogger implementation of the Logger2 interface. + */ +public class Logger2Test { + + private static class LogEvent { + @SuppressWarnings("unused") + final String fqcn; + @SuppressWarnings("unused") + final Level level; + final Marker marker; + final Message message; + final Throwable throwable; + + public LogEvent(String fqcn, Level level, Marker marker, Message message, Throwable t) { + this.fqcn = fqcn; + this.level = level; + this.marker = marker; + this.message = message; + this.throwable = t; + } + } + + class Logger2Impl extends AbstractLogger { + private static final long serialVersionUID = 1L; + + boolean enabled = true; + final List<Logger2Test.LogEvent> list = new ArrayList<Logger2Test.LogEvent>(); + + @Override + public boolean isEnabled(Level level, Marker marker, Message message, Throwable t) { + return enabled; + } + + @Override + public boolean isEnabled(Level level, Marker marker, Object message, Throwable t) { + return enabled; + } + + @Override + public boolean isEnabled(Level level, Marker marker, String message, Throwable t) { + return enabled; + } + + @Override + public boolean isEnabled(Level level, Marker marker, String message) { + return enabled; + } + + @Override + public boolean isEnabled(Level level, Marker marker, String message, Object... params) { + return enabled; + } + + @Override + public void logMessage(String fqcn, Level level, Marker marker, Message message, Throwable t) { + list.add(new LogEvent(fqcn, level, marker, message, t)); + } + + @Override + public Level getLevel() { + return null; + } + + public AbstractLogger disable() { + enabled = false; + return this; + } + + public AbstractLogger enable() { + enabled = true; + return this; + } + } + + final Logger2Impl logger2 = new Logger2Impl(); + final Message message = new SimpleMessage("HiMessage"); + final Throwable throwable = new Error("I'm Bad"); + final Marker marker = MarkerManager.getMarker("test"); + + class MyMessageSupplier implements MessageSupplier { + public int count = 0; + + @Override + public Message get() { + count++; + return message; + } + }; + + final MyMessageSupplier messageSupplier = new MyMessageSupplier(); + + @Before + public void beforeEachTest() { + logger2.list.clear(); + messageSupplier.count = 0; + } + + @Test + public void testDebugMarkerMessageSupplier() { + logger2.disable().debug(marker, messageSupplier); + assertTrue(logger2.list.isEmpty()); + assertEquals(0, messageSupplier.count); + + logger2.enable().debug(marker, messageSupplier); + assertEquals(1, logger2.list.size()); + assertEquals(1, messageSupplier.count); + + LogEvent event = logger2.list.get(0); + assertSame(message, event.message); + assertSame(marker, event.marker); + } + + @Test + public void testDebugMessageSupplier() { + logger2.disable().debug(messageSupplier); + assertTrue(logger2.list.isEmpty()); + assertEquals(0, messageSupplier.count); + + logger2.enable().debug(messageSupplier); + assertEquals(1, logger2.list.size()); + assertEquals(1, messageSupplier.count); + + LogEvent event = logger2.list.get(0); + assertSame(message, event.message); + } + + @Test + public void testDebugMarkerMessageSupplierThrowable() { + logger2.disable().debug(marker, messageSupplier, throwable); + assertTrue(logger2.list.isEmpty()); + assertEquals(0, messageSupplier.count); + + logger2.enable().debug(marker, messageSupplier, throwable); + assertEquals(1, logger2.list.size()); + assertEquals(1, messageSupplier.count); + + LogEvent event = logger2.list.get(0); + assertSame(marker, event.marker); + assertSame(message, event.message); + assertSame(throwable, event.throwable); + } + + @Test + public void testDebugMessageSupplierThrowable() { + logger2.disable().debug(messageSupplier, throwable); + assertTrue(logger2.list.isEmpty()); + assertEquals(0, messageSupplier.count); + + logger2.enable().debug(messageSupplier, throwable); + assertEquals(1, logger2.list.size()); + assertEquals(1, messageSupplier.count); + + LogEvent event = logger2.list.get(0); + assertSame(message, event.message); + assertSame(throwable, event.throwable); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/log4j-api/src/test/java/org/apache/logging/log4j/util/LambdaUtilTest.java ---------------------------------------------------------------------- diff --git a/log4j-api/src/test/java/org/apache/logging/log4j/util/LambdaUtilTest.java b/log4j-api/src/test/java/org/apache/logging/log4j/util/LambdaUtilTest.java index edfc50e..946c63c 100644 --- a/log4j-api/src/test/java/org/apache/logging/log4j/util/LambdaUtilTest.java +++ b/log4j-api/src/test/java/org/apache/logging/log4j/util/LambdaUtilTest.java @@ -17,8 +17,8 @@ package org.apache.logging.log4j.util; -import java.util.concurrent.Callable; - +import org.apache.logging.log4j.message.Message; +import org.apache.logging.log4j.message.SimpleMessage; import org.junit.Test; import static org.junit.Assert.*; @@ -29,10 +29,21 @@ import static org.junit.Assert.*; public class LambdaUtilTest { @Test - public void testCallReturnsResultOfCallable() { + public void testGetSupplierResultOfSupplier() { final String expected = "result"; - final Object actual = LambdaUtil.call(new Callable<String>() { - public String call() { + final Object actual = LambdaUtil.get(new Supplier<String>() { + public String get() { + return expected; + } + }); + assertSame(expected, actual); + } + + @Test + public void testGetMessageSupplierResultOfSupplier() { + final Message expected = new SimpleMessage("hi"); + final Message actual = LambdaUtil.get(new MessageSupplier() { + public Message get() { return expected; } }); @@ -40,55 +51,71 @@ public class LambdaUtilTest { } @Test - public void testCallReturnsNullIfCallableNull() { - final Object actual = LambdaUtil.call(null); + public void testGetSupplierReturnsNullIfSupplierNull() { + final Object actual = LambdaUtil.get((Supplier<?>) null); + assertNull(actual); + } + + @Test + public void testGetMessageSupplierReturnsNullIfSupplierNull() { + final Object actual = LambdaUtil.get((MessageSupplier) null); assertNull(actual); } @Test - public void testCallReturnsExceptionIfCallableThrowsException() { - final Exception expected = new RuntimeException(); - final Object actual = LambdaUtil.call(new Callable<String>() { - public String call() throws Exception{ + public void testGetSupplierExceptionIfSupplierThrowsException() { + final RuntimeException expected = new RuntimeException(); + final Object actual = LambdaUtil.get(new Supplier<String>() { + public String get() { throw expected; } }); assertSame(expected, actual); } + @Test + public void testGetMessageSupplierExceptionIfSupplierThrowsException() { + final RuntimeException expected = new RuntimeException(); + final Object actual = LambdaUtil.get(new MessageSupplier() { + public Message get() { + throw expected; + } + }); + assertSame(expected, actual); + } @Test - public void testCallAllReturnsResultOfCallables() { + public void testGetAllReturnsResultOfSuppliers() { final String expected1 = "result1"; - Callable<String> function1 = new Callable<String>() { - public String call() { + Supplier<String> function1 = new Supplier<String>() { + public String get() { return expected1; } }; final String expected2 = "result2"; - Callable<String> function2 = new Callable<String>() { - public String call() { + Supplier<String> function2 = new Supplier<String>() { + public String get() { return expected2; } }; - - Callable<?>[] functions = {function1, function2}; - final Object[] actual = LambdaUtil.callAll(functions); + + Supplier<?>[] functions = { function1, function2 }; + final Object[] actual = LambdaUtil.getAll(functions); assertEquals(actual.length, functions.length); assertSame(expected1, actual[0]); assertSame(expected2, actual[1]); } @Test - public void testCallAllReturnsNullArrayIfCallablesArrayNull() { - final Object[] actual = LambdaUtil.callAll((Callable<?>[]) null); + public void testGetAllReturnsNullArrayIfSupplierArrayNull() { + final Object[] actual = LambdaUtil.getAll((Supplier<?>[]) null); assertNull(actual); } @Test - public void testCallAllReturnsNullElementsIfCallableArrayContainsNulls() { - final Callable<?>[] functions = new Callable[3]; - final Object[] actual = LambdaUtil.callAll(functions); + public void testGetAllReturnsNullElementsIfSupplierArrayContainsNulls() { + final Supplier<?>[] functions = new Supplier[3]; + final Object[] actual = LambdaUtil.getAll(functions); assertEquals(actual.length, functions.length); for (Object object : actual) { assertNull(object); @@ -96,22 +123,22 @@ public class LambdaUtilTest { } @Test - public void testCallAllReturnsExceptionsIfCallablesThrowsException() { - final Exception expected1 = new RuntimeException(); - Callable<String> function1 = new Callable<String>() { - public String call() throws Exception{ + public void testGetAllReturnsExceptionsIfSuppliersThrowsException() { + final RuntimeException expected1 = new RuntimeException(); + Supplier<String> function1 = new Supplier<String>() { + public String get() { throw expected1; } }; - final Exception expected2 = new RuntimeException(); - Callable<String> function2 = new Callable<String>() { - public String call() throws Exception{ + final RuntimeException expected2 = new RuntimeException(); + Supplier<String> function2 = new Supplier<String>() { + public String get() { throw expected2; } }; - - Callable<?>[] functions = {function1, function2}; - final Object[] actual = LambdaUtil.callAll(functions); + + Supplier<?>[] functions = { function1, function2 }; + final Object[] actual = LambdaUtil.getAll(functions); assertEquals(actual.length, functions.length); assertSame(expected1, actual[0]); assertSame(expected2, actual[1]); http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/12be6d86/src/site/xdoc/manual/api.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/manual/api.xml b/src/site/xdoc/manual/api.xml index 1783353..d7a8cd3 100644 --- a/src/site/xdoc/manual/api.xml +++ b/src/site/xdoc/manual/api.xml @@ -118,7 +118,7 @@ logger.printf(Level.INFO, "Logging in user %1$s with birthday %2$tm %2$te,%2$tY" <a name="LambdaSupport"/> <h4>Java 8 lambda support for lazy logging</h4> <p> - The <code>LambdaLogger</code> interface extends <code>Logger</code> to add support for lambda expressions. + The <code>Logger2</code> interface extends <code>Logger</code> to add support for lambda expressions. This logger allows client code to lazily log messages without explicitly checking if the requested log level is enabled. For example, previously you would write: </p> @@ -129,12 +129,12 @@ if (logger.isTraceEnabled()) { logger.trace("Some long-running operation returned {}", expensiveOperation()); }</pre> <p> - With Java 8 and the <code>LambdaLogger</code> interface, you can achieve the same effect by using a + With Java 8 and the <code>Logger2</code> interface, you can achieve the same effect by using a lambda expression. You no longer need to explicitly check the log level: </p> <pre class="prettyprint linenums">// Java-8 style optimization: no need to explicitly check the log level: // the lambda expression is not evaluated if the TRACE level is not enabled -LambdaLogger logger = LogManager.getLambdaLogger(); +Logger2 logger = LogManager.getLogger2(); logger.trace("Some long-running operation returned {}", () -> expensiveOperation());</pre> <h4>Logger Names</h4>