CompositeLog respects log level changes at runtime
Closes gh-28477
This commit is contained in:
parent
7c47b470ff
commit
8fcc7ab9d1
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2022 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.
|
||||
|
|
@ -22,6 +22,7 @@ import java.util.function.Predicate;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.impl.NoOpLog;
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of {@link Log} that wraps a list of loggers and delegates
|
||||
* to the first one for which logging is enabled at the given level.
|
||||
|
|
@ -35,35 +36,114 @@ final class CompositeLog implements Log {
|
|||
private static final Log NO_OP_LOG = new NoOpLog();
|
||||
|
||||
|
||||
private final Log fatalLogger;
|
||||
|
||||
private final Log errorLogger;
|
||||
|
||||
private final Log warnLogger;
|
||||
|
||||
private final Log infoLogger;
|
||||
|
||||
private final Log debugLogger;
|
||||
|
||||
private final Log traceLogger;
|
||||
private final List<Log> loggers;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor with list of loggers. For optimal performance, the constructor
|
||||
* checks and remembers which logger is on for each log category.
|
||||
* Package-private constructor with list of loggers.
|
||||
* @param loggers the loggers to use
|
||||
*/
|
||||
public CompositeLog(List<Log> loggers) {
|
||||
this.fatalLogger = initLogger(loggers, Log::isFatalEnabled);
|
||||
this.errorLogger = initLogger(loggers, Log::isErrorEnabled);
|
||||
this.warnLogger = initLogger(loggers, Log::isWarnEnabled);
|
||||
this.infoLogger = initLogger(loggers, Log::isInfoEnabled);
|
||||
this.debugLogger = initLogger(loggers, Log::isDebugEnabled);
|
||||
this.traceLogger = initLogger(loggers, Log::isTraceEnabled);
|
||||
CompositeLog(List<Log> loggers) {
|
||||
this.loggers = loggers;
|
||||
}
|
||||
|
||||
private static Log initLogger(List<Log> loggers, Predicate<Log> predicate) {
|
||||
for (Log logger : loggers) {
|
||||
|
||||
@Override
|
||||
public boolean isFatalEnabled() {
|
||||
return isEnabled(Log::isFatalEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isErrorEnabled() {
|
||||
return isEnabled(Log::isErrorEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return isEnabled(Log::isWarnEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return isEnabled(Log::isInfoEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebugEnabled() {
|
||||
return isEnabled(Log::isDebugEnabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTraceEnabled() {
|
||||
return isEnabled(Log::isTraceEnabled);
|
||||
}
|
||||
|
||||
private boolean isEnabled(Predicate<Log> predicate) {
|
||||
return (getLogger(predicate) != NO_OP_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fatal(Object message) {
|
||||
getLogger(Log::isFatalEnabled).fatal(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fatal(Object message, Throwable ex) {
|
||||
getLogger(Log::isFatalEnabled).fatal(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Object message) {
|
||||
getLogger(Log::isErrorEnabled).error(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Object message, Throwable ex) {
|
||||
getLogger(Log::isErrorEnabled).error(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(Object message) {
|
||||
getLogger(Log::isWarnEnabled).warn(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(Object message, Throwable ex) {
|
||||
getLogger(Log::isWarnEnabled).warn(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(Object message) {
|
||||
getLogger(Log::isInfoEnabled).info(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(Object message, Throwable ex) {
|
||||
getLogger(Log::isInfoEnabled).info(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(Object message) {
|
||||
getLogger(Log::isDebugEnabled).debug(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(Object message, Throwable ex) {
|
||||
getLogger(Log::isDebugEnabled).debug(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(Object message) {
|
||||
getLogger(Log::isTraceEnabled).trace(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(Object message, Throwable ex) {
|
||||
getLogger(Log::isTraceEnabled).trace(message, ex);
|
||||
}
|
||||
|
||||
private Log getLogger(Predicate<Log> predicate) {
|
||||
for (Log logger : this.loggers) {
|
||||
if (predicate.test(logger)) {
|
||||
return logger;
|
||||
}
|
||||
|
|
@ -71,95 +151,4 @@ final class CompositeLog implements Log {
|
|||
return NO_OP_LOG;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isFatalEnabled() {
|
||||
return (this.fatalLogger != NO_OP_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isErrorEnabled() {
|
||||
return (this.errorLogger != NO_OP_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWarnEnabled() {
|
||||
return (this.warnLogger != NO_OP_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInfoEnabled() {
|
||||
return (this.infoLogger != NO_OP_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDebugEnabled() {
|
||||
return (this.debugLogger != NO_OP_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTraceEnabled() {
|
||||
return (this.traceLogger != NO_OP_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fatal(Object message) {
|
||||
this.fatalLogger.fatal(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fatal(Object message, Throwable ex) {
|
||||
this.fatalLogger.fatal(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Object message) {
|
||||
this.errorLogger.error(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(Object message, Throwable ex) {
|
||||
this.errorLogger.error(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(Object message) {
|
||||
this.warnLogger.warn(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void warn(Object message, Throwable ex) {
|
||||
this.warnLogger.warn(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(Object message) {
|
||||
this.infoLogger.info(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(Object message, Throwable ex) {
|
||||
this.infoLogger.info(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(Object message) {
|
||||
this.debugLogger.debug(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void debug(Object message, Throwable ex) {
|
||||
this.debugLogger.debug(message, ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(Object message) {
|
||||
this.traceLogger.trace(message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void trace(Object message, Throwable ex) {
|
||||
this.traceLogger.trace(message, ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Copyright 2002-2022 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.Arrays;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.BDDMockito.when;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for {@link CompositeLog}.
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class CompositeLogTests {
|
||||
|
||||
private final Log logger1 = mock(Log.class);
|
||||
|
||||
private final Log logger2 = mock(Log.class);
|
||||
|
||||
private final CompositeLog compositeLog = new CompositeLog(Arrays.asList(logger1, logger2));
|
||||
|
||||
|
||||
@Test
|
||||
void useFirstLogger() {
|
||||
when(logger1.isInfoEnabled()).thenReturn(true);
|
||||
when(logger2.isInfoEnabled()).thenReturn(true);
|
||||
|
||||
this.compositeLog.info("info message");
|
||||
|
||||
verify(this.logger1).isInfoEnabled();
|
||||
verify(this.logger1).info("info message");
|
||||
|
||||
verifyNoMoreInteractions(this.logger1);
|
||||
verifyNoMoreInteractions(this.logger2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void useSecondLogger() {
|
||||
when(logger1.isInfoEnabled()).thenReturn(false);
|
||||
when(logger2.isInfoEnabled()).thenReturn(true);
|
||||
|
||||
this.compositeLog.info("info message");
|
||||
|
||||
verify(this.logger1).isInfoEnabled();
|
||||
verify(this.logger2).isInfoEnabled();
|
||||
verify(this.logger2).info("info message");
|
||||
|
||||
verifyNoMoreInteractions(this.logger1);
|
||||
verifyNoMoreInteractions(this.logger2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void useNeitherLogger() {
|
||||
when(logger1.isInfoEnabled()).thenReturn(false);
|
||||
when(logger2.isInfoEnabled()).thenReturn(false);
|
||||
|
||||
this.compositeLog.info("info message");
|
||||
|
||||
verify(this.logger1).isInfoEnabled();
|
||||
verify(this.logger2).isInfoEnabled();
|
||||
|
||||
verifyNoMoreInteractions(this.logger1);
|
||||
verifyNoMoreInteractions(this.logger2);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue