From 7728488541bca3ba7cccbf5c66ae543b3694be93 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 15 Apr 2023 21:25:35 -0700 Subject: [PATCH] Allow LogLevel to be used easily with commons logging Add a `LogLevel.log` method that can be used to log a message at the given level using commons logging. Closes gh-35024 --- .../boot/logging/LogLevel.java | 54 ++++++++++- .../boot/logging/LogLevelTests.java | 91 +++++++++++++++++++ 2 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogLevelTests.java diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogLevel.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogLevel.java index bfbda6703a..fc3e5a45c1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogLevel.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/LogLevel.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2023 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. @@ -16,6 +16,8 @@ package org.springframework.boot.logging; +import org.apache.commons.logging.Log; + /** * Logging levels supported by a {@link LoggingSystem}. * @@ -24,6 +26,54 @@ package org.springframework.boot.logging; */ public enum LogLevel { - TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF + TRACE(Log::trace), + + DEBUG(Log::debug), + + INFO(Log::info), + + WARN(Log::warn), + + ERROR(Log::error), + + FATAL(Log::fatal), + + OFF(null); + + private final LogMethod logMethod; + + LogLevel(LogMethod logMethod) { + this.logMethod = logMethod; + } + + /** + * Log a message to the given logger at this level. + * @param logger the logger + * @param message the message to log + * @since 3.1.0 + */ + public void log(Log logger, Object message) { + log(logger, message, null); + } + + /** + * Log a message to the given logger at this level. + * @param logger the logger + * @param message the message to log + * @param cause the cause to log + * @since 3.1.0 + */ + public void log(Log logger, Object message, Throwable cause) { + if (logger != null && this.logMethod != null) { + this.logMethod.log(logger, message, cause); + } + } + + @FunctionalInterface + private interface LogMethod { + + void log(Log logger, Object message, Throwable cause); + + } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogLevelTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogLevelTests.java new file mode 100644 index 0000000000..db9be83815 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogLevelTests.java @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2023 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.boot.logging; + +import org.apache.commons.logging.Log; +import org.junit.jupiter.api.Test; + +import static org.mockito.BDDMockito.then; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link LogLevel}. + * + * @author Phillip Webb + */ +class LogLevelTests { + + private Log logger = mock(Log.class); + + private Exception exception = new Exception(); + + @Test + void logWhenTraceLogsAtTrace() { + LogLevel.TRACE.log(this.logger, "test"); + LogLevel.TRACE.log(this.logger, "test", this.exception); + then(this.logger).should().trace("test", null); + then(this.logger).should().trace("test", this.exception); + } + + @Test + void logWhenDebugLogsAtDebug() { + LogLevel.DEBUG.log(this.logger, "test"); + LogLevel.DEBUG.log(this.logger, "test", this.exception); + then(this.logger).should().debug("test", null); + then(this.logger).should().debug("test", this.exception); + } + + @Test + void logWhenInfoLogsAtInfo() { + LogLevel.INFO.log(this.logger, "test"); + LogLevel.INFO.log(this.logger, "test", this.exception); + then(this.logger).should().info("test", null); + then(this.logger).should().info("test", this.exception); + } + + @Test + void logWhenWarnLogsAtWarn() { + LogLevel.WARN.log(this.logger, "test"); + LogLevel.WARN.log(this.logger, "test", this.exception); + then(this.logger).should().warn("test", null); + then(this.logger).should().warn("test", this.exception); + } + + @Test + void logWhenErrorLogsAtError() { + LogLevel.ERROR.log(this.logger, "test"); + LogLevel.ERROR.log(this.logger, "test", this.exception); + then(this.logger).should().error("test", null); + then(this.logger).should().error("test", this.exception); + } + + @Test + void logWhenFatalLogsAtFatal() { + LogLevel.FATAL.log(this.logger, "test"); + LogLevel.FATAL.log(this.logger, "test", this.exception); + then(this.logger).should().fatal("test", null); + then(this.logger).should().fatal("test", this.exception); + } + + @Test + void logWhenOffDoesNotLog() { + LogLevel.OFF.log(this.logger, "test"); + LogLevel.OFF.log(this.logger, "test", this.exception); + then(this.logger).shouldHaveNoInteractions(); + } + +}