diff --git a/spring-core/src/main/java/org/springframework/core/log/LogAccessor.java b/spring-core/src/main/java/org/springframework/core/log/LogAccessor.java new file mode 100644 index 0000000000..78a500443a --- /dev/null +++ b/spring-core/src/main/java/org/springframework/core/log/LogAccessor.java @@ -0,0 +1,280 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.core.log; + +import java.util.function.Supplier; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * A convenient accessor for Commons Logging, providing not only + * {@code CharSequence} based log methods but also {@code Supplier} + * based variants for use with Java 8 lambda expressions. + * + * @author Juergen Hoeller + * @since 5.2 + */ +public class LogAccessor { + + private final Log log; + + + /** + * Create a new accessor for the given Commons Log. + * @see LogFactory#getLog(Class) + * @see LogFactory#getLog(String) + */ + public LogAccessor(Log log) { + this.log = log; + } + + /** + * Create a new accessor for the specified Commons Log category. + * @see LogFactory#getLog(Class) + */ + public LogAccessor(Class logCategory) { + this.log = LogFactory.getLog(logCategory); + } + + /** + * Create a new accessor for the specified Commons Log category. + * @see LogFactory#getLog(String) + */ + public LogAccessor(String logCategory) { + this.log = LogFactory.getLog(logCategory); + } + + + /** + * Return the target Commons Log. + */ + public final Log getLog() { + return this.log; + } + + + // Plain log methods + + /** + * Log a message with fatal log level. + * @param message the message to log + */ + public void fatal(CharSequence message) { + this.log.fatal(message); + } + + /** + * Log an error with fatal log level. + * @param message the message to log + * @param t log this cause + */ + public void fatal(CharSequence message, Throwable t) { + this.log.fatal(message, t); + } + + /** + * Log a message with error log level. + * @param message the message to log + */ + public void error(CharSequence message) { + this.log.error(message); + } + + /** + * Log an error with error log level. + * @param message the message to log + * @param t log this cause + */ + public void error(CharSequence message, Throwable t) { + this.log.error(message, t); + } + + /** + * Log a message with warn log level. + * @param message the message to log + */ + public void warn(CharSequence message) { + this.log.warn(message); + } + + /** + * Log an error with warn log level. + * @param message the message to log + * @param t log this cause + */ + public void warn(CharSequence message, Throwable t) { + this.log.warn(message, t); + } + + /** + * Log a message with info log level. + * @param message the message to log + */ + public void info(CharSequence message) { + this.log.info(message); + } + + /** + * Log an error with info log level. + * @param message the message to log + * @param t log this cause + */ + public void info(CharSequence message, Throwable t) { + this.log.info(message, t); + } + + /** + * Log a message with debug log level. + * @param message the message to log + */ + public void debug(CharSequence message) { + this.log.debug(message); + } + + /** + * Log an error with debug log level. + * @param message the message to log + * @param t log this cause + */ + public void debug(CharSequence message, Throwable t) { + this.log.debug(message, t); + } + + /** + * Log a message with trace log level. + * @param message the message to log + */ + public void trace(CharSequence message) { + this.log.trace(message); + } + + /** + * Log an error with trace log level. + * @param message the message to log + * @param t log this cause + */ + public void trace(CharSequence message, Throwable t) { + this.log.trace(message, t); + } + + + // Supplier-based log methods + + /** + * Log a message with fatal log level. + * @param messageSupplier a lazy supplier for the message to log + */ + public void fatal(Supplier messageSupplier) { + this.log.fatal(new LogMessage(messageSupplier)); + } + + /** + * Log an error with fatal log level. + * @param messageSupplier a lazy supplier for the message to log + * @param t log this cause + */ + public void fatal(Supplier messageSupplier, Throwable t) { + this.log.fatal(new LogMessage(messageSupplier), t); + } + + /** + * Log a message with error log level. + * @param messageSupplier a lazy supplier for the message to log + */ + public void error(Supplier messageSupplier) { + this.log.error(new LogMessage(messageSupplier)); + } + + /** + * Log an error with error log level. + * @param messageSupplier a lazy supplier for the message to log + * @param t log this cause + */ + public void error(Supplier messageSupplier, Throwable t) { + this.log.error(new LogMessage(messageSupplier), t); + } + + /** + * Log a message with warn log level. + * @param messageSupplier a lazy supplier for the message to log + */ + public void warn(Supplier messageSupplier) { + this.log.warn(new LogMessage(messageSupplier)); + } + + /** + * Log an error with warn log level. + * @param messageSupplier a lazy supplier for the message to log + * @param t log this cause + */ + public void warn(Supplier messageSupplier, Throwable t) { + this.log.warn(new LogMessage(messageSupplier), t); + } + + /** + * Log a message with info log level. + * @param messageSupplier a lazy supplier for the message to log + */ + public void info(Supplier messageSupplier) { + this.log.info(new LogMessage(messageSupplier)); + } + + /** + * Log an error with info log level. + * @param messageSupplier a lazy supplier for the message to log + * @param t log this cause + */ + public void info(Supplier messageSupplier, Throwable t) { + this.log.info(new LogMessage(messageSupplier), t); + } + + /** + * Log a message with debug log level. + * @param messageSupplier a lazy supplier for the message to log + */ + public void debug(Supplier messageSupplier) { + this.log.debug(new LogMessage(messageSupplier)); + } + + /** + * Log an error with debug log level. + * @param messageSupplier a lazy supplier for the message to log + * @param t log this cause + */ + public void debug(Supplier messageSupplier, Throwable t) { + this.log.debug(new LogMessage(messageSupplier), t); + } + + /** + * Log a message with trace log level. + * @param messageSupplier a lazy supplier for the message to log + */ + public void trace(Supplier messageSupplier) { + this.log.trace(new LogMessage(messageSupplier)); + } + + /** + * Log an error with trace log level. + * @param messageSupplier a lazy supplier for the message to log + * @param t log this cause + */ + public void trace(Supplier messageSupplier, Throwable t) { + this.log.trace(new LogMessage(messageSupplier), t); + } + +} diff --git a/spring-core/src/main/java/org/springframework/core/log/LogMessage.java b/spring-core/src/main/java/org/springframework/core/log/LogMessage.java new file mode 100644 index 0000000000..a1ce24465a --- /dev/null +++ b/spring-core/src/main/java/org/springframework/core/log/LogMessage.java @@ -0,0 +1,69 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.core.log; + +import java.util.function.Supplier; + +import org.springframework.lang.Nullable; +import org.springframework.util.Assert; + +/** + * A simple log message type for use with Commons Logging, allowing + * for convenient late resolution of a given {@link Supplier} instance + * (typically bound to a Java 8 lambda expression) in {@link #toString()}. + * + * @author Juergen Hoeller + * @since 5.2 + * @see org.apache.commons.logging.Log#fatal(Object) + * @see org.apache.commons.logging.Log#error(Object) + * @see org.apache.commons.logging.Log#warn(Object) + * @see org.apache.commons.logging.Log#info(Object) + * @see org.apache.commons.logging.Log#debug(Object) + * @see org.apache.commons.logging.Log#trace(Object) + */ +public class LogMessage { + + private final Supplier supplier; + + @Nullable + private String result; + + + /** + * Construct a new {@code LogMessage} for the given supplier. + * @param supplier the lazily resolving supplier + * (typically bound to a Java 8 lambda expression) + */ + public LogMessage(Supplier supplier) { + Assert.notNull(supplier, "Supplier must not be null"); + this.supplier = supplier; + } + + + /** + * This will be called by the logging provider, potentially once + * per log target (therefore locally caching the result here). + */ + @Override + public String toString() { + if (this.result == null) { + this.result = this.supplier.get().toString(); + } + return this.result; + } + +} diff --git a/spring-core/src/test/java/org/springframework/core/log/LogSupportTests.java b/spring-core/src/test/java/org/springframework/core/log/LogSupportTests.java new file mode 100644 index 0000000000..e217df9278 --- /dev/null +++ b/spring-core/src/test/java/org/springframework/core/log/LogSupportTests.java @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2019 the original author or authors. + * + * Licensed 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 + * + * https://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.springframework.core.log; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author Juergen Hoeller + * @since 5.2 + */ +public class LogSupportTests { + + @Test + public void testLogMessage() { + LogMessage msg = new LogMessage(() -> new StringBuilder("a")); + assertEquals("a", msg.toString()); + assertSame(msg.toString(), msg.toString()); + } + +}