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
This commit is contained in:
Phillip Webb 2023-04-15 21:25:35 -07:00
parent efff253d35
commit 7728488541
2 changed files with 143 additions and 2 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}