disable slf4j logging backend

Bug: https://lists.apache.org/thread/vgj87y00m5svz9ydv5grt3zp2z9hx2zx
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/libcommons-logging-java/+bug/2055845
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1061025
Forwarded: not-needed
Last-Update: 2024-03-06

commons-logging added support for slf4j backend that is incompatible
with slf4j 1.7.x. Both logging subsystems try to get logger from
each other, causing "IllegalState: Recursive Update" exception.

Gbp-Pq: Name 09_disable_slf4j.patch
This commit is contained in:
Vladimir Petko 2024-05-07 14:05:46 +08:00 committed by luoyaoming
parent 6a2af07115
commit 92d2743355
3 changed files with 2 additions and 449 deletions

View File

@ -94,9 +94,7 @@ public abstract class LogFactory {
private static final String FACTORY_LOG4J_API = "org.apache.commons.logging.impl.Log4jApiLogFactory"; private static final String FACTORY_LOG4J_API = "org.apache.commons.logging.impl.Log4jApiLogFactory";
private static final String LOG4J_API_LOGGER = "org.apache.logging.log4j.Logger"; private static final String LOG4J_API_LOGGER = "org.apache.logging.log4j.Logger";
private static final String LOG4J_TO_SLF4J_BRIDGE = "org.apache.logging.slf4j.SLF4JProvider";
private static final String FACTORY_SLF4J = "org.apache.commons.logging.impl.Slf4jLogFactory";
private static final String SLF4J_API_LOGGER = "org.slf4j.Logger"; private static final String SLF4J_API_LOGGER = "org.slf4j.Logger";
/** /**
@ -918,21 +916,10 @@ public abstract class LogFactory {
try { try {
// We prefer Log4j API, since it does not stringify objects. // We prefer Log4j API, since it does not stringify objects.
if (factory == null && isClassAvailable(LOG4J_API_LOGGER, baseClassLoader)) { if (factory == null && isClassAvailable(LOG4J_API_LOGGER, baseClassLoader)) {
// If the Log4j API is redirected to SLF4J, we use SLF4J directly. logDiagnostic("[LOOKUP] Log4j API detected. Loading the Log4j API LogFactory implementation '" + FACTORY_LOG4J_API + "'.");
if (isClassAvailable(LOG4J_TO_SLF4J_BRIDGE, baseClassLoader)) { factory = newFactory(FACTORY_LOG4J_API, baseClassLoader, contextClassLoader);
logDiagnostic(
"[LOOKUP] Log4j API to SLF4J redirection detected. Loading the SLF4J LogFactory implementation '" + FACTORY_SLF4J + "'.");
factory = newFactory(FACTORY_SLF4J, baseClassLoader, contextClassLoader);
} else {
logDiagnostic("[LOOKUP] Log4j API detected. Loading the Log4j API LogFactory implementation '" + FACTORY_LOG4J_API + "'.");
factory = newFactory(FACTORY_LOG4J_API, baseClassLoader, contextClassLoader);
}
} }
if (factory == null && isClassAvailable(SLF4J_API_LOGGER, baseClassLoader)) {
logDiagnostic("[LOOKUP] SLF4J detected. Loading the SLF4J LogFactory implementation '" + FACTORY_SLF4J + "'.");
factory = newFactory(FACTORY_SLF4J, baseClassLoader, contextClassLoader);
}
} catch (final Exception e) { } catch (final Exception e) {
logDiagnostic("[LOOKUP] An exception occurred while creating LogFactory: " + e.getMessage()); logDiagnostic("[LOOKUP] An exception occurred while creating LogFactory: " + e.getMessage());
} }

View File

@ -1,322 +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.commons.logging.impl;
import static org.slf4j.spi.LocationAwareLogger.DEBUG_INT;
import static org.slf4j.spi.LocationAwareLogger.ERROR_INT;
import static org.slf4j.spi.LocationAwareLogger.INFO_INT;
import static org.slf4j.spi.LocationAwareLogger.TRACE_INT;
import static org.slf4j.spi.LocationAwareLogger.WARN_INT;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogConfigurationException;
import org.apache.commons.logging.LogFactory;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.slf4j.spi.LocationAwareLogger;
/**
* Logger factory hardcoded to send everything to SLF4J.
*
* @since 1.3.0
*/
public final class Slf4jLogFactory extends LogFactory {
private static final class Slf4jLocationAwareLog implements Log {
private static final String FQCN = Slf4jLocationAwareLog.class.getName();
private final LocationAwareLogger logger;
public Slf4jLocationAwareLog(final LocationAwareLogger logger) {
this.logger = logger;
}
@Override
public void debug(Object message) {
log(DEBUG_INT, message, null);
}
@Override
public void debug(Object message, Throwable t) {
log(DEBUG_INT, message, t);
}
@Override
public void error(Object message) {
log(ERROR_INT, message, null);
}
@Override
public void error(Object message, Throwable t) {
log(ERROR_INT, message, t);
}
@Override
public void fatal(Object message) {
error(message);
}
@Override
public void fatal(Object message, Throwable t) {
error(message, t);
}
@Override
public void info(Object message) {
log(INFO_INT, message, null);
}
@Override
public void info(Object message, Throwable t) {
log(INFO_INT, message, t);
}
@Override
public boolean isDebugEnabled() {
return logger.isDebugEnabled(MARKER);
}
@Override
public boolean isErrorEnabled() {
return logger.isErrorEnabled(MARKER);
}
@Override
public boolean isFatalEnabled() {
return isErrorEnabled();
}
@Override
public boolean isInfoEnabled() {
return logger.isInfoEnabled(MARKER);
}
@Override
public boolean isTraceEnabled() {
return logger.isTraceEnabled(MARKER);
}
@Override
public boolean isWarnEnabled() {
return logger.isWarnEnabled(MARKER);
}
private void log(final int level, final Object message, final Throwable t) {
logger.log(MARKER, FQCN, level, String.valueOf(message), EMPTY_OBJECT_ARRAY, t);
}
@Override
public void trace(Object message) {
log(TRACE_INT, message, null);
}
@Override
public void trace(Object message, Throwable t) {
log(TRACE_INT, message, t);
}
@Override
public void warn(Object message) {
log(WARN_INT, message, null);
}
@Override
public void warn(Object message, Throwable t) {
log(WARN_INT, message, t);
}
}
private static class Slf4jLog implements Log {
private final Logger logger;
public Slf4jLog(final Logger logger) {
this.logger = logger;
}
@Override
public void debug(Object message) {
logger.debug(MARKER, String.valueOf(message));
}
@Override
public void debug(Object message, Throwable t) {
logger.debug(MARKER, String.valueOf(message), t);
}
@Override
public void error(Object message) {
logger.error(MARKER, String.valueOf(message));
}
@Override
public void error(Object message, Throwable t) {
logger.debug(MARKER, String.valueOf(message), t);
}
@Override
public void fatal(Object message) {
error(message);
}
@Override
public void fatal(Object message, Throwable t) {
error(message, t);
}
@Override
public void info(Object message) {
logger.info(MARKER, String.valueOf(message));
}
@Override
public void info(Object message, Throwable t) {
logger.info(MARKER, String.valueOf(message), t);
}
@Override
public boolean isDebugEnabled() {
return logger.isDebugEnabled(MARKER);
}
@Override
public boolean isErrorEnabled() {
return logger.isErrorEnabled(MARKER);
}
@Override
public boolean isFatalEnabled() {
return isErrorEnabled();
}
@Override
public boolean isInfoEnabled() {
return logger.isInfoEnabled(MARKER);
}
@Override
public boolean isTraceEnabled() {
return logger.isTraceEnabled(MARKER);
}
@Override
public boolean isWarnEnabled() {
return logger.isWarnEnabled(MARKER);
}
@Override
public void trace(Object message) {
logger.trace(MARKER, String.valueOf(message));
}
@Override
public void trace(Object message, Throwable t) {
logger.trace(MARKER, String.valueOf(message), t);
}
@Override
public void warn(Object message) {
logger.warn(MARKER, String.valueOf(message));
}
@Override
public void warn(Object message, Throwable t) {
logger.warn(MARKER, String.valueOf(message), t);
}
}
private static final Object[] EMPTY_OBJECT_ARRAY = {};
private static final String[] EMPTY_STRING_ARRAY = {};
/**
* Marker used by all messages coming from Apache Commons Logging.
*/
private static final Marker MARKER = MarkerFactory.getMarker("COMMONS-LOGGING");
/**
* Caches Log instances.
* <p>
* The SLF4J reference implementation (Logback) has a single logger context, so each call to
* {@link #getInstance(String)}
* should give the same result.
* </p>
*/
private final ConcurrentMap<String, Log> loggers = new ConcurrentHashMap<>();
private final ConcurrentMap<String, Object> attributes = new ConcurrentHashMap<>();
@Override
public Object getAttribute(final String name) {
return attributes.get(name);
}
@Override
public String[] getAttributeNames() {
return attributes.keySet().toArray(EMPTY_STRING_ARRAY);
}
@Override
public Log getInstance(final Class<?> clazz) throws LogConfigurationException {
return getInstance(clazz.getName());
}
@Override
public Log getInstance(final String name) {
return loggers.computeIfAbsent(name, n -> {
final Logger logger = LoggerFactory.getLogger(n);
return logger instanceof LocationAwareLogger ? new Slf4jLocationAwareLog((LocationAwareLogger) logger) : new Slf4jLog(
logger);
});
}
/**
* This method is supposed to clear all loggers.
* <p>
* In this implementation it calls a "stop" method if the logger factory supports it. This is the case of
* Logback.
* </p>
*/
@Override
public void release() {
final ILoggerFactory factory = LoggerFactory.getILoggerFactory();
try {
factory.getClass().getMethod("stop").invoke(factory);
} catch (final ReflectiveOperationException ignored) {
}
}
@Override
public void removeAttribute(final String name) {
attributes.remove(name);
}
@Override
public void setAttribute(final String name, final Object value) {
if (value != null) {
attributes.put(name, value);
} else {
removeAttribute(name);
}
}
}

View File

@ -1,112 +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.commons.logging.slf4j;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Slf4jLogFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.read.ListAppender;
import ch.qos.logback.core.spi.FilterReply;
import junit.framework.TestCase;
public class CallerInformationTestCase extends TestCase {
private static final String STRING = "String";
private static final Throwable T = new RuntimeException();
private static final List<Marker> MARKERS = Collections.singletonList(MarkerFactory.getMarker("COMMONS-LOGGING"));
private static final Level[] levels = {Level.ERROR, // SLF4J has no FATAL level
Level.ERROR, Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE};
private LogFactory factory;
private Log log;
private ListAppender<ILoggingEvent> appender;
@Override
public void setUp() {
factory = LogFactory.getFactory();
log = factory.getInstance(getClass());
final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
final Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
appender = (ListAppender) logger.getAppender("LIST");
appender.clearAllFilters();
appender.addFilter(new Filter<ILoggingEvent>() {
@Override
public FilterReply decide(ILoggingEvent event) {
// Force the registration of caller data
event.getCallerData();
return FilterReply.NEUTRAL;
}
});
}
public void testFactoryClassName() {
assertEquals(Slf4jLogFactory.class, factory.getClass());
}
public void testLocationInfo() {
appender.list.clear();
// The following value must match the line number
final int currentLineNumber = 78;
log.fatal(STRING);
log.fatal(STRING, T);
log.error(STRING);
log.error(STRING, T);
log.warn(STRING);
log.warn(STRING, T);
log.info(STRING);
log.info(STRING, T);
log.debug(STRING);
log.debug(STRING, T);
log.trace(STRING);
log.trace(STRING, T);
final List<ILoggingEvent> events = new ArrayList<>(appender.list);
assertEquals("All events received.", levels.length * 2, events.size());
for (int lev = 0; lev < levels.length; lev++) {
for (int hasThrowable = 0; hasThrowable <= 1; hasThrowable++) {
final ILoggingEvent event = events.get(2 * lev + hasThrowable);
assertEquals("Correct message.", STRING, event.getMessage());
assertEquals("Level matches.", levels[lev], event.getLevel());
final StackTraceElement[] callerData = event.getCallerData();
assertTrue("Has location", callerData != null && callerData.length > 0);
final StackTraceElement location = callerData[0];
assertEquals("Correct location class.", getClass().getName(), location.getClassName());
assertEquals("Correct location line.",
currentLineNumber + 2 * lev + hasThrowable + 1,
location.getLineNumber());
final ThrowableProxy throwableProxy = (ThrowableProxy) event.getThrowableProxy();
assertEquals("Correct exception",
hasThrowable > 0 ? T : null,
throwableProxy != null ? throwableProxy.getThrowable() : null);
}
}
}
}