diff --git a/jul-to-log4j/pom.xml b/jul-to-log4j/pom.xml deleted file mode 100644 index 9e1842005f7..00000000000 --- a/jul-to-log4j/pom.xml +++ /dev/null @@ -1,133 +0,0 @@ - - - - 4.0.0 - - org.apache.logging.log4j - log4j - ${revision} - ../log4j-parent - - - jul-to-log4j - Apache Log4j JUL LogManager - A `java.util.logging` LogManager that forwards events to the Log4j API. - - - - - - org.jspecify.*;resolution:=optional - - - - org.jspecify;transitive=false - - - - - - - org.apache.logging.log4j - log4j-api - - - - org.apache.logging.log4j - log4j-kit - - - - org.assertj - assertj-core - test - - - - org.hamcrest - hamcrest - test - - - - junit - junit - test - - - - org.apache.logging.log4j - log4j-async-logger - test - - - - org.apache.logging.log4j - log4j-core - test - - - - org.apache.logging.log4j - log4j-core-test - test - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - true - - -Xms256m -Xmx1024m - 1 - false - - - - - org.apache.maven.surefire - surefire-junit47 - ${surefire.version} - - - - - default-test - - test - - test - - - - org.apache.logging.jul.tolog4j.LogManager - - - - - - - - diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/LevelTranslator.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/LevelTranslator.java deleted file mode 100644 index 142b19fe759..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/LevelTranslator.java +++ /dev/null @@ -1,88 +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.logging.jul.tolog4j; - -import org.apache.logging.jul.tolog4j.internal.DefaultLevelConverter; -import org.apache.logging.jul.tolog4j.internal.JulProperties; -import org.apache.logging.jul.tolog4j.spi.LevelConverter; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.kit.env.PropertyEnvironment; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.LoaderUtil; - -/** - * Utility class to convert between JDK Levels and Log4j 2 Levels. - * - * @since 2.1 - */ -public final class LevelTranslator { - - /** - * Custom Log4j level corresponding to the {@link java.util.logging.Level#FINEST} logging level. This maps to a - * level more specific than {@link org.apache.logging.log4j.Level#TRACE}. - */ - public static final Level FINEST = Level.forName("FINEST", Level.TRACE.intLevel() + 100); - - /** - * Custom Log4j level corresponding to the {@link java.util.logging.Level#CONFIG} logging level. This maps to a - * level in between {@link org.apache.logging.log4j.Level#INFO} and {@link org.apache.logging.log4j.Level#DEBUG}. - */ - public static final Level CONFIG = Level.forName("CONFIG", Level.INFO.intLevel() + 50); - - private static final Logger LOGGER = StatusLogger.getLogger(); - private static final LevelConverter LEVEL_CONVERTER; - - static { - final Class levelConverterClass = - PropertyEnvironment.getGlobal().getProperty(JulProperties.class).levelConverter(); - if (levelConverterClass != null) { - LevelConverter levelConverter; - try { - levelConverter = LoaderUtil.newInstanceOf(levelConverterClass); - } catch (final Exception e) { - LOGGER.error("Could not create custom LevelConverter [{}].", levelConverterClass.getName(), e); - levelConverter = new DefaultLevelConverter(); - } - LEVEL_CONVERTER = levelConverter; - } else { - LEVEL_CONVERTER = new DefaultLevelConverter(); - } - } - - /** - * Converts a JDK logging Level to a Log4j logging Level. - * - * @param level JDK Level to convert, may be null per the JUL specification. - * @return converted Level or null - */ - public static Level toLevel(final java.util.logging.Level level) { - return LEVEL_CONVERTER.toLevel(level); - } - - /** - * Converts a Log4j logging Level to a JDK logging Level. - * - * @param level Log4j Level to convert. - * @return converted Level. - */ - public static java.util.logging.Level toJavaLevel(final Level level) { - return LEVEL_CONVERTER.toJavaLevel(level); - } - - private LevelTranslator() {} -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/LogManager.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/LogManager.java deleted file mode 100644 index 6e105aa679f..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/LogManager.java +++ /dev/null @@ -1,104 +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.logging.jul.tolog4j; - -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Set; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.internal.ApiLoggerAdapter; -import org.apache.logging.jul.tolog4j.internal.JulProperties; -import org.apache.logging.jul.tolog4j.internal.NoOpLogger; -import org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter; -import org.apache.logging.log4j.kit.env.PropertyEnvironment; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.LoaderUtil; - -/** - * Log4j implementation of {@link java.util.logging.LogManager}. - *

- * Note that the system property {@code java.util.logging.manager} must be set to - * {@code org.apache.logging.jul.tolog4j.LogManager} in order to use this adaptor. - * This LogManager requires the {@code log4j-api} library to be available. - *

- *

- * To override the default {@link AbstractLoggerAdapter} that is used, specify the Log4j property - * {@code log4j.jul.LoggerAdapter} and set it to the fully qualified class name of a custom - * implementation. - * All implementations must have a default constructor. - *

- * - * @since 2.1 - */ -public class LogManager extends java.util.logging.LogManager { - - private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger(); - private final AbstractLoggerAdapter loggerAdapter; - // Contains the set of logger names that are actively being requested using getLogger. - private final ThreadLocal> recursive = ThreadLocal.withInitial(HashSet::new); - - public LogManager() { - AbstractLoggerAdapter adapter = null; - final Class adapterClass = - PropertyEnvironment.getGlobal().getProperty(JulProperties.class).loggerAdapter(); - if (adapterClass != null) { - try { - LOGGER.info("Trying to use LoggerAdapter [{}] specified by Log4j property.", adapterClass.getName()); - adapter = LoaderUtil.newInstanceOf(adapterClass); - } catch (final Exception e) { - LOGGER.error( - "Specified LoggerAdapter [{}] can not be created, using default.", adapterClass.getName(), e); - } - } - if (adapter == null) { - // Use API by default - // See https://github.com/apache/logging-log4j2/issues/2353 - adapter = new ApiLoggerAdapter(); - } - loggerAdapter = adapter; - LOGGER.info("Registered Log4j as the java.util.logging.LogManager."); - } - - @Override - public boolean addLogger(final Logger logger) { - // in order to prevent non-bridged loggers from being registered, we always return false to indicate that - // the named logger should be obtained through getLogger(name) - return false; - } - - @Override - public Logger getLogger(final String name) { - LOGGER.trace("Call to LogManager.getLogger({})", name); - final Set activeRequests = recursive.get(); - if (activeRequests.add(name)) { - try { - return loggerAdapter.getLogger(name); - } finally { - activeRequests.remove(name); - } - } - LOGGER.warn("Recursive call to getLogger for {} ignored.", name); - return new NoOpLogger(name); - } - - @Override - public Enumeration getLoggerNames() { - return Collections.enumeration( - loggerAdapter.getLoggersInContext(loggerAdapter.getContext()).keySet()); - } -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/ApiLogger.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/ApiLogger.java deleted file mode 100644 index bde8a191009..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/ApiLogger.java +++ /dev/null @@ -1,74 +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.logging.jul.tolog4j.internal; - -import java.util.logging.Filter; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.support.AbstractLogger; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.apache.logging.log4j.status.StatusLogger; - -/** - * Implementation of {@link java.util.logging.Logger} that ignores all method calls that do not have an equivalent in - * the Log4j API. - */ -public class ApiLogger extends AbstractLogger { - - private static final String MUTATOR_DISABLED = - """ - Ignoring call to `j.ul.Logger.{}()`, since the Log4j API does not provide methods to modify the underlying implementation. - To modify the configuration using JUL, use an `AbstractLoggerAdapter` appropriate for your logging implementation. - See https://logging.apache.org/log4j/3.x/log4j-jul.html#log4j.jul.loggerAdapter for more information."""; - private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger(); - - public ApiLogger(ExtendedLogger logger) { - super(logger); - } - - @Override - public void setFilter(Filter newFilter) throws SecurityException { - LOGGER.warn(MUTATOR_DISABLED, "setFilter"); - } - - @Override - public void setLevel(Level newLevel) throws SecurityException { - LOGGER.warn(MUTATOR_DISABLED, "setLevel"); - } - - @Override - public void addHandler(Handler handler) throws SecurityException { - LOGGER.warn(MUTATOR_DISABLED, "addHandler"); - } - - @Override - public void removeHandler(Handler handler) throws SecurityException { - LOGGER.warn(MUTATOR_DISABLED, "removeHandler"); - } - - @Override - public void setUseParentHandlers(boolean useParentHandlers) { - LOGGER.warn(MUTATOR_DISABLED, "setUseParentHandlers"); - } - - @Override - public void setParent(Logger parent) { - throw new UnsupportedOperationException( - ApiLogger.class.getSimpleName() + " does not support `j.u.l.Logger#setParent()`."); - } -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/ApiLoggerAdapter.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/ApiLoggerAdapter.java deleted file mode 100644 index 6e6778f9e2d..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/ApiLoggerAdapter.java +++ /dev/null @@ -1,39 +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.logging.jul.tolog4j.internal; - -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.message.MessageFormatMessageFactory; -import org.apache.logging.log4j.spi.LoggerContext; - -/** - * {@link Logger} registry implementation using just log4j-api. This is the fallback registry used when log4j-core is - * not available. - * - * @since 2.1 - */ -public class ApiLoggerAdapter extends AbstractLoggerAdapter { - - private static final MessageFactory MESSAGE_FACTORY = new MessageFormatMessageFactory(); - - @Override - public Logger newLogger(final String name, final LoggerContext context) { - return new ApiLogger(context.getLogger(name, MESSAGE_FACTORY)); - } -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/DefaultLevelConverter.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/DefaultLevelConverter.java deleted file mode 100644 index 9a8ed846b55..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/DefaultLevelConverter.java +++ /dev/null @@ -1,129 +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.logging.jul.tolog4j.internal; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.IdentityHashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import org.apache.logging.jul.tolog4j.LevelTranslator; -import org.apache.logging.jul.tolog4j.spi.LevelConverter; -import org.apache.logging.log4j.Level; - -/** - * Default implementation of LevelConverter strategy. - *

- * Since 2.4, supports custom JUL levels by mapping them to their closest mapped neighbour. - *

- * - * @since 2.1 - */ -public class DefaultLevelConverter implements LevelConverter { - - static final class JulLevelComparator implements Comparator { - @Override - public int compare(final java.util.logging.Level level1, final java.util.logging.Level level2) { - return Integer.compare(level1.intValue(), level2.intValue()); - } - } - - private final ConcurrentMap julToLog4j = new ConcurrentHashMap<>(9); - private final Map log4jToJul = new IdentityHashMap<>(10); - private final List sortedJulLevels = new ArrayList<>(9); - - public DefaultLevelConverter() { - // Map JUL to Log4j - mapJulToLog4j(java.util.logging.Level.ALL, Level.ALL); - mapJulToLog4j(java.util.logging.Level.FINEST, LevelTranslator.FINEST); - mapJulToLog4j(java.util.logging.Level.FINER, Level.TRACE); - mapJulToLog4j(java.util.logging.Level.FINE, Level.DEBUG); - mapJulToLog4j(java.util.logging.Level.CONFIG, LevelTranslator.CONFIG); - mapJulToLog4j(java.util.logging.Level.INFO, Level.INFO); - mapJulToLog4j(java.util.logging.Level.WARNING, Level.WARN); - mapJulToLog4j(java.util.logging.Level.SEVERE, Level.ERROR); - mapJulToLog4j(java.util.logging.Level.OFF, Level.OFF); - // Map Log4j to JUL - mapLog4jToJul(Level.ALL, java.util.logging.Level.ALL); - mapLog4jToJul(LevelTranslator.FINEST, java.util.logging.Level.FINEST); - mapLog4jToJul(Level.TRACE, java.util.logging.Level.FINER); - mapLog4jToJul(Level.DEBUG, java.util.logging.Level.FINE); - mapLog4jToJul(LevelTranslator.CONFIG, java.util.logging.Level.CONFIG); - mapLog4jToJul(Level.INFO, java.util.logging.Level.INFO); - mapLog4jToJul(Level.WARN, java.util.logging.Level.WARNING); - mapLog4jToJul(Level.ERROR, java.util.logging.Level.SEVERE); - mapLog4jToJul(Level.FATAL, java.util.logging.Level.SEVERE); - mapLog4jToJul(Level.OFF, java.util.logging.Level.OFF); - // Sorted Java levels - sortedJulLevels.addAll(julToLog4j.keySet()); - Collections.sort(sortedJulLevels, new JulLevelComparator()); - } - - private long distance(final java.util.logging.Level javaLevel, final java.util.logging.Level customJavaLevel) { - return Math.abs((long) customJavaLevel.intValue() - (long) javaLevel.intValue()); - } - - /* - * TODO consider making public for advanced configuration. - */ - private void mapJulToLog4j(final java.util.logging.Level julLevel, final Level level) { - julToLog4j.put(julLevel, level); - } - - /* - * TODO consider making public for advanced configuration. - */ - private void mapLog4jToJul(final Level level, final java.util.logging.Level julLevel) { - log4jToJul.put(level, julLevel); - } - - private Level nearestLevel(final java.util.logging.Level customJavaLevel) { - long prevDist = Long.MAX_VALUE; - java.util.logging.Level prevLevel = null; - for (final java.util.logging.Level mappedJavaLevel : sortedJulLevels) { - final long distance = distance(customJavaLevel, mappedJavaLevel); - if (distance > prevDist) { - return julToLog4j.get(prevLevel); - } - prevDist = distance; - prevLevel = mappedJavaLevel; - } - return julToLog4j.get(prevLevel); - } - - @Override - public java.util.logging.Level toJavaLevel(final Level level) { - return log4jToJul.get(level); - } - - @Override - public Level toLevel(final java.util.logging.Level javaLevel) { - if (javaLevel == null) { - return null; - } - final Level level = julToLog4j.get(javaLevel); - if (level != null) { - return level; - } - final Level nearestLevel = nearestLevel(javaLevel); - julToLog4j.put(javaLevel, nearestLevel); - return nearestLevel; - } -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/JulProperties.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/JulProperties.java deleted file mode 100644 index 1b500b678bc..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/JulProperties.java +++ /dev/null @@ -1,27 +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.logging.jul.tolog4j.internal; - -import org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter; -import org.apache.logging.jul.tolog4j.spi.LevelConverter; -import org.apache.logging.log4j.kit.env.Log4jProperty; -import org.jspecify.annotations.Nullable; - -@Log4jProperty(name = "jul") -public record JulProperties( - @Nullable Class levelConverter, - @Nullable Class loggerAdapter) {} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/NoOpLogger.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/NoOpLogger.java deleted file mode 100644 index 879d19abb43..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/internal/NoOpLogger.java +++ /dev/null @@ -1,222 +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.logging.jul.tolog4j.internal; - -import java.util.ResourceBundle; -import java.util.function.Supplier; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; - -/** - * Dummy version of a java.util.Logger. - */ -public class NoOpLogger extends Logger { - - public NoOpLogger(final String name) { - super(name, null); - } - - @Override - public void log(final LogRecord record) {} - - @Override - public void log(final Level level, final String msg) {} - - @Override - public void log(final Level level, final Supplier msgSupplier) {} - - @Override - public void log(final Level level, final String msg, final Object param1) {} - - @Override - public void log(final Level level, final String msg, final Object[] params) {} - - @Override - public void log(final Level level, final String msg, final Throwable thrown) {} - - @Override - public void log(final Level level, final Throwable thrown, final Supplier msgSupplier) {} - - @Override - public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg) {} - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final Supplier msgSupplier) {} - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final String msg, - final Object param1) {} - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final String msg, - final Object[] params) {} - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final String msg, - final Throwable thrown) {} - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final Throwable thrown, - final Supplier msgSupplier) {} - - @Override - public void logrb( - final Level level, - final String sourceClass, - final String sourceMethod, - final String bundleName, - final String msg) {} - - @Override - public void logrb( - final Level level, - final String sourceClass, - final String sourceMethod, - final String bundleName, - final String msg, - final Object param1) {} - - @Override - public void logrb( - final Level level, - final String sourceClass, - final String sourceMethod, - final String bundleName, - final String msg, - final Object[] params) {} - - @Override - public void logrb( - final Level level, - final String sourceClass, - final String sourceMethod, - final ResourceBundle bundle, - final String msg, - final Object... params) {} - - @Override - public void logrb( - final Level level, - final String sourceClass, - final String sourceMethod, - final String bundleName, - final String msg, - final Throwable thrown) {} - - @Override - public void logrb( - final Level level, - final String sourceClass, - final String sourceMethod, - final ResourceBundle bundle, - final String msg, - final Throwable thrown) {} - - @Override - public void entering(final String sourceClass, final String sourceMethod) {} - - @Override - public void entering(final String sourceClass, final String sourceMethod, final Object param1) {} - - @Override - public void entering(final String sourceClass, final String sourceMethod, final Object[] params) {} - - @Override - public void exiting(final String sourceClass, final String sourceMethod) {} - - @Override - public void exiting(final String sourceClass, final String sourceMethod, final Object result) {} - - @Override - public void throwing(final String sourceClass, final String sourceMethod, final Throwable thrown) {} - - @Override - public void severe(final String msg) {} - - @Override - public void warning(final String msg) {} - - @Override - public void info(final String msg) {} - - @Override - public void config(final String msg) {} - - @Override - public void fine(final String msg) {} - - @Override - public void finer(final String msg) {} - - @Override - public void finest(final String msg) {} - - @Override - public void severe(final Supplier msgSupplier) {} - - @Override - public void warning(final Supplier msgSupplier) {} - - @Override - public void info(final Supplier msgSupplier) {} - - @Override - public void config(final Supplier msgSupplier) {} - - @Override - public void fine(final Supplier msgSupplier) {} - - @Override - public void finer(final Supplier msgSupplier) {} - - @Override - public void finest(final Supplier msgSupplier) {} - - @Override - public void setLevel(final Level newLevel) throws SecurityException {} - - @Override - public Level getLevel() { - return Level.OFF; - } - - @Override - public boolean isLoggable(final Level level) { - return false; - } -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/package-info.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/package-info.java deleted file mode 100644 index d0fc9b2d63e..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/package-info.java +++ /dev/null @@ -1,22 +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. - */ -@Export -@Version("3.0.0") -package org.apache.logging.jul.tolog4j; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/AbstractLoggerAdapter.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/AbstractLoggerAdapter.java deleted file mode 100644 index 8c43dc8a1a2..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/AbstractLoggerAdapter.java +++ /dev/null @@ -1,67 +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.logging.jul.tolog4j.spi; - -import java.util.logging.Logger; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.util.StackLocatorUtil; - -/** - * Abstract Logger registry. - *

- * JUL contains methods, such as {@link Logger#setLevel}, which modify the configuration of the logging backend. - * To fully implement all {@code Logger} methods, we need to provide a different {@code Logger} implementation - * for each Log4j API implementation. - *

- *

- * Older Log4j versions provided an alternative {@code CoreLoggerAdapter} implementation that supported - * the modification of Log4j Core configuration using JUL. - *

- * Since version 2.24.0, however, this implementation was deprecated for removal. - * If you wish to enable this feature again, you need to implement this class and provide its FQCN - * as {@code log4j.jul.loggerAdapter} configuration property. - *

- *

- * Implementation note: since version 3.0.0, this interface was moved to a new package. - *

- * - * @see Issue #2353 - * @since 2.1 - */ -public abstract class AbstractLoggerAdapter extends org.apache.logging.log4j.spi.AbstractLoggerAdapter { - - /** - * Creates a new {@link java.util.logging.Logger} - *

- * Each implementation should provide this method. - *

- */ - @Override - public abstract Logger newLogger(String name, LoggerContext context); - - /** - * Provides the most appropriate {@link LoggerContext} for the caller. - */ - @Override - public LoggerContext getContext() { - return getContext( - LogManager.getFactory().isClassLoaderDependent() - ? StackLocatorUtil.getCallerClass(java.util.logging.LogManager.class) - : null); - } -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/LevelConverter.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/LevelConverter.java deleted file mode 100644 index 489477ebaa9..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/LevelConverter.java +++ /dev/null @@ -1,49 +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.logging.jul.tolog4j.spi; - -import org.apache.logging.jul.tolog4j.internal.JulProperties; -import org.apache.logging.log4j.Level; - -/** - * Strategy interface to convert between custom Log4j {@link Level Levels} and JUL - * {@link java.util.logging.Level Levels}. - *

- * Implementation note: since version 3.0.0, this interface was moved to a new package. - *

- * - * @see JulProperties#levelConverter() - * @since 2.1 - */ -public interface LevelConverter { - - /** - * Converts a JDK logging Level to a Log4j logging Level. - * - * @param javaLevel JDK Level to convert, may be null per the JUL specification. - * @return converted Level or {@code null} if the given level could not be converted. - */ - Level toLevel(java.util.logging.Level javaLevel); - - /** - * Converts a Log4j logging Level to a JDK logging Level. - * - * @param level Log4j Level to convert. - * @return converted Level or {@code null} if the given level could not be converted. - */ - java.util.logging.Level toJavaLevel(Level level); -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/package-info.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/package-info.java deleted file mode 100644 index b7eb30cbacc..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/spi/package-info.java +++ /dev/null @@ -1,25 +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. - */ -/** - * Contains interfaces an abstract classes to extend the functionality of Log4j JUL Adapter. - */ -@Export -@Version("3.0.0") -package org.apache.logging.jul.tolog4j.spi; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/support/AbstractLogger.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/support/AbstractLogger.java deleted file mode 100644 index f91812b45b6..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/support/AbstractLogger.java +++ /dev/null @@ -1,441 +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.logging.jul.tolog4j.support; - -import static org.apache.logging.log4j.spi.AbstractLogger.ENTRY_MARKER; -import static org.apache.logging.log4j.spi.AbstractLogger.EXIT_MARKER; -import static org.apache.logging.log4j.spi.AbstractLogger.THROWING_MARKER; - -import java.util.ResourceBundle; -import java.util.function.Supplier; -import java.util.logging.Filter; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LevelTranslator; -import org.apache.logging.log4j.BridgeAware; -import org.apache.logging.log4j.LogBuilder; -import org.apache.logging.log4j.message.DefaultFlowMessageFactory; -import org.apache.logging.log4j.message.LocalizedMessage; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.spi.ExtendedLogger; - -/** - * Log4j API implementation of the JUL {@link Logger} class. - *

- * Note that this implementation does not use the {@link java.util.logging.Handler} class. - * Instead, - * logging is delegated to the underlying Log4j {@link org.apache.logging.log4j.Logger} - * which may be implemented in one of many different ways. - * Consult the documentation for your Log4j API Provider for more details. - *

- *

- * Note that the methods {@link #getParent()} and mutator methods such as {@link #setLevel(java.util.logging.Level)} - * must be provided by implementations of this class. - * The default {@link org.apache.logging.jul.tolog4j.internal.ApiLogger} implementations just ignores them. - * If you need support for these methods, then you'll need to provide your own - * {@link org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter}. - *

- * - * @since 3.0.0 - */ -public abstract class AbstractLogger extends Logger { - - private final ExtendedLogger logger; - private static final String FQCN = AbstractLogger.class.getName(); - - protected AbstractLogger(final ExtendedLogger logger) { - super(logger.getName(), null); - final Level javaLevel = LevelTranslator.toJavaLevel(logger.getLevel()); - super.setLevel(javaLevel); - this.logger = logger; - } - - @Override - public void log(final LogRecord record) { - final org.apache.logging.log4j.Level level = LevelTranslator.toLevel(record.getLevel()); - final Object[] parameters = record.getParameters(); - final MessageFactory messageFactory = logger.getMessageFactory(); - final Message message = parameters == null - ? messageFactory.newMessage(record.getMessage()) /* LOG4J2-1251: not formatted case */ - : messageFactory.newMessage(record.getMessage(), parameters); - final Throwable thrown = record.getThrown(); - logger.logIfEnabled(FQCN, level, null, message, thrown); - } - - // - // Methods - - @Override - public abstract void setFilter(Filter newFilter) throws SecurityException; - - @Override - public abstract void setLevel(Level newLevel) throws SecurityException; - - @Override - public abstract void addHandler(Handler handler) throws SecurityException; - - @Override - public abstract void removeHandler(Handler handler) throws SecurityException; - - @Override - public abstract void setUseParentHandlers(boolean useParentHandlers); - - @Override - public abstract void setParent(Logger parent); - - @Override - public Filter getFilter() { - return null; - } - - /** - * Returns the configured level of a logger. - *

- * Implementation note: this method returns the level explicitly configured - * in the Log4j API logging implementation and is implementation specific. - * The default implementation always returns {@code null}. - *

- *

- * To test if a logger is enabled for a specific logging level, i.e. to test its effective - * level, use {@link Logger#isLoggable(Level)}. - *

- * @see #isLoggable(Level) - */ - @Override - public Level getLevel() { - return null; - } - - @Override - public Handler[] getHandlers() { - return new Handler[0]; - } - - @Override - public boolean getUseParentHandlers() { - return false; - } - - @Override - public Logger getParent() { - return null; - } - - //
- - // - // Implementation of methods used for logging - - @Override - public boolean isLoggable(final Level level) { - return logger.isEnabled(LevelTranslator.toLevel(level)); - } - - @Override - public String getName() { - return logger.getName(); - } - - private org.apache.logging.log4j.util.Supplier toLog4jSupplier(Supplier msgSupplier) { - return msgSupplier::get; - } - - private org.apache.logging.log4j.util.Supplier toMessageSupplier(Supplier msgSupplier) { - return () -> logger.getMessageFactory().newMessage(msgSupplier.get()); - } - - private org.apache.logging.log4j.util.Supplier toMessageSupplier(ResourceBundle bundle, String msg) { - return () -> new LocalizedMessage(bundle, msg); - } - - private org.apache.logging.log4j.util.Supplier toMessageSupplier( - ResourceBundle bundle, String msg, Object[] params) { - return () -> new LocalizedMessage(bundle, msg, params); - } - - private StackTraceElement toLocation(String sourceClass, String sourceMethod) { - return new StackTraceElement(sourceClass, sourceMethod, null, 0); - } - - @Override - public void log(final Level level, final String msg) { - logger.logIfEnabled(FQCN, LevelTranslator.toLevel(level), null, msg); - } - - /** - * @since 3.0.0 - */ - @Override - public void log(Level level, Supplier msgSupplier) { - logger.logIfEnabled(FQCN, LevelTranslator.toLevel(level), null, toLog4jSupplier(msgSupplier), null); - } - - @Override - public void log(final Level level, final String msg, final Object param1) { - logger.logIfEnabled(FQCN, LevelTranslator.toLevel(level), null, msg, param1); - } - - @Override - public void log(final Level level, final String msg, final Object[] params) { - logger.logIfEnabled(FQCN, LevelTranslator.toLevel(level), null, msg, params); - } - - @Override - public void log(final Level level, final String msg, final Throwable thrown) { - logger.logIfEnabled(FQCN, LevelTranslator.toLevel(level), null, msg, thrown); - } - - /** - * @since 3.0.0 - */ - @Override - public void log(Level level, Throwable thrown, Supplier msgSupplier) { - logger.logIfEnabled(FQCN, LevelTranslator.toLevel(level), null, toLog4jSupplier(msgSupplier), thrown); - } - - @Override - public void logp(final Level level, final String sourceClass, final String sourceMethod, final String msg) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .log(msg); - } - - /** - * @since 3.0.0 - */ - @Override - public void logp(Level level, String sourceClass, String sourceMethod, Supplier msgSupplier) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .log(toMessageSupplier(msgSupplier)); - } - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final String msg, - final Object param1) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .log(msg, param1); - } - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final String msg, - final Object[] params) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .log(msg, params); - } - - @Override - public void logp( - final Level level, - final String sourceClass, - final String sourceMethod, - final String msg, - final Throwable thrown) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .withThrowable(thrown) - .log(msg); - } - - /** - * @since 3.0.0 - */ - @Override - public void logp( - Level level, String sourceClass, String sourceMethod, Throwable thrown, Supplier msgSupplier) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .withThrowable(thrown) - .log(toMessageSupplier(msgSupplier)); - } - - /** - * @since 3.0.0 - */ - @Override - public void logrb( - Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String msg, Object... params) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .log(toMessageSupplier(bundle, msg, params)); - } - - @Override - public void logrb( - Level level, String sourceClass, String sourceMethod, ResourceBundle bundle, String msg, Throwable thrown) { - logger.atLevel(LevelTranslator.toLevel(level)) - .withLocation(toLocation(sourceClass, sourceMethod)) - .withThrowable(thrown) - .log(toMessageSupplier(bundle, msg)); - } - - /** - * @since 3.0.0 - */ - @Override - public void logrb(Level level, ResourceBundle bundle, String msg, Object... params) { - logger.logIfEnabled(FQCN, LevelTranslator.toLevel(level), null, toMessageSupplier(bundle, msg, params), null); - } - - /** - * @since 3.0.0 - */ - @Override - public void logrb(Level level, ResourceBundle bundle, String msg, Throwable thrown) { - LogBuilder builder = logger.atLevel(LevelTranslator.toLevel(level)).withThrowable(thrown); - if (builder instanceof BridgeAware bridgeAware) { - bridgeAware.setEntryPoint(FQCN); - } - builder.log(toMessageSupplier(bundle, msg)); - } - - @Override - public void entering(final String sourceClass, final String sourceMethod) { - logger.atTrace() - .withLocation(toLocation(sourceClass, sourceMethod)) - .withMarker(ENTRY_MARKER) - .log(DefaultFlowMessageFactory.INSTANCE.newEntryMessage(null, (Object[]) null)); - } - - @Override - public void entering(final String sourceClass, final String sourceMethod, final Object param1) { - logger.atTrace() - .withLocation(toLocation(sourceClass, sourceMethod)) - .withMarker(ENTRY_MARKER) - .log(DefaultFlowMessageFactory.INSTANCE.newEntryMessage(null, param1)); - } - - @Override - public void entering(final String sourceClass, final String sourceMethod, final Object[] params) { - logger.atTrace() - .withLocation(toLocation(sourceClass, sourceMethod)) - .withMarker(ENTRY_MARKER) - .log(DefaultFlowMessageFactory.INSTANCE.newEntryMessage(null, params)); - } - - @Override - public void exiting(final String sourceClass, final String sourceMethod) { - logger.atTrace() - .withLocation(toLocation(sourceClass, sourceMethod)) - .withMarker(EXIT_MARKER) - .log(DefaultFlowMessageFactory.INSTANCE.newExitMessage(null, (Object) null)); - } - - @Override - public void exiting(final String sourceClass, final String sourceMethod, final Object result) { - logger.atTrace() - .withLocation(toLocation(sourceClass, sourceMethod)) - .withMarker(EXIT_MARKER) - .log(DefaultFlowMessageFactory.INSTANCE.newExitMessage(null, result)); - } - - @Override - public void throwing(final String sourceClass, final String sourceMethod, final Throwable thrown) { - logger.atTrace() - .withLocation(toLocation(sourceClass, sourceMethod)) - .withMarker(THROWING_MARKER) - .withThrowable(thrown) - .log("Throwing"); - } - - @Override - public void severe(final String msg) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.ERROR, null, msg); - } - - /** - * @since 3.0.0 - */ - @Override - public void severe(Supplier msgSupplier) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.ERROR, null, toLog4jSupplier(msgSupplier), null); - } - - @Override - public void warning(final String msg) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.WARN, null, msg); - } - - @Override - public void warning(Supplier msgSupplier) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.WARN, null, toLog4jSupplier(msgSupplier), null); - } - - @Override - public void info(final String msg) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.INFO, null, msg); - } - - @Override - public void info(Supplier msgSupplier) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.INFO, null, toLog4jSupplier(msgSupplier), null); - } - - @Override - public void config(final String msg) { - logger.logIfEnabled(FQCN, LevelTranslator.CONFIG, null, msg); - } - - @Override - public void config(Supplier msgSupplier) { - logger.logIfEnabled(FQCN, LevelTranslator.CONFIG, null, toLog4jSupplier(msgSupplier), null); - } - - @Override - public void fine(final String msg) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.DEBUG, null, msg); - } - - @Override - public void fine(Supplier msgSupplier) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.DEBUG, null, toLog4jSupplier(msgSupplier), null); - } - - @Override - public void finer(final String msg) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.TRACE, null, msg); - } - - @Override - public void finer(Supplier msgSupplier) { - logger.logIfEnabled(FQCN, org.apache.logging.log4j.Level.TRACE, null, toLog4jSupplier(msgSupplier), null); - } - - @Override - public void finest(final String msg) { - logger.logIfEnabled(FQCN, LevelTranslator.FINEST, null, msg); - } - - @Override - public void finest(Supplier msgSupplier) { - logger.logIfEnabled(FQCN, LevelTranslator.FINEST, null, toLog4jSupplier(msgSupplier), null); - } - // -} diff --git a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/support/package-info.java b/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/support/package-info.java deleted file mode 100644 index 8a9b13054b2..00000000000 --- a/jul-to-log4j/src/main/java/org/apache/logging/jul/tolog4j/support/package-info.java +++ /dev/null @@ -1,26 +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. - */ -/** - * Utility classes that can be used in implementing providing implementation of the classes in - * {@link org.apache.logging.log4j.jul.spi}. - */ -@Export -@Version("3.0.0") -package org.apache.logging.jul.tolog4j.support; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/jul-to-log4j/src/main/resources/META-INF/log4j/propertyMapping.json b/jul-to-log4j/src/main/resources/META-INF/log4j/propertyMapping.json deleted file mode 100644 index f88a32870c7..00000000000 --- a/jul-to-log4j/src/main/resources/META-INF/log4j/propertyMapping.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "jul": { - "levelConverter": [ - "log4j2.julLevelConverter" - ], - "loggerAdapter": [ - "log4j2.julLoggerAdapter" - ] - } -} \ No newline at end of file diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/AsyncLoggerThreadsTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/AsyncLoggerThreadsTest.java deleted file mode 100644 index 1ec5eb6b166..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/AsyncLoggerThreadsTest.java +++ /dev/null @@ -1,59 +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.logging.jul.tolog4j.test; - -import static org.junit.Assert.assertEquals; - -import java.util.List; -import java.util.stream.Collectors; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.async.logger.AsyncLoggerContextSelector; -import org.apache.logging.log4j.core.test.TestConstants; -import org.apache.logging.log4j.core.test.categories.AsyncLoggers; -import org.jspecify.annotations.Nullable; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -@Category(AsyncLoggers.class) -public class AsyncLoggerThreadsTest { - - static @Nullable String oldSelector; - - @BeforeClass - public static void beforeClass() { - oldSelector = TestConstants.setSystemProperty( - TestConstants.LOGGER_CONTEXT_SELECTOR, AsyncLoggerContextSelector.class.getName()); - System.setProperty("java.util.logging.manager", LogManager.class.getName()); - } - - @AfterClass - public static void afterClass() { - TestConstants.setSystemProperty(TestConstants.LOGGER_CONTEXT_SELECTOR, oldSelector); - System.clearProperty("java.util.logging.manager"); - } - - @Test - public void testAsyncLoggerThreads() { - LogManager.getLogger("com.foo.Bar").info("log"); - final List asyncLoggerThreads = Thread.getAllStackTraces().keySet().stream() - .filter(thread -> thread.getName().matches("Log4j2-TF.*AsyncLogger.*")) - .collect(Collectors.toList()); - assertEquals(asyncLoggerThreads.toString(), 1, asyncLoggerThreads.size()); - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/BracketInNotInterpolatedMessageTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/BracketInNotInterpolatedMessageTest.java deleted file mode 100644 index 3bb6fd0afca..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/BracketInNotInterpolatedMessageTest.java +++ /dev/null @@ -1,58 +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.logging.jul.tolog4j.test; - -import static java.util.logging.Level.INFO; -import static org.hamcrest.Matchers.hasSize; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; - -import java.util.List; -import java.util.logging.LogRecord; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LogManager; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -public class BracketInNotInterpolatedMessageTest { - - @BeforeClass - public static void setUpClass() { - System.setProperty("java.util.logging.manager", LogManager.class.getName()); - } - - @AfterClass - public static void tearDownClass() { - System.clearProperty("java.util.logging.manager"); - } - - @Test - public void noInterpolation() { - final Logger logger = Logger.getLogger("Test"); - logger.info("{raw}"); - logger.log( - new LogRecord(INFO, "{raw}")); // should lead to the same as previous but was not the case LOG4J2-1251 - final List events = - ListAppender.getListAppender("TestAppender").getEvents(); - assertThat(events, hasSize(2)); - assertEquals("{raw}", events.get(0).getMessage().getFormattedMessage()); - assertEquals("{raw}", events.get(1).getMessage().getFormattedMessage()); - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/CallerInformationTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/CallerInformationTest.java deleted file mode 100644 index 1562b70169e..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/CallerInformationTest.java +++ /dev/null @@ -1,162 +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.logging.jul.tolog4j.test; - -import static org.junit.Assert.assertEquals; - -import java.util.List; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LogManager; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; - -public class CallerInformationTest { - - private static final String PARAM_1 = "PARAM_1"; - private static final String[] PARAMS = {PARAM_1, "PARAM_2"}; - private static final String SOURCE_CLASS = "SourceClass"; - private static final String SOURCE_METHOD = "sourceMethod"; - - @Rule - public final LoggerContextRule ctx = new LoggerContextRule("CallerInformationTest.xml"); - - @BeforeClass - public static void setUpClass() { - System.setProperty("java.util.logging.manager", LogManager.class.getName()); - } - - @AfterClass - public static void tearDownClass() { - System.clearProperty("java.util.logging.manager"); - } - - @Test - public void testClassLogger() { - final ListAppender app = ctx.getListAppender("Class").clear(); - final Logger logger = Logger.getLogger("ClassLogger"); - // Eager methods - logger.severe("CATASTROPHE INCOMING!"); - logger.warning("ZOMBIES!!!"); - logger.info("brains~~~"); - logger.config("Config!"); - logger.fine("Itchy. Tasty."); - logger.finer("Finer message."); - logger.finest("Finest message."); - logger.log(Level.FINEST, "Finest message."); - logger.log(Level.FINEST, "Message of level {1}.", Level.FINEST); - logger.log(Level.FINEST, "Hello {1} and {2}!.", new Object[] {"foo", "bar"}); - // Lazy methods - logger.severe(() -> "CATASTROPHE INCOMING!"); - logger.warning(() -> "ZOMBIES!!!"); - logger.info(() -> "brains~~~"); - logger.config(() -> "Config!"); - logger.fine(() -> "Itchy. Tasty."); - logger.finer(() -> "Finer message."); - logger.finest(() -> "Finest message."); - logger.log(Level.FINEST, () -> "Finest message."); - logger.log(Level.FINEST, new RuntimeException(), () -> "Message with exception."); - List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 19, messages.size()); - for (int i = 0; i < messages.size(); i++) { - String message = messages.get(i); - assertEquals( - "Incorrect caller class name for message " + i, - this.getClass().getName(), - message); - } - - // Test passing the location information directly - app.clear(); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello!"); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello {1}!", PARAM_1); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello {1} and {2}!", PARAMS); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello!", new RuntimeException()); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, () -> "Hello" + PARAM_1 + "!"); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, new RuntimeException(), () -> "Hello " + PARAM_1 + "!"); - logger.entering(SOURCE_CLASS, SOURCE_METHOD); - logger.entering(SOURCE_CLASS, SOURCE_METHOD, PARAM_1); - logger.entering(SOURCE_CLASS, SOURCE_METHOD, PARAMS); - logger.exiting(SOURCE_CLASS, SOURCE_METHOD); - logger.exiting(SOURCE_CLASS, SOURCE_METHOD, PARAM_1); - logger.throwing(SOURCE_CLASS, SOURCE_METHOD, new RuntimeException()); - messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 12, messages.size()); - for (int i = 0; i < messages.size(); i++) { - String message = messages.get(i); - assertEquals("Incorrect caller class name for message " + i, SOURCE_CLASS, message); - } - } - - @Test - public void testMethodLogger() { - final ListAppender app = ctx.getListAppender("Method").clear(); - final Logger logger = Logger.getLogger("MethodLogger"); - // Eager methods - logger.severe("CATASTROPHE INCOMING!"); - logger.warning("ZOMBIES!!!"); - logger.info("brains~~~"); - logger.config("Config!"); - logger.fine("Itchy. Tasty."); - logger.finer("Finer message."); - logger.finest("Finest message."); - logger.log(Level.FINEST, "Finest message."); - logger.log(Level.FINEST, "Message of level {1}.", Level.FINEST); - logger.log(Level.FINEST, "Hello {1} and {2}!.", new Object[] {"foo", "bar"}); - // Lazy methods - logger.severe(() -> "CATASTROPHE INCOMING!"); - logger.warning(() -> "ZOMBIES!!!"); - logger.info(() -> "brains~~~"); - logger.config(() -> "Config!"); - logger.fine(() -> "Itchy. Tasty."); - logger.finer(() -> "Finer message."); - logger.finest(() -> "Finest message."); - logger.log(Level.FINEST, () -> "Finest message."); - logger.log(Level.FINEST, new RuntimeException(), () -> "Message with exception."); - List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 19, messages.size()); - for (int i = 0; i < messages.size(); i++) { - String message = messages.get(i); - assertEquals("Incorrect caller class name for message " + i, "testMethodLogger", message); - } - - // Test passing the location information directly - app.clear(); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello!"); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello {1}!", PARAM_1); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello {1} and {2}!", PARAMS); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, "Hello!", new RuntimeException()); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, () -> "Hello " + PARAM_1 + "!"); - logger.logp(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, new RuntimeException(), () -> "Hello " + PARAM_1 + "!"); - logger.entering(SOURCE_CLASS, SOURCE_METHOD); - logger.entering(SOURCE_CLASS, SOURCE_METHOD, PARAM_1); - logger.entering(SOURCE_CLASS, SOURCE_METHOD, PARAMS); - logger.exiting(SOURCE_CLASS, SOURCE_METHOD); - logger.exiting(SOURCE_CLASS, SOURCE_METHOD, PARAM_1); - logger.throwing(SOURCE_CLASS, SOURCE_METHOD, new RuntimeException()); - messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 12, messages.size()); - for (int i = 0; i < messages.size(); i++) { - String message = messages.get(i); - assertEquals("Incorrect caller class name for message " + i, SOURCE_METHOD, message); - } - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/JavaLevelTranslatorTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/JavaLevelTranslatorTest.java deleted file mode 100644 index 76744a3959d..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/JavaLevelTranslatorTest.java +++ /dev/null @@ -1,72 +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.logging.jul.tolog4j.test; - -import static org.junit.Assert.assertEquals; - -import java.util.Arrays; -import java.util.Collection; -import org.apache.logging.jul.tolog4j.LevelTranslator; -import org.apache.logging.log4j.Level; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -/** - * Tests that all JUL levels are mapped to a Log4j level. - */ -@RunWith(Parameterized.class) -public class JavaLevelTranslatorTest { - - private final java.util.logging.Level javaLevel; - private final Level log4jLevel; - - public JavaLevelTranslatorTest(final java.util.logging.Level javaLevel, final Level log4jLevel) { - this.javaLevel = javaLevel; - this.log4jLevel = log4jLevel; - } - - @Parameterized.Parameters - public static Collection data() { - return Arrays.asList(new Object[][] { - // All 9 JUL levels, All 8 Log4j levels and extras - // @formatter:off - {java.util.logging.Level.OFF, Level.OFF}, - {java.util.logging.Level.SEVERE, Level.ERROR}, - {java.util.logging.Level.WARNING, Level.WARN}, - {java.util.logging.Level.INFO, Level.INFO}, - {java.util.logging.Level.CONFIG, LevelTranslator.CONFIG}, - {java.util.logging.Level.FINE, Level.DEBUG}, - {java.util.logging.Level.FINER, Level.TRACE}, - {java.util.logging.Level.FINEST, LevelTranslator.FINEST}, - {java.util.logging.Level.ALL, Level.ALL} - // @formatter:on - }); - } - - @Test - public void testToLevel() throws Exception { - final Level actualLevel = LevelTranslator.toLevel(javaLevel); - assertEquals(log4jLevel, actualLevel); - } - - @Test - public void testToJavaLevel() throws Exception { - final java.util.logging.Level actualLevel = LevelTranslator.toJavaLevel(log4jLevel); - assertEquals(javaLevel, actualLevel); - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/JulTestProperties.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/JulTestProperties.java deleted file mode 100644 index 28c4b2f6122..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/JulTestProperties.java +++ /dev/null @@ -1,24 +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.logging.jul.tolog4j.test; - -public final class JulTestProperties { - - public static final String JUL_LOGGER_ADAPTER = "log4j.jul.loggerAdapter"; - - private JulTestProperties() {} -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/Log4jLevelTranslatorTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/Log4jLevelTranslatorTest.java deleted file mode 100644 index 43b6796a2a0..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/Log4jLevelTranslatorTest.java +++ /dev/null @@ -1,65 +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.logging.jul.tolog4j.test; - -import static org.junit.Assert.assertEquals; - -import java.util.Arrays; -import java.util.Collection; -import org.apache.logging.jul.tolog4j.LevelTranslator; -import org.apache.logging.log4j.Level; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; - -/** - * Tests that all Log4j levels are mapped to a JUL level. - */ -@RunWith(Parameterized.class) -public class Log4jLevelTranslatorTest { - - private final java.util.logging.Level javaLevel; - private final Level log4jLevel; - - public Log4jLevelTranslatorTest(final java.util.logging.Level javaLevel, final Level log4jLevel) { - this.javaLevel = javaLevel; - this.log4jLevel = log4jLevel; - } - - @Parameterized.Parameters - public static Collection data() { - return Arrays.asList(new Object[][] { - // Some JUL levels, All 8 Log4j levels - // @formatter:off - {java.util.logging.Level.OFF, Level.OFF}, - {java.util.logging.Level.SEVERE, Level.FATAL}, - {java.util.logging.Level.SEVERE, Level.ERROR}, - {java.util.logging.Level.WARNING, Level.WARN}, - {java.util.logging.Level.INFO, Level.INFO}, - {java.util.logging.Level.FINE, Level.DEBUG}, - {java.util.logging.Level.FINER, Level.TRACE}, - {java.util.logging.Level.ALL, Level.ALL}, - // @formatter:on - }); - } - - @Test - public void testToJavaLevel() throws Exception { - final java.util.logging.Level actualLevel = LevelTranslator.toJavaLevel(log4jLevel); - assertEquals(javaLevel, actualLevel); - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/ResourceBundleTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/ResourceBundleTest.java deleted file mode 100644 index ed94daa6e47..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/ResourceBundleTest.java +++ /dev/null @@ -1,91 +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.logging.jul.tolog4j.test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; - -import java.util.ResourceBundle; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LogManager; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; - -/** - * Test methods that accept a resource bundle - */ -public class ResourceBundleTest { - - private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("ResourceBundleTest"); - private static final String SOURCE_CLASS = "SourceClass"; - private static final String SOURCE_METHOD = "sourceMethod"; - - private static final String[] EXPECTED_MESSAGES = {"Hello!", "Hello Log4j and JUL!"}; - private static final String[] EXPECTED_CLASS_NAMES = {ResourceBundleTest.class.getName(), SOURCE_CLASS}; - private static final String[] EXPECTED_METHOD_NAMES = {"testCorrectMessageAndLocation", SOURCE_METHOD}; - - @Rule - public final LoggerContextRule ctx = new LoggerContextRule("ResourceBundleTest.xml"); - - @BeforeClass - public static void setUpClass() { - System.setProperty("java.util.logging.manager", LogManager.class.getName()); - } - - @AfterClass - public static void tearDownClass() { - System.clearProperty("java.util.logging.manager"); - } - - @Test - public void testCorrectMessageAndLocation() { - ListAppender appender = ctx.getListAppender("LIST").clear(); - Logger logger = Logger.getLogger(ResourceBundleTest.class.getName()); - - Throwable thrown = new RuntimeException(); - logger.logrb(Level.INFO, BUNDLE, "msg_1", thrown); - logger.logrb(Level.INFO, BUNDLE, "msg_2", "Log4j", "JUL"); - logger.logrb(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, BUNDLE, "msg_1", thrown); - logger.logrb(Level.INFO, SOURCE_CLASS, SOURCE_METHOD, BUNDLE, "msg_2", "Log4j", "JUL"); - - LogEvent[] logEvents = appender.getEvents().toArray(LogEvent[]::new); - for (int idx = 0; idx < logEvents.length; ++idx) { - assertEquals( - String.format("Message of event %d", idx), - EXPECTED_MESSAGES[idx % 2], - logEvents[idx].getMessage().getFormattedMessage()); - assertEquals( - String.format("Source class of event %d", idx), - EXPECTED_CLASS_NAMES[idx / 2], - logEvents[idx].getSource().getClassName()); - assertEquals( - String.format("Source method of event %d", idx), - EXPECTED_METHOD_NAMES[idx / 2], - logEvents[idx].getSource().getMethodName()); - assertSame( - String.format("Exception of event %d", idx), - idx % 2 == 0 ? thrown : null, - logEvents[idx].getThrown()); - } - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/ApiLoggerTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/ApiLoggerTest.java deleted file mode 100644 index b7da8115fc0..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/ApiLoggerTest.java +++ /dev/null @@ -1,91 +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.logging.jul.tolog4j.test.internal; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LogManager; -import org.apache.logging.jul.tolog4j.test.support.AbstractLoggerTest; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -public class ApiLoggerTest extends AbstractLoggerTest { - - @BeforeClass - public static void setUpClass() { - System.setProperty("java.util.logging.manager", LogManager.class.getName()); - } - - @AfterClass - public static void tearDownClass() { - System.clearProperty("java.util.logging.manager"); - } - - @Before - public void setUp() { - logger = Logger.getLogger(LOGGER_NAME); - logger.setFilter(null); - assertThat(logger.isLoggable(java.util.logging.Level.FINE)) - .as("Level %s is enabled", java.util.logging.Level.FINE) - .isTrue(); - assertThat(logger.isLoggable(java.util.logging.Level.FINER)) - .as("Level %s is enabled", java.util.logging.Level.FINER) - .isFalse(); - eventAppender = ListAppender.getListAppender("TestAppender"); - flowAppender = ListAppender.getListAppender("FlowAppender"); - stringAppender = ListAppender.getListAppender("StringAppender"); - assertNotNull(eventAppender); - assertNotNull(flowAppender); - assertNotNull(stringAppender); - } - - @After - public void tearDown() { - if (eventAppender != null) { - eventAppender.clear(); - } - if (flowAppender != null) { - flowAppender.clear(); - } - if (stringAppender != null) { - stringAppender.clear(); - } - } - - @Test - public void testGetParent() { - final Logger parent = logger.getParent(); - assertNull("No parent logger should be automatically set up using log4j-api", parent); - } - - @Test(expected = UnsupportedOperationException.class) - public void testSetParentFails() { - logger.setParent(null); - } - - @Test - public void testSetLevelFails() { - logger.setLevel(null); - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/DefaultLevelConverterCustomJulLevelsTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/DefaultLevelConverterCustomJulLevelsTest.java deleted file mode 100644 index 825b476c489..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/DefaultLevelConverterCustomJulLevelsTest.java +++ /dev/null @@ -1,144 +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.logging.jul.tolog4j.test.internal; - -import org.apache.logging.jul.tolog4j.LevelTranslator; -import org.apache.logging.jul.tolog4j.internal.DefaultLevelConverter; -import org.apache.logging.log4j.Level; -import org.junit.Assert; -import org.junit.Test; - -/** - * Tests {@link DefaultLevelConverter} for custom JUL levels. - * - * @since 2.4 - */ -public class DefaultLevelConverterCustomJulLevelsTest { - - static class CustomLevel extends java.util.logging.Level { - - static CustomLevel ALL_P_1 = new CustomLevel("ALL_P_1", java.util.logging.Level.ALL.intValue() + 1); - - static CustomLevel FINEST_P_1 = new CustomLevel("FINEST_P_1", java.util.logging.Level.FINEST.intValue() + 1); - static CustomLevel FINEST_M_1 = new CustomLevel("FINEST_M_1", java.util.logging.Level.FINEST.intValue() - 1); - - static CustomLevel FINER_P_1 = new CustomLevel("FINER_P_1", java.util.logging.Level.FINER.intValue() + 1); - static CustomLevel FINER_M_1 = new CustomLevel("FINER_M_1", java.util.logging.Level.FINER.intValue() - 1); - - static CustomLevel FINE_P_1 = new CustomLevel("FINE_P_1", java.util.logging.Level.FINE.intValue() + 1); - static CustomLevel FINE_M_1 = new CustomLevel("FINE_M_1", java.util.logging.Level.FINE.intValue() - 1); - - static CustomLevel CONFIG_P_1 = new CustomLevel("CONFIG_P_1", java.util.logging.Level.CONFIG.intValue() + 1); - static CustomLevel CONFIG_M_1 = new CustomLevel("CONFIG_M_1", java.util.logging.Level.CONFIG.intValue() - 1); - - static CustomLevel INFO_P_1 = new CustomLevel("INFO_P_1", java.util.logging.Level.INFO.intValue() + 1); - static CustomLevel INFO_M_1 = new CustomLevel("INFO_M_1", java.util.logging.Level.INFO.intValue() - 1); - - static CustomLevel WARNING_P_1 = new CustomLevel("WARNING_P_1", java.util.logging.Level.WARNING.intValue() + 1); - static CustomLevel WARNING_M_1 = new CustomLevel("WARNING_M_1", java.util.logging.Level.WARNING.intValue() - 1); - - static CustomLevel SEVERE_P_1 = new CustomLevel("SEVERE_P_1", java.util.logging.Level.SEVERE.intValue() + 1); - static CustomLevel SEVERE_M_1 = new CustomLevel("SEVERE_M_1", java.util.logging.Level.SEVERE.intValue() - 1); - - static CustomLevel OFF_M_1 = new CustomLevel("OFF_M_1", java.util.logging.Level.OFF.intValue() - 1); - - protected CustomLevel(final String name, final int value) { - super(name, value); - } - } - - private final DefaultLevelConverter converter = new DefaultLevelConverter(); - - @Test - public void testCustomJulLevelNearAll() { - // Sanity check: - Assert.assertEquals(Level.ALL, converter.toLevel(java.util.logging.Level.ALL)); - // Test: - Assert.assertEquals(Level.ALL, converter.toLevel(CustomLevel.ALL_P_1)); - } - - @Test - public void testCustomJulLevelNearFinest() { - // Sanity check: - Assert.assertEquals(LevelTranslator.FINEST, converter.toLevel(java.util.logging.Level.FINEST)); - // Test: - Assert.assertEquals(LevelTranslator.FINEST, converter.toLevel(CustomLevel.FINEST_P_1)); - Assert.assertEquals(LevelTranslator.FINEST, converter.toLevel(CustomLevel.FINEST_M_1)); - } - - @Test - public void testCustomJulLevelNearFiner() { - // Sanity check: - Assert.assertEquals(Level.TRACE, converter.toLevel(java.util.logging.Level.FINER)); - // Test: - Assert.assertEquals(Level.TRACE, converter.toLevel(CustomLevel.FINER_P_1)); - Assert.assertEquals(Level.TRACE, converter.toLevel(CustomLevel.FINER_M_1)); - } - - @Test - public void testCustomJulLevelNearFine() { - // Sanity check: - Assert.assertEquals(Level.DEBUG, converter.toLevel(java.util.logging.Level.FINE)); - // Test: - Assert.assertEquals(Level.DEBUG, converter.toLevel(CustomLevel.FINE_P_1)); - Assert.assertEquals(Level.DEBUG, converter.toLevel(CustomLevel.FINE_M_1)); - } - - @Test - public void testCustomJulLevelNearConfig() { - // Sanity check: - Assert.assertEquals(LevelTranslator.CONFIG, converter.toLevel(java.util.logging.Level.CONFIG)); - // Test: - Assert.assertEquals(LevelTranslator.CONFIG, converter.toLevel(CustomLevel.CONFIG_P_1)); - Assert.assertEquals(LevelTranslator.CONFIG, converter.toLevel(CustomLevel.CONFIG_M_1)); - } - - @Test - public void testCustomJulLevelNearInfo() { - // Sanity check: - Assert.assertEquals(Level.INFO, converter.toLevel(java.util.logging.Level.INFO)); - // Test: - Assert.assertEquals(Level.INFO, converter.toLevel(CustomLevel.INFO_P_1)); - Assert.assertEquals(Level.INFO, converter.toLevel(CustomLevel.INFO_M_1)); - } - - @Test - public void testCustomJulLevelNearWarning() { - // Sanity check: - Assert.assertEquals(Level.WARN, converter.toLevel(java.util.logging.Level.WARNING)); - // Test: - Assert.assertEquals(Level.WARN, converter.toLevel(CustomLevel.WARNING_P_1)); - Assert.assertEquals(Level.WARN, converter.toLevel(CustomLevel.WARNING_M_1)); - } - - @Test - public void testCustomJulLevelNearSevere() { - // Sanity check: - Assert.assertEquals(Level.ERROR, converter.toLevel(java.util.logging.Level.SEVERE)); - // Test: - Assert.assertEquals(Level.ERROR, converter.toLevel(CustomLevel.SEVERE_P_1)); - Assert.assertEquals(Level.ERROR, converter.toLevel(CustomLevel.SEVERE_M_1)); - } - - @Test - public void testCustomJulLevelNearOff() { - // Sanity check: - Assert.assertEquals(Level.OFF, converter.toLevel(java.util.logging.Level.OFF)); - // Test: - Assert.assertEquals(Level.OFF, converter.toLevel(CustomLevel.OFF_M_1)); - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/DefaultLevelConverterTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/DefaultLevelConverterTest.java deleted file mode 100644 index f93a4f4ca54..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/internal/DefaultLevelConverterTest.java +++ /dev/null @@ -1,33 +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.logging.jul.tolog4j.test.internal; - -import static org.junit.Assert.assertNull; - -import org.apache.logging.jul.tolog4j.internal.DefaultLevelConverter; -import org.junit.Test; - -public class DefaultLevelConverterTest { - - /** - * (LOG4J2-1108) NullPointerException when passing null to java.util.logging.Logger.setLevel(). - */ - @Test - public void testJulSetNull() { - assertNull(new DefaultLevelConverter().toLevel(null)); - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/support/AbstractLoggerTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/support/AbstractLoggerTest.java deleted file mode 100644 index 9e620cdffc9..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/support/AbstractLoggerTest.java +++ /dev/null @@ -1,185 +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.logging.jul.tolog4j.test.support; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LevelTranslator; -import org.apache.logging.jul.tolog4j.support.AbstractLogger; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.impl.MementoLogEvent; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.junit.Test; - -/** - * - */ -public abstract class AbstractLoggerTest { - public static final String LOGGER_NAME = "Test"; - protected Logger logger; - protected ListAppender eventAppender; - protected ListAppender flowAppender; - protected ListAppender stringAppender; - - @Test - public void testGetName() { - assertThat(logger.getName()).isEqualTo(LOGGER_NAME); - } - - @Test - public void testGlobalLogger() { - final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); - root.info("Test info message"); - root.config("Test info message"); - root.fine("Test info message"); - final List events = eventAppender.getEvents(); - assertThat(events).hasSize(3); - for (final LogEvent event : events) { - final String message = event.getMessage().getFormattedMessage(); - assertThat(message).isEqualTo("Test info message"); - } - } - - @Test - public void testGlobalLoggerName() { - final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); - assertThat(root.getName()).isEqualTo(Logger.GLOBAL_LOGGER_NAME); - } - - @Test - public void testIsLoggable() { - assertThat(logger.isLoggable(java.util.logging.Level.SEVERE)).isTrue(); - } - - @Test - public void testLog() { - logger.info("Informative message here."); - final List events = eventAppender.getEvents(); - assertThat(events).hasSize(1); - final LogEvent event = events.get(0); - assertThat(event).isInstanceOf(MementoLogEvent.class); - assertThat(event.getLevel()).isEqualTo(Level.INFO); - assertThat(event.getLoggerName()).isEqualTo(LOGGER_NAME); - assertThat(event.getMessage().getFormattedMessage()).isEqualTo("Informative message here."); - assertThat(event.getLoggerFqcn()).isEqualTo(AbstractLogger.class.getName()); - } - - @Test - public void testLogParamMarkers() { - final Logger flowLogger = Logger.getLogger("TestFlow"); - flowLogger.logp(java.util.logging.Level.FINER, "sourceClass", "sourceMethod", "ENTER {0}", "params"); - final List events = flowAppender.getEvents(); - assertThat(events.get(0).getMessage().getFormattedMessage()).isEqualTo("ENTER params"); - } - - @Test - public void testLogUsingCustomLevel() { - logger.config("Config level"); - final List events = eventAppender.getEvents(); - assertThat(events).hasSize(1); - final LogEvent event = events.get(0); - assertThat(event.getLevel()).isEqualTo(LevelTranslator.CONFIG); - } - - @Test - public void testLogWithCallingClass() { - final Logger log = Logger.getLogger("Test.CallerClass"); - log.config("Calling from LoggerTest"); - final List messages = stringAppender.getMessages(); - assertThat(messages).hasSize(1); - final String message = messages.get(0); - assertThat(message).isEqualTo(AbstractLoggerTest.class.getName()); - } - - @Test - public void testCurlyBraces() { - testMessage("{message}"); - } - - @Test - public void testPercent() { - testMessage("message%s"); - } - - @Test - public void testPercentAndCurlyBraces() { - testMessage("message{%s}"); - } - - private void testMessage(final String string) { - final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); - root.info("Test info " + string); - root.config("Test info " + string); - root.fine("Test info " + string); - final List events = eventAppender.getEvents(); - assertThat(events).hasSize(3); - for (final LogEvent event : events) { - final String message = event.getMessage().getFormattedMessage(); - assertThat(message).isEqualTo("Test info " + string); - } - } - - @Test - public void testFlowMessages() { - final Logger flowLogger = Logger.getLogger("TestFlow"); - flowLogger.entering("com.example.TestSourceClass1", "testSourceMethod1(String)"); - flowLogger.entering("com.example.TestSourceClass2", "testSourceMethod2(String)", "TestParam"); - flowLogger.entering( - "com.example.TestSourceClass3", "testSourceMethod3(String)", new Object[] {"TestParam0", "TestParam1"}); - final List events = flowAppender.getEvents(); - assertThat(events).hasSize(3); - assertThat(events.get(0).getMessage().getFormattedMessage()).isEqualTo("Enter"); - assertThat(events.get(1).getMessage().getFormattedMessage()).isEqualTo("Enter params(TestParam)"); - assertThat(events.get(2).getMessage().getFormattedMessage()).isEqualTo("Enter params(TestParam0, TestParam1)"); - } - - @Test - public void testLambdasGlobalLogger() { - testLambdaMessages("message"); - } - - @Test - public void testLambdasCurlyBraces() { - testLambdaMessages("{message}"); - } - - @Test - public void testLambdasPercent() { - testLambdaMessages("message%s"); - } - - @Test - public void testLambdasPercentAndCurlyBraces() { - testLambdaMessages("message{%s}"); - } - - private void testLambdaMessages(final String string) { - final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); - root.info(() -> "Test info " + string); - root.config(() -> "Test info " + string); - root.fine(() -> "Test info " + string); - final List events = eventAppender.getEvents(); - assertThat(events).hasSize(3); - for (final LogEvent event : events) { - final String message = event.getMessage().getFormattedMessage(); - assertThat(message).isEqualTo("Test info " + string); - } - } -} diff --git a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/support/CustomLoggerAdapterTest.java b/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/support/CustomLoggerAdapterTest.java deleted file mode 100644 index 83d90f325dd..00000000000 --- a/jul-to-log4j/src/test/java/org/apache/logging/jul/tolog4j/test/support/CustomLoggerAdapterTest.java +++ /dev/null @@ -1,95 +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.logging.jul.tolog4j.test.support; - -import static org.junit.Assert.assertTrue; - -import java.util.logging.Filter; -import java.util.logging.Handler; -import java.util.logging.Level; -import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LogManager; -import org.apache.logging.jul.tolog4j.spi.AbstractLoggerAdapter; -import org.apache.logging.jul.tolog4j.support.AbstractLogger; -import org.apache.logging.jul.tolog4j.test.JulTestProperties; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.status.StatusLogger; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * Tests if the logger adapter can be customized. - */ -public class CustomLoggerAdapterTest { - - private static final org.apache.logging.log4j.Logger LOGGER = StatusLogger.getLogger(); - - @BeforeClass - public static void setUpClass() { - System.setProperty("java.util.logging.manager", LogManager.class.getName()); - System.setProperty(JulTestProperties.JUL_LOGGER_ADAPTER, CustomLoggerAdapter.class.getName()); - } - - @AfterClass - public static void tearDownClass() { - System.clearProperty("java.util.logging.manager"); - System.clearProperty(JulTestProperties.JUL_LOGGER_ADAPTER); - } - - @Test - public void testCustomLoggerAdapter() { - Logger logger = Logger.getLogger(CustomLoggerAdapterTest.class.getName()); - assertTrue("CustomLoggerAdapter is used", logger instanceof CustomLogger); - } - - public static class CustomLoggerAdapter extends AbstractLoggerAdapter { - - @Override - public Logger newLogger(String name, LoggerContext context) { - return new CustomLogger(context.getLogger(name)); - } - } - - private static class CustomLogger extends AbstractLogger { - - CustomLogger(ExtendedLogger logger) { - super(logger); - } - - @Override - public void setFilter(Filter newFilter) {} - - @Override - public void setLevel(final Level newLevel) throws SecurityException { - LOGGER.error("Cannot set JUL log level through Log4j API: ignoring call to Logger.setLevel({})", newLevel); - } - - @Override - public void addHandler(Handler handler) {} - - @Override - public void removeHandler(Handler handler) {} - - @Override - public void setUseParentHandlers(boolean useParentHandlers) {} - - @Override - public void setParent(Logger parent) {} - } -} diff --git a/jul-to-log4j/src/test/resources/CallerInformationTest.xml b/jul-to-log4j/src/test/resources/CallerInformationTest.xml deleted file mode 100644 index 087587ea5df..00000000000 --- a/jul-to-log4j/src/test/resources/CallerInformationTest.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/jul-to-log4j/src/test/resources/ResourceBundleTest.properties b/jul-to-log4j/src/test/resources/ResourceBundleTest.properties deleted file mode 100644 index 3563a119784..00000000000 --- a/jul-to-log4j/src/test/resources/ResourceBundleTest.properties +++ /dev/null @@ -1,21 +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. -# -## -# Resource bundle used in ResourceBundleTest - -msg_1 = Hello! -msg_2 = Hello %s and %s! diff --git a/jul-to-log4j/src/test/resources/ResourceBundleTest.xml b/jul-to-log4j/src/test/resources/ResourceBundleTest.xml deleted file mode 100644 index 3d2d891500e..00000000000 --- a/jul-to-log4j/src/test/resources/ResourceBundleTest.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - diff --git a/jul-to-log4j/src/test/resources/log4j2-test.xml b/jul-to-log4j/src/test/resources/log4j2-test.xml deleted file mode 100644 index 9c5bdb23ab9..00000000000 --- a/jul-to-log4j/src/test/resources/log4j2-test.xml +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-jndi-test/pom.xml b/log4j-jndi-test/pom.xml index dbe975ee014..84959b9e1f0 100644 --- a/log4j-jndi-test/pom.xml +++ b/log4j-jndi-test/pom.xml @@ -46,10 +46,6 @@ org.apache.logging.log4j log4j-jndi - - commons-logging - commons-logging - junit junit diff --git a/log4j-jpl/pom.xml b/log4j-jpl/pom.xml deleted file mode 100644 index 8f34451bee8..00000000000 --- a/log4j-jpl/pom.xml +++ /dev/null @@ -1,61 +0,0 @@ - - - - 4.0.0 - - org.apache.logging.log4j - log4j - ${revision} - ../log4j-parent - - - log4j-jpl - Apache Log4j JDK Platform Logging Adapter - The Apache Log4j implementation of java.lang.System.LoggerFinder - - - org.apache.logging.log4j.jpl - - - - - org.apache.logging.log4j - log4j-api - - - org.apache.logging.log4j - log4j-core - test - - - org.apache.logging.log4j - log4j-core-test - test - - - org.hamcrest - hamcrest - test - - - org.junit.jupiter - junit-jupiter-engine - test - - - diff --git a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLogger.java b/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLogger.java deleted file mode 100644 index cc384bd0dc6..00000000000 --- a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLogger.java +++ /dev/null @@ -1,145 +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.logging.log4j.jpl; - -import java.lang.System.Logger; -import java.util.MissingResourceException; -import java.util.Objects; -import java.util.ResourceBundle; -import java.util.function.Supplier; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.MessageFormatMessage; -import org.apache.logging.log4j.message.SimpleMessage; -import org.apache.logging.log4j.spi.ExtendedLogger; - -/** - * JPL {@link Logger logger} implementation that uses Log4j. - * Implement all default {@link Logger} methods to ensure proper class resolution - * - * @since 2.14 - */ -public class Log4jSystemLogger implements Logger { - - private final ExtendedLogger logger; - - private static final String FQCN = Log4jSystemLogger.class.getName(); - - public Log4jSystemLogger(final ExtendedLogger logger) { - this.logger = logger; - } - - @Override - public String getName() { - return logger.getName(); - } - - @Override - public boolean isLoggable(final Level level) { - return logger.isEnabled(getLevel(level)); - } - - @Override - public void log(final Level level, final String msg) { - log(level, (ResourceBundle) null, msg, (Throwable) null); - } - - @Override - public void log(final Level level, final Supplier msgSupplier) { - Objects.requireNonNull(msgSupplier); - if (isLoggable(Objects.requireNonNull(level))) { - log(level, (ResourceBundle) null, msgSupplier.get(), (Throwable) null); - } - } - - @Override - public void log(final Level level, final Object obj) { - Objects.requireNonNull(obj); - if (isLoggable(Objects.requireNonNull(level))) { - log(level, (ResourceBundle) null, obj.toString(), (Throwable) null); - } - } - - @Override - public void log(final Level level, final String msg, final Throwable thrown) { - log(level, null, msg, thrown); - } - - @Override - public void log(final Level level, final Supplier msgSupplier, final Throwable thrown) { - Objects.requireNonNull(msgSupplier); - if (isLoggable(Objects.requireNonNull(level))) { - log(level, null, msgSupplier.get(), thrown); - } - } - - @Override - public void log(final Level level, final String format, final Object... params) { - log(level, null, format, params); - } - - @Override - public void log(final Level level, final ResourceBundle bundle, final String msg, final Throwable thrown) { - logger.logIfEnabled(FQCN, getLevel(level), null, getResource(bundle, msg), thrown); - } - - @Override - public void log(final Level level, final ResourceBundle bundle, final String format, final Object... params) { - final Message message = createMessage(getResource(bundle, format), params); - logger.logIfEnabled(FQCN, getLevel(level), null, message, message.getThrowable()); - } - - private static Message createMessage(final String format, final Object... params) { - if (params == null || params.length == 0) { - return new SimpleMessage(format); - } - return new MessageFormatMessage(format, params); - } - - private static org.apache.logging.log4j.Level getLevel(final Level level) { - switch (level) { - case OFF: - return org.apache.logging.log4j.Level.OFF; - case ERROR: - return org.apache.logging.log4j.Level.ERROR; - case WARNING: - return org.apache.logging.log4j.Level.WARN; - case INFO: - return org.apache.logging.log4j.Level.INFO; - case DEBUG: - return org.apache.logging.log4j.Level.DEBUG; - case TRACE: - return org.apache.logging.log4j.Level.TRACE; - case ALL: - return org.apache.logging.log4j.Level.ALL; - } - return org.apache.logging.log4j.Level.ERROR; - } - - private static String getResource(final ResourceBundle bundle, final String msg) { - if (bundle == null || msg == null) { - return msg; - } - try { - return bundle.getString(msg); - } catch (MissingResourceException e) { - // ignore - return msg; - } catch (ClassCastException ex) { - return bundle.getObject(msg).toString(); - } - } -} diff --git a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerAdapter.java b/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerAdapter.java deleted file mode 100644 index ec418416ad4..00000000000 --- a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerAdapter.java +++ /dev/null @@ -1,45 +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.logging.log4j.jpl; - -import java.lang.System.Logger; -import java.lang.System.LoggerFinder; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.spi.AbstractLoggerAdapter; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.util.StackLocatorUtil; - -/** - * {@link Logger} registry implementation using just log4j-api. - * - * @since 2.14 - */ -public class Log4jSystemLoggerAdapter extends AbstractLoggerAdapter { - - @Override - protected Logger newLogger(final String name, final LoggerContext context) { - return new Log4jSystemLogger(context.getLogger(name)); - } - - @Override - protected LoggerContext getContext() { - return getContext( - LogManager.getFactory().isClassLoaderDependent() - ? StackLocatorUtil.getCallerClass(LoggerFinder.class) - : null); - } -} diff --git a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerFinder.java b/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerFinder.java deleted file mode 100644 index 4cc7ae11d6d..00000000000 --- a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerFinder.java +++ /dev/null @@ -1,35 +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.logging.log4j.jpl; - -import aQute.bnd.annotation.Resolution; -import aQute.bnd.annotation.spi.ServiceProvider; -import java.lang.System.Logger; - -/** - * @since 2.14 - */ -@ServiceProvider(value = System.LoggerFinder.class, resolution = Resolution.OPTIONAL) -public class Log4jSystemLoggerFinder extends System.LoggerFinder { - - private final Log4jSystemLoggerAdapter loggerAdapter = new Log4jSystemLoggerAdapter(); - - @Override - public Logger getLogger(final String name, final Module module) { - return loggerAdapter.getLogger(name); - } -} diff --git a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/package-info.java b/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/package-info.java deleted file mode 100644 index f2db7043462..00000000000 --- a/log4j-jpl/src/main/java/org/apache/logging/log4j/jpl/package-info.java +++ /dev/null @@ -1,22 +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. - */ -@Export -@Version("2.20.1") -package org.apache.logging.log4j.jpl; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/log4j-jpl/src/main/resources/META-INF/services/java.lang.System$LoggerFinder b/log4j-jpl/src/main/resources/META-INF/services/java.lang.System$LoggerFinder deleted file mode 100644 index b959e7e19a6..00000000000 --- a/log4j-jpl/src/main/resources/META-INF/services/java.lang.System$LoggerFinder +++ /dev/null @@ -1 +0,0 @@ -org.apache.logging.log4j.jpl.Log4jSystemLoggerFinder \ No newline at end of file diff --git a/log4j-jpl/src/test/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerTest.java b/log4j-jpl/src/test/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerTest.java deleted file mode 100644 index 4e137d02d6f..00000000000 --- a/log4j-jpl/src/test/java/org/apache/logging/log4j/jpl/Log4jSystemLoggerTest.java +++ /dev/null @@ -1,149 +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.logging.log4j.jpl; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; - -import java.lang.System.Logger; -import java.util.List; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.impl.MementoLogEvent; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class Log4jSystemLoggerTest { - - public static final String LOGGER_NAME = "Test"; - protected Logger logger; - protected ListAppender eventAppender; - protected ListAppender stringAppender; - - @BeforeEach - public void setUp() throws Exception { - logger = System.getLogger(LOGGER_NAME); - assertThat(logger, instanceOf(Log4jSystemLogger.class)); - eventAppender = ListAppender.getListAppender("TestAppender"); - stringAppender = ListAppender.getListAppender("StringAppender"); - assertNotNull(eventAppender); - assertNotNull(stringAppender); - } - - @AfterEach - public void tearDown() throws Exception { - if (eventAppender != null) { - eventAppender.clear(); - } - if (stringAppender != null) { - stringAppender.clear(); - } - } - - @Test - public void testGetName() throws Exception { - assertThat(logger.getName(), equalTo(LOGGER_NAME)); - } - - @Test - public void testIsLoggable() throws Exception { - assertThat(logger.isLoggable(Logger.Level.ERROR), equalTo(true)); - } - - @Test - public void testLog() throws Exception { - logger.log(Logger.Level.INFO, "Informative message here."); - final List events = eventAppender.getEvents(); - assertThat(events, hasSize(1)); - final LogEvent event = events.get(0); - assertThat(event, instanceOf(MementoLogEvent.class)); - assertEquals(Level.INFO, event.getLevel()); - assertEquals(LOGGER_NAME, event.getLoggerName()); - assertEquals("Informative message here.", event.getMessage().getFormattedMessage()); - assertEquals(Log4jSystemLogger.class.getName(), event.getLoggerFqcn()); - } - - @Test - public void testParameterizedLogging() { - logger.log(Logger.Level.INFO, "Hello, {0}!", "World"); - final List events = eventAppender.getEvents(); - assertThat(events, hasSize(1)); - final LogEvent event = events.get(0); - assertThat(event, instanceOf(MementoLogEvent.class)); - assertEquals(Level.INFO, event.getLevel()); - assertEquals(LOGGER_NAME, event.getLoggerName()); - assertEquals("Hello, World!", event.getMessage().getFormattedMessage()); - assertEquals(Log4jSystemLogger.class.getName(), event.getLoggerFqcn()); - } - - @Test - public void testParameterizedLoggingWithThrowable() { - final Throwable throwable = new RuntimeException(); - logger.log(Logger.Level.INFO, "Hello, {0}!", "World", throwable); - final List events = eventAppender.getEvents(); - assertThat(events, hasSize(1)); - final LogEvent event = events.get(0); - assertThat(event, instanceOf(MementoLogEvent.class)); - assertEquals(Level.INFO, event.getLevel()); - assertEquals(LOGGER_NAME, event.getLoggerName()); - assertEquals("Hello, World!", event.getMessage().getFormattedMessage()); - assertEquals(Log4jSystemLogger.class.getName(), event.getLoggerFqcn()); - assertSame(throwable, event.getThrown()); - } - - @Test - public void testLogWithCallingClass() throws Exception { - final Logger log = System.getLogger("Test.CallerClass"); - log.log(Logger.Level.INFO, "Calling from LoggerTest"); - final List messages = stringAppender.getMessages(); - assertThat(messages, hasSize(1)); - final String message = messages.get(0); - assertEquals(Log4jSystemLoggerTest.class.getName(), message); - } - - @Test - public void testCurlyBraces() { - testMessage("{message}"); - } - - @Test - public void testPercent() { - testMessage("message%s"); - } - - @Test - public void testPercentAndCurlyBraces() { - testMessage("message{%s}"); - } - - private void testMessage(final String string) { - logger.log(Logger.Level.INFO, "Test info " + string); - final List events = eventAppender.getEvents(); - assertThat(events, hasSize(1)); - for (final LogEvent event : events) { - final String message = event.getMessage().getFormattedMessage(); - assertThat(message, equalTo("Test info " + string)); - } - } -} diff --git a/log4j-jpl/src/test/resources/log4j2-test.xml b/log4j-jpl/src/test/resources/log4j2-test.xml deleted file mode 100644 index 79c22e7d827..00000000000 --- a/log4j-jpl/src/test/resources/log4j2-test.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-jul/pom.xml b/log4j-jul/pom.xml index fc598b85775..b8b1715e53b 100644 --- a/log4j-jul/pom.xml +++ b/log4j-jul/pom.xml @@ -35,11 +35,6 @@ log4j-api - - org.apache.logging.log4j - jul-to-log4j - - org.apache.logging.log4j log4j-core diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java index 7eca5cd12b7..97ee10e93e9 100644 --- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java +++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/Log4jBridgeHandler.java @@ -23,8 +23,8 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.util.ServiceLoader; import java.util.logging.LogRecord; -import org.apache.logging.jul.tolog4j.LevelTranslator; import org.apache.logging.log4j.jul.internal.JulLevelPropagator; +import org.apache.logging.log4j.jul.internal.LevelConverter; import org.apache.logging.log4j.jul.spi.LevelChangePropagator; import org.apache.logging.log4j.spi.ExtendedLogger; import org.apache.logging.log4j.status.StatusLogger; @@ -33,7 +33,7 @@ /** * Bridge from JUL to Log4j API *

- * This is an alternative to {@link org.apache.logging.jul.tolog4j.LogManager} (running as complete JUL replacement), + * This is an alternative to {@code org.apache.logging.jul.LogManager} (running as complete JUL replacement), * especially useful for webapps running on a container for which the LogManager cannot or should not be used. *

* @@ -176,7 +176,7 @@ public void publish(final LogRecord record) { final org.apache.logging.log4j.Logger log4jLogger = getLog4jLogger(record); final String msg = julFormatter.formatMessage(record); - final org.apache.logging.log4j.Level log4jLevel = LevelTranslator.toLevel(record.getLevel()); + final org.apache.logging.log4j.Level log4jLevel = LevelConverter.toLog4jLevel(record.getLevel()); final Throwable thrown = record.getThrown(); if (log4jLogger instanceof ExtendedLogger) { // relevant for location information diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/internal/JulLevelPropagator.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/internal/JulLevelPropagator.java index 53a933dfed5..adb2e34460e 100644 --- a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/internal/JulLevelPropagator.java +++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/internal/JulLevelPropagator.java @@ -22,7 +22,6 @@ import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; -import org.apache.logging.jul.tolog4j.LevelTranslator; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; @@ -82,7 +81,7 @@ private void propagateLogLevels(final Configuration configuration) { final java.util.logging.Logger julLog = java.util.logging.Logger.getLogger(loggerConfig.getName()); // this also fits for root = "" final java.util.logging.Level julLevel = - LevelTranslator.toJavaLevel(loggerConfig.getLevel()); // loggerConfig.getLevel() never returns null + LevelConverter.toJulLevel(loggerConfig.getLevel()); // loggerConfig.getLevel() never returns null julLog.setLevel(julLevel); julLoggerRefs.add(julLog); } diff --git a/log4j-jul/src/main/java/org/apache/logging/log4j/jul/internal/LevelConverter.java b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/internal/LevelConverter.java new file mode 100644 index 00000000000..bbd4b0a008c --- /dev/null +++ b/log4j-jul/src/main/java/org/apache/logging/log4j/jul/internal/LevelConverter.java @@ -0,0 +1,165 @@ +/* + * 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.logging.log4j.jul.internal; + +import static org.apache.logging.log4j.Level.ALL; +import static org.apache.logging.log4j.Level.DEBUG; +import static org.apache.logging.log4j.Level.ERROR; +import static org.apache.logging.log4j.Level.FATAL; +import static org.apache.logging.log4j.Level.INFO; +import static org.apache.logging.log4j.Level.OFF; +import static org.apache.logging.log4j.Level.TRACE; +import static org.apache.logging.log4j.Level.WARN; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.logging.log4j.Level; + +/** + * Default implementation of LevelConverter strategy. + *

+ * Since 2.4, supports custom JUL levels by mapping them to their closest mapped neighbour. + *

+ * + * @since 2.1 + */ +public final class LevelConverter { + + /** + * Custom Log4j level corresponding to the {@link java.util.logging.Level#FINEST} logging level. + */ + static final Level FINEST = Level.forName("FINEST", TRACE.intLevel() + 100); + + /** + * Custom Log4j level corresponding to the {@link java.util.logging.Level#CONFIG} logging level. + */ + static final Level CONFIG = Level.forName("CONFIG", INFO.intLevel() + 50); + + /** + * Custom JUL level corresponding to the {@link Level#FATAL} logging loevel + */ + private static final java.util.logging.Level JUL_FATAL = + new CustomJulLevel("FATAL", java.util.logging.Level.SEVERE.intValue() + 100); + + private static final Map julToLog4j = new ConcurrentHashMap<>(); + private static final Map log4jToJul = new ConcurrentHashMap<>(); + + static { + // Predefined levels sorted from most severe to least severe. + Map log4jtoJul = Map.of( + OFF, + java.util.logging.Level.OFF, + FATAL, + JUL_FATAL, + ERROR, + java.util.logging.Level.SEVERE, + WARN, + java.util.logging.Level.WARNING, + INFO, + java.util.logging.Level.INFO, + CONFIG, + java.util.logging.Level.CONFIG, + DEBUG, + java.util.logging.Level.FINE, + TRACE, + java.util.logging.Level.FINER, + FINEST, + java.util.logging.Level.FINEST, + ALL, + java.util.logging.Level.ALL); + // Map Log4j to JUL + LevelConverter.log4jToJul.putAll(log4jtoJul); + // Map JUL to Log4j + // SEVERE will be mapped to ERROR. + for (Map.Entry entry : log4jtoJul.entrySet()) { + LevelConverter.julToLog4j.put(entry.getValue(), entry.getKey()); + } + } + + private LevelConverter() {} + + static int log4jToJulIntLevel(int log4jLevel) { + if (log4jLevel <= OFF.intLevel()) { + return java.util.logging.Level.OFF.intValue(); + } + // From OFF to INFO: normal pace + if (log4jLevel <= INFO.intLevel()) { + return java.util.logging.Level.INFO.intValue() + (INFO.intLevel() - log4jLevel); + } + // From INFO to CONFIG: double pace + if (log4jLevel <= LevelConverter.CONFIG.intLevel()) { + return java.util.logging.Level.CONFIG.intValue() + 2 * (LevelConverter.CONFIG.intLevel() - log4jLevel); + } + // From CONFIG to DEBUG: quadruple pace + if (log4jLevel <= DEBUG.intLevel()) { + return java.util.logging.Level.FINE.intValue() + 4 * (DEBUG.intLevel() - log4jLevel); + } + // Above DEBUG we have: + // * Integer.MAX_VALUE - 500 Log4j levels + // * Integer.MAX_VALUE + 401 JUL levels + // So every Log4j level will have a JUL level + if (log4jLevel != ALL.intLevel()) { + return java.util.logging.Level.FINE.intValue() + (DEBUG.intLevel() - log4jLevel); + } + return ALL.intLevel(); + } + + static int julToLog4jIntLevel(int julLevel) { + // No Log4j level is mapped below this value + if (julLevel <= java.util.logging.Level.FINE.intValue() + (DEBUG.intLevel() - ALL.intLevel())) { + return ALL.intLevel(); + } + // From ALL to FINE: normal pace + if (julLevel <= java.util.logging.Level.FINE.intValue()) { + return DEBUG.intLevel() + (java.util.logging.Level.FINE.intValue() - julLevel); + } + // From FINE to CONFIG: 1/4 of the pace + if (julLevel <= java.util.logging.Level.CONFIG.intValue()) { + return CONFIG.intLevel() + (java.util.logging.Level.CONFIG.intValue() - julLevel) / 4; + } + // From CONFIG to INFO: 1/2 of the pace + if (julLevel <= java.util.logging.Level.INFO.intValue()) { + return INFO.intLevel() + (java.util.logging.Level.INFO.intValue() - julLevel) / 2; + } + // From INFO to OFF: normal pace + if (julLevel <= java.util.logging.Level.INFO.intValue() + (INFO.intLevel() - 1)) { + return INFO.intLevel() + (java.util.logging.Level.INFO.intValue() - julLevel); + } + return OFF.intLevel(); + } + + public static java.util.logging.Level toJulLevel(final Level log4jLevel) { + return log4jLevel != null + ? log4jToJul.computeIfAbsent( + log4jLevel, l -> new CustomJulLevel(l.name(), log4jToJulIntLevel(l.intLevel()))) + : null; + } + + public static Level toLog4jLevel(final java.util.logging.Level julLevel) { + return julLevel != null + ? julToLog4j.computeIfAbsent( + julLevel, l -> Level.forName(l.getName(), julToLog4jIntLevel(l.intValue()))) + : null; + } + + private static class CustomJulLevel extends java.util.logging.Level { + + private CustomJulLevel(String name, int value) { + super(name, value); + } + } +} diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/internal/JulLevelPropagatorTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/internal/JulLevelPropagatorTest.java index 960e9c5ea57..c25d449309d 100644 --- a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/internal/JulLevelPropagatorTest.java +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/internal/JulLevelPropagatorTest.java @@ -20,7 +20,6 @@ import java.util.logging.Level; import java.util.logging.Logger; -import org.apache.logging.jul.tolog4j.LevelTranslator; import org.apache.logging.log4j.core.config.Configurator; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -56,7 +55,7 @@ void synchronization_retained_after_GC() { @Test void when_set_level_synchronization_works() { - Configurator.setLevel("", LevelTranslator.CONFIG); + Configurator.setLevel("", LevelConverter.CONFIG); Configurator.setLevel("foo", org.apache.logging.log4j.Level.DEBUG); Configurator.setLevel("foo.bar", org.apache.logging.log4j.Level.TRACE); diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jul/internal/LevelConverterTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/internal/LevelConverterTest.java new file mode 100644 index 00000000000..7e97bf17775 --- /dev/null +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jul/internal/LevelConverterTest.java @@ -0,0 +1,65 @@ +/* + * 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.logging.log4j.jul.internal; + +import static org.apache.logging.log4j.jul.internal.LevelConverter.julToLog4jIntLevel; +import static org.apache.logging.log4j.jul.internal.LevelConverter.log4jToJulIntLevel; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.apache.logging.log4j.Level; +import org.junit.jupiter.api.Test; + +class LevelConverterTest { + + @Test + void intConversionsAreInverseFunctions() { + for (int log4jLevel = Level.OFF.intLevel() + 1, expectedJulLevel = 1200; + log4jLevel < Level.TRACE.intLevel() + 200; + log4jLevel++) { + // We decrement `expectedJulLevel` generally by 1, + // but between INFO and DEBUG there are 100 Log4j levels, but 300 JUL levels, + // so sometimes we decrement more. + if (log4jLevel <= Level.INFO.intLevel()) { + expectedJulLevel--; + } else if (log4jLevel <= LevelConverter.CONFIG.intLevel()) { + expectedJulLevel -= 2; + } else if (log4jLevel <= Level.DEBUG.intLevel()) { + expectedJulLevel -= 4; + } else { + expectedJulLevel--; + } + + int julLevel = log4jToJulIntLevel(log4jLevel); + + assertThat(julLevel) + .as("JUL Level corresponding to Log4j Level %d", log4jLevel) + .isEqualTo(expectedJulLevel); + assertThat(julToLog4jIntLevel(log4jToJulIntLevel(log4jLevel))) + .as("Log4j level %d", log4jLevel) + .isEqualTo(log4jLevel); + } + } + + /** + * (LOG4J2-1108) NullPointerException when passing null to java.util.logging.Logger.setLevel(). + */ + @Test + public void testJulSetNull() { + assertNull(LevelConverter.toLog4jLevel(null)); + } +} diff --git a/log4j-mongodb/pom.xml b/log4j-mongodb/pom.xml index e02b8ce6193..918dfb0ceb4 100644 --- a/log4j-mongodb/pom.xml +++ b/log4j-mongodb/pom.xml @@ -62,6 +62,12 @@ ${mongodb.version}
+ + org.slf4j + slf4j-nop + ${slf4j2.version} + + @@ -148,16 +154,19 @@ true + org.junit.jupiter junit-jupiter-engine ${junit-jupiter.version} + org.slf4j - slf4j-nop + slf4j-simple ${slf4j2.version} + diff --git a/log4j-osgi-test/pom.xml b/log4j-osgi-test/pom.xml index ec386848a68..dded11ea819 100644 --- a/log4j-osgi-test/pom.xml +++ b/log4j-osgi-test/pom.xml @@ -40,100 +40,138 @@ true true - 1.3.7 org.apache.logging.log4j.osgi_test + + + 1.3.7 + 1.4.14 + + + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.apache.aries.spifly + org.apache.aries.spifly.dynamic.bundle + ${spifly.version} + + + + + + javax.inject javax.inject 1 provided + org.apache.logging.log4j log4j-api test + org.apache.logging.log4j log4j-api-test test + org.apache.logging.log4j log4j-core test + org.apache.logging.log4j log4j-plugins test + org.apache.logging.log4j log4j-to-jul test + org.apache.logging.log4j log4j-to-slf4j test + org.junit.jupiter junit-jupiter-engine test + org.junit.vintage junit-vintage-engine test + ch.qos.logback logback-classic test + org.apache.aries.spifly org.apache.aries.spifly.dynamic.bundle - ${spifly.version} test + org.apache.felix org.apache.felix.framework test + org.eclipse.platform org.eclipse.osgi test + org.ops4j.pax.exam pax-exam test + org.ops4j.pax.exam pax-exam-container-native test + org.ops4j.pax.exam pax-exam-junit4 test + org.ops4j.pax.exam pax-exam-link-assembly test + org.ops4j.pax.exam pax-exam-spi diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml index 25624ac1102..eb98b5ad6d4 100644 --- a/log4j-parent/pom.xml +++ b/log4j-parent/pom.xml @@ -101,7 +101,6 @@ 2.12.0 2.17.0 3.17.0 - 1.3.4 4.0.0 0.9.0 7.0.5 @@ -122,10 +121,8 @@ 4.13.2 5.11.3 2.3.0 - 1.2.17 1.1.0 0.18 - 1.5.12 3.9.9 5.14.2 15.4 @@ -276,12 +273,6 @@ ${commons-lang3.version} - - commons-logging - commons-logging - ${commons-logging.version} - - org.apache.commons commons-pool2 @@ -413,12 +404,6 @@ ${junit-pioneer.version} - - log4j - log4j - ${log4j.version} - - com.github.ivandzf @@ -439,32 +424,6 @@ ${log4j2-logstash-layout.version} - - ch.qos.logback - logback-classic - ${logback.version} - - - - ch.qos.logback - logback-classic - ${logback.version} - test-jar - - - - ch.qos.logback - logback-core - ${logback.version} - - - - ch.qos.logback - logback-core - ${logback.version} - test-jar - - org.apache.maven maven-core diff --git a/log4j-perf-test/pom.xml b/log4j-perf-test/pom.xml index d6cd2076e14..1a98f0d17cc 100644 --- a/log4j-perf-test/pom.xml +++ b/log4j-perf-test/pom.xml @@ -40,6 +40,8 @@ 1.2.25 + 1.6.0 + 1.4.14 2.0.16 @@ -47,9 +49,21 @@ - org.slf4j - slf4j-api - ${slf4j2.version} + co.elastic.logging + log4j2-ecs-layout + ${log4j2-ecs-layout.version} + + + + ch.qos.logback + logback-classic + ${logback.version} + + + + ch.qos.logback + logback-core + ${logback.version} @@ -58,23 +72,33 @@ ${reload4j.version} + + org.slf4j + slf4j-api + ${slf4j2.version} + + + org.openjdk.jmh jmh-generator-annprocess provided + org.apache.logging.log4j log4j-api + org.apache.logging.log4j log4j-async-logger + org.apache.logging.log4j log4j-conversant @@ -83,63 +107,75 @@ org.apache.logging.log4j log4j-core + org.apache.logging.log4j log4j-core-test + org.apache.logging.log4j log4j-jdbc + org.apache.logging.log4j log4j-layout-template-json + org.apache.logging.log4j log4j-layout-template-json-test + org.apache.logging.log4j log4j-plugins + com.h2database h2 + org.hsqldb hsqldb + com.fasterxml.jackson.core jackson-core true + com.fasterxml.jackson.core jackson-databind true + org.jctools jctools-core + org.openjdk.jmh jmh-core + ch.qos.logback logback-classic - compile + ch.qos.logback logback-core - compile + ch.qos.reload4j reload4j @@ -148,6 +184,7 @@ org.slf4j slf4j-api + @@ -188,6 +225,7 @@ + org.apache.maven.plugins maven-shade-plugin @@ -204,6 +242,10 @@ ${uberjar.name} + + + META-INF/sisu/javax.inject.Named + org.openjdk.jmh.Main @@ -214,15 +256,28 @@ - *:* + + module-info.class + META-INF/versions/9/module-info.class + META-INF/MANIFEST.MF + META-INF/*.SF META-INF/*.DSA META-INF/*.RSA + + META-INF/log4j/propertyMapping.json + + about.html + overview.html + META-INF/LICENSE-notice.md + LICENSE + + META-INF/DEPENDENCIES diff --git a/log4j-slf4j-impl/pom.xml b/log4j-slf4j-impl/pom.xml deleted file mode 100644 index 81f71fc9c5b..00000000000 --- a/log4j-slf4j-impl/pom.xml +++ /dev/null @@ -1,163 +0,0 @@ - - - - - 4.0.0 - - - org.apache.logging.log4j - log4j - ${revision} - ../log4j-parent - - - log4j-slf4j-impl - - SLF4J 1 Binding for Log4j API - - SLF4J 1 binding (provider) for the Log4j API. - It forwards SLF4J 1 calls to the Log4j API. - (Refer to the `log4j-to-slf4j` artifact for forwarding the Log4j API to SLF4J.) - - - 1.7.36 - false - - - - - - - org.slf4j - slf4j-api - ${slf4j.version} - - - - - - - org.osgi - org.osgi.framework - provided - - - org.apache.logging.log4j - log4j-api - - - org.slf4j - slf4j-api - - - org.apache.logging.log4j - log4j-core - runtime - - - org.apache.logging.log4j - log4j-api-test - test - - - org.slf4j - slf4j-api - - - - - org.apache.logging.log4j - log4j-core-test - test - - - org.apache.logging.log4j - log4j-to-slf4j - test - - - org.slf4j - slf4j-api - - - - - org.apache.commons - commons-csv - test - - - org.apache.commons - commons-lang3 - test - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.vintage - junit-vintage-engine - test - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - loop-test - - test - - test - - - **/OverflowTest.java - - - - - default-test - - test - - test - - - **/*Test.java - - - **/OverflowTest.java - - - org.apache.logging.log4j:log4j-to-slf4j - - - - - - - - - diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java deleted file mode 100644 index 41848745051..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java +++ /dev/null @@ -1,390 +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.logging.slf4j; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.ParameterizedMessage; -import org.apache.logging.log4j.message.SimpleMessage; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.slf4j.Marker; -import org.slf4j.spi.LocationAwareLogger; - -/** - * SLF4J logger implementation that uses Log4j. - */ -public class Log4jLogger implements LocationAwareLogger { - - public static final String FQCN = Log4jLogger.class.getName(); - - private final ExtendedLogger logger; - private final String name; - private final Log4jMarkerFactory markerFactory; - - public Log4jLogger(final Log4jMarkerFactory markerFactory, final ExtendedLogger logger, final String name) { - this.markerFactory = markerFactory; - this.logger = logger; - this.name = name; - } - - @Override - public void trace(final String format) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format); - } - - @Override - public void trace(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, o); - } - - @Override - public void trace(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, arg1, arg2); - } - - @Override - public void trace(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, args); - } - - @Override - public void trace(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, t); - } - - @Override - public boolean isTraceEnabled() { - return logger.isEnabled(Level.TRACE, null, null); - } - - @Override - public boolean isTraceEnabled(final Marker marker) { - return logger.isEnabled(Level.TRACE, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void trace(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void trace(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void trace(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void trace(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void trace(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void debug(final String format) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format); - } - - @Override - public void debug(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, o); - } - - @Override - public void debug(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, arg1, arg2); - } - - @Override - public void debug(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, args); - } - - @Override - public void debug(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, t); - } - - @Override - public boolean isDebugEnabled() { - return logger.isEnabled(Level.DEBUG, null, null); - } - - @Override - public boolean isDebugEnabled(final Marker marker) { - return logger.isEnabled(Level.DEBUG, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void debug(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void debug(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void debug(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void debug(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void debug(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void info(final String format) { - logger.logIfEnabled(FQCN, Level.INFO, null, format); - } - - @Override - public void info(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, o); - } - - @Override - public void info(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, arg1, arg2); - } - - @Override - public void info(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, args); - } - - @Override - public void info(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, t); - } - - @Override - public boolean isInfoEnabled() { - return logger.isEnabled(Level.INFO, null, null); - } - - @Override - public boolean isInfoEnabled(final Marker marker) { - return logger.isEnabled(Level.INFO, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void info(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void info(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void info(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void info(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void info(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void warn(final String format) { - logger.logIfEnabled(FQCN, Level.WARN, null, format); - } - - @Override - public void warn(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, o); - } - - @Override - public void warn(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, arg1, arg2); - } - - @Override - public void warn(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, args); - } - - @Override - public void warn(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, t); - } - - @Override - public boolean isWarnEnabled() { - return logger.isEnabled(Level.WARN, null, null); - } - - @Override - public boolean isWarnEnabled(final Marker marker) { - return logger.isEnabled(Level.WARN, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void warn(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void warn(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void warn(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void warn(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void warn(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void error(final String format) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format); - } - - @Override - public void error(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, o); - } - - @Override - public void error(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, arg1, arg2); - } - - @Override - public void error(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, args); - } - - @Override - public void error(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, t); - } - - @Override - public boolean isErrorEnabled() { - return logger.isEnabled(Level.ERROR, null, null); - } - - @Override - public boolean isErrorEnabled(final Marker marker) { - return logger.isEnabled(Level.ERROR, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void error(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void error(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void error(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void error(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void error(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void log( - final Marker marker, - final String fqcn, - final int level, - final String message, - final Object[] params, - final Throwable throwable) { - final Level log4jLevel = getLevel(level); - final org.apache.logging.log4j.Marker log4jMarker = markerFactory.getLog4jMarker(marker); - - if (!logger.isEnabled(log4jLevel, log4jMarker, message, params)) { - return; - } - final Message msg; - final Throwable actualThrowable; - if (params == null) { - msg = new SimpleMessage(message); - actualThrowable = throwable; - } else { - msg = new ParameterizedMessage(message, params, throwable); - actualThrowable = throwable != null ? throwable : msg.getThrowable(); - } - logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, actualThrowable); - } - - @Override - public String getName() { - return name; - } - - private static Level getLevel(final int i) { - switch (i) { - case TRACE_INT: - return Level.TRACE; - case DEBUG_INT: - return Level.DEBUG; - case INFO_INT: - return Level.INFO; - case WARN_INT: - return Level.WARN; - case ERROR_INT: - return Level.ERROR; - } - return Level.ERROR; - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLoggerFactory.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLoggerFactory.java deleted file mode 100644 index 0f3eded7a82..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jLoggerFactory.java +++ /dev/null @@ -1,71 +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.logging.slf4j; - -import java.util.function.Predicate; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.LoggingException; -import org.apache.logging.log4j.spi.AbstractLoggerAdapter; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.StackLocatorUtil; -import org.slf4j.ILoggerFactory; -import org.slf4j.Logger; - -/** - * Log4j implementation of SLF4J ILoggerFactory interface. - */ -public class Log4jLoggerFactory extends AbstractLoggerAdapter implements ILoggerFactory { - - private static final StatusLogger LOGGER = StatusLogger.getLogger(); - private static final String SLF4J_PACKAGE = "org.slf4j"; - private static final Predicate> CALLER_PREDICATE = clazz -> - !AbstractLoggerAdapter.class.equals(clazz) && !clazz.getName().startsWith(SLF4J_PACKAGE); - private static final String TO_SLF4J_CONTEXT = "org.apache.logging.slf4j.SLF4JLoggerContext"; - - private final Log4jMarkerFactory markerFactory; - - public Log4jLoggerFactory(final Log4jMarkerFactory markerFactory) { - this.markerFactory = markerFactory; - } - - @Override - protected Logger newLogger(final String name, final LoggerContext context) { - final String key = Logger.ROOT_LOGGER_NAME.equals(name) ? LogManager.ROOT_LOGGER_NAME : name; - return new Log4jLogger(markerFactory, validateContext(context).getLogger(key), name); - } - - @Override - protected LoggerContext getContext() { - final Class anchor = LogManager.getFactory().isClassLoaderDependent() - ? StackLocatorUtil.getCallerClass(Log4jLoggerFactory.class, CALLER_PREDICATE) - : null; - LOGGER.trace("Log4jLoggerFactory.getContext() found anchor {}", anchor); - return anchor == null ? LogManager.getContext(false) : getContext(anchor); - } - - Log4jMarkerFactory getMarkerFactory() { - return markerFactory; - } - - private LoggerContext validateContext(final LoggerContext context) { - if (TO_SLF4J_CONTEXT.equals(context.getClass().getName())) { - throw new LoggingException("log4j-slf4j-impl cannot be present with log4j-to-slf4j"); - } - return context; - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMDCAdapter.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMDCAdapter.java deleted file mode 100644 index a32c9c77796..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMDCAdapter.java +++ /dev/null @@ -1,58 +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.logging.slf4j; - -import java.util.Map; -import org.apache.logging.log4j.ThreadContext; -import org.slf4j.spi.MDCAdapter; - -/** - * - */ -public class Log4jMDCAdapter implements MDCAdapter { - - @Override - public void put(final String key, final String val) { - ThreadContext.put(key, val); - } - - @Override - public String get(final String key) { - return ThreadContext.get(key); - } - - @Override - public void remove(final String key) { - ThreadContext.remove(key); - } - - @Override - public void clear() { - ThreadContext.clearMap(); - } - - @Override - public Map getCopyOfContextMap() { - return ThreadContext.getContext(); - } - - @Override - public void setContextMap(final Map map) { - ThreadContext.clearMap(); - ThreadContext.putAll(map); - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java deleted file mode 100644 index e2fbe624911..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java +++ /dev/null @@ -1,126 +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.logging.slf4j; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; -import org.apache.logging.log4j.MarkerManager; -import org.slf4j.IMarkerFactory; -import org.slf4j.Marker; - -/** - * Log4j/SLF4J {@link Marker} type bridge. - */ -class Log4jMarker implements Marker { - - public static final long serialVersionUID = 1590472L; - - private final IMarkerFactory factory; - - private final org.apache.logging.log4j.Marker marker; - - /** - * Constructs a Log4jMarker using an existing Log4j {@link org.apache.logging.log4j.Marker}. - * @param marker The Log4j Marker upon which to base this Marker. - */ - public Log4jMarker(final IMarkerFactory markerFactory, final org.apache.logging.log4j.Marker marker) { - this.factory = markerFactory; - this.marker = marker; - } - - @Override - public void add(final Marker marker) { - if (marker == null) { - throw new IllegalArgumentException(); - } - final Marker m = factory.getMarker(marker.getName()); - this.marker.addParents(((Log4jMarker) m).getLog4jMarker()); - } - - @Override - public boolean contains(final Marker marker) { - if (marker == null) { - throw new IllegalArgumentException(); - } - return this.marker.isInstanceOf(marker.getName()); - } - - @Override - public boolean contains(final String s) { - return s != null ? this.marker.isInstanceOf(s) : false; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof Log4jMarker)) { - return false; - } - final Log4jMarker other = (Log4jMarker) obj; - return Objects.equals(marker, other.marker); - } - - public org.apache.logging.log4j.Marker getLog4jMarker() { - return marker; - } - - @Override - public String getName() { - return marker.getName(); - } - - @Override - public boolean hasChildren() { - return marker.hasParents(); - } - - @Override - public int hashCode() { - return 31 + Objects.hashCode(marker); - } - - @Override - public boolean hasReferences() { - return marker.hasParents(); - } - - @Override - public Iterator iterator() { - final org.apache.logging.log4j.Marker[] log4jParents = this.marker.getParents(); - if (log4jParents == null) { - return Collections.emptyIterator(); - } - final List parents = new ArrayList<>(log4jParents.length); - for (final org.apache.logging.log4j.Marker m : log4jParents) { - parents.add(factory.getMarker(m.getName())); - } - return parents.iterator(); - } - - @Override - public boolean remove(final Marker marker) { - return marker != null ? this.marker.remove(MarkerManager.getMarker(marker.getName())) : false; - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java deleted file mode 100644 index cedfc06571e..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java +++ /dev/null @@ -1,151 +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.logging.slf4j; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.MarkerManager; -import org.apache.logging.log4j.status.StatusLogger; -import org.slf4j.IMarkerFactory; -import org.slf4j.Marker; - -/** - * Log4j/SLF4J bridge to create SLF4J Markers based on name or based on existing SLF4J Markers. - */ -public class Log4jMarkerFactory implements IMarkerFactory { - - private static final Logger LOGGER = StatusLogger.getLogger(); - - private final ConcurrentMap markerMap = new ConcurrentHashMap<>(); - - /** - * Returns a Log4j Marker that is compatible with SLF4J. - * @param name The name of the Marker. - * @return A Marker. - */ - @Override - public Marker getMarker(final String name) { - if (name == null) { - throw new IllegalArgumentException("Marker name must not be null"); - } - final Marker marker = markerMap.get(name); - if (marker != null) { - return marker; - } - final org.apache.logging.log4j.Marker log4jMarker = MarkerManager.getMarker(name); - return addMarkerIfAbsent(name, log4jMarker); - } - - private Marker addMarkerIfAbsent(final String name, final org.apache.logging.log4j.Marker log4jMarker) { - final Marker marker = new Log4jMarker(this, log4jMarker); - final Marker existing = markerMap.putIfAbsent(name, marker); - return existing == null ? marker : existing; - } - - /** - * Returns a Log4j Marker converted from an existing custom SLF4J Marker. - * @param marker The SLF4J Marker to convert. - * @return A converted Log4j/SLF4J Marker. - * @since 2.1 - */ - public Marker getMarker(final Marker marker) { - if (marker == null) { - throw new IllegalArgumentException("Marker must not be null"); - } - final Marker m = markerMap.get(marker.getName()); - if (m != null) { - return m; - } - return addMarkerIfAbsent(marker.getName(), convertMarker(marker)); - } - - /** - * Gets the Log4j2 marker associated to this SLF4J marker or creates a new one. - * - * @param marker a SLF4J marker - * @return a Log4j2 marker - */ - org.apache.logging.log4j.Marker getLog4jMarker(final Marker marker) { - if (marker == null) { - return null; - } else if (marker instanceof Log4jMarker) { - return ((Log4jMarker) marker).getLog4jMarker(); - } else { - return ((Log4jMarker) getMarker(marker)).getLog4jMarker(); - } - } - - static org.apache.logging.log4j.Marker convertMarker(final Marker original) { - if (original == null) { - throw new IllegalArgumentException("Marker must not be null"); - } - return convertMarker(original, new ArrayList()); - } - - private static org.apache.logging.log4j.Marker convertMarker( - final Marker original, final Collection visited) { - final org.apache.logging.log4j.Marker marker = MarkerManager.getMarker(original.getName()); - if (original.hasReferences()) { - final Iterator it = original.iterator(); - while (it.hasNext()) { - final Marker next = it.next(); - if (visited.contains(next)) { - LOGGER.warn("Found a cycle in Marker [{}]. Cycle will be broken.", next.getName()); - } else { - visited.add(next); - marker.addParents(convertMarker(next, visited)); - } - } - } - return marker; - } - - /** - * Returns true if the Marker exists. - * @param name The Marker name. - * @return {@code true} if the Marker exists, {@code false} otherwise. - */ - @Override - public boolean exists(final String name) { - return markerMap.containsKey(name); - } - - /** - * Log4j does not support detached Markers. This method always returns false. - * @param name The Marker name. - * @return {@code false} - */ - @Override - public boolean detachMarker(final String name) { - return false; - } - - /** - * Log4j does not support detached Markers for performance reasons. The returned Marker is attached. - * @param name The Marker name. - * @return The named Marker (unmodified). - */ - @Override - public Marker getDetachedMarker(final String name) { - LOGGER.warn("Log4j does not support detached Markers. Returned Marker [{}] will be unchanged.", name); - return getMarker(name); - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/SLF4JLoggingException.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/SLF4JLoggingException.java deleted file mode 100644 index 57a65e1dfb5..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/SLF4JLoggingException.java +++ /dev/null @@ -1,41 +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.logging.slf4j; - -/** - * Exception thrown when the SLF4J adapter encounters a problem. - * - */ -public class SLF4JLoggingException extends RuntimeException { - - /** - * Generated serial version ID. - */ - private static final long serialVersionUID = -1618650972455089998L; - - public SLF4JLoggingException(final String msg) { - super(msg); - } - - public SLF4JLoggingException(final String msg, final Exception ex) { - super(msg, ex); - } - - public SLF4JLoggingException(final Exception ex) { - super(ex); - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/package-info.java b/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/package-info.java deleted file mode 100644 index d847bc764bb..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/apache/logging/slf4j/package-info.java +++ /dev/null @@ -1,30 +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. - */ -/** - * SLF4J support. Note that this does indeed share the same package namespace as the one found in log4j-to-slf4j; - * this is intentional. The two JARs should not be used at the same time! Thus, in an OSGi environment - * where split packages are not allowed, this error is prevented due to both JARs sharing an exported package name. - */ -@Export -@Header(name = Constants.BUNDLE_ACTIVATIONPOLICY, value = Constants.ACTIVATION_LAZY) -@Version("3.0.0") -package org.apache.logging.slf4j; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.bundle.Header; -import org.osgi.annotation.versioning.Version; -import org.osgi.framework.Constants; diff --git a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticLoggerBinder.java b/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticLoggerBinder.java deleted file mode 100644 index c60fac542d2..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticLoggerBinder.java +++ /dev/null @@ -1,84 +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.slf4j.impl; - -import org.apache.logging.slf4j.Log4jLoggerFactory; -import org.apache.logging.slf4j.Log4jMarkerFactory; -import org.slf4j.ILoggerFactory; -import org.slf4j.spi.LoggerFactoryBinder; - -/** - * SLF4J LoggerFactoryBinder implementation using Log4j. This class is part of the required classes used to specify an - * SLF4J logger provider implementation. - */ -public final class StaticLoggerBinder implements LoggerFactoryBinder { - - /** - * Declare the version of the SLF4J API this implementation is compiled - * against. The value of this field is usually modified with each release. - */ - // to avoid constant folding by the compiler, this field must *not* be final - public static String REQUESTED_API_VERSION = "1.6"; // !final - - private static final String LOGGER_FACTORY_CLASS_STR = Log4jLoggerFactory.class.getName(); - - /** - * The unique instance of this class. - */ - private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder(); - - /** - * The ILoggerFactory instance returned by the {@link #getLoggerFactory} - * method should always be the same object - */ - private final ILoggerFactory loggerFactory; - - /** - * Private constructor to prevent instantiation - */ - private StaticLoggerBinder() { - loggerFactory = new Log4jLoggerFactory( - (Log4jMarkerFactory) StaticMarkerBinder.getSingleton().getMarkerFactory()); - } - - /** - * Returns the singleton of this class. - * - * @return the StaticLoggerBinder singleton - */ - public static StaticLoggerBinder getSingleton() { - return SINGLETON; - } - - /** - * Returns the factory. - * @return the factor. - */ - @Override - public ILoggerFactory getLoggerFactory() { - return loggerFactory; - } - - /** - * Returns the class name. - * @return the class name; - */ - @Override - public String getLoggerFactoryClassStr() { - return LOGGER_FACTORY_CLASS_STR; - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticMDCBinder.java b/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticMDCBinder.java deleted file mode 100644 index abe3fbb6b60..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticMDCBinder.java +++ /dev/null @@ -1,60 +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.slf4j.impl; - -import org.apache.logging.slf4j.Log4jMDCAdapter; -import org.slf4j.spi.MDCAdapter; - -/** - * - */ -public final class StaticMDCBinder { - - /** - * The unique instance of this class. - */ - public static final StaticMDCBinder SINGLETON = new StaticMDCBinder(); - - private final MDCAdapter mdcAdapter = new Log4jMDCAdapter(); - - private StaticMDCBinder() {} - - /** - * Returns the {@link #SINGLETON} {@link StaticMDCBinder}. - * Added to slf4j-api 1.7.14 via https://github.com/qos-ch/slf4j/commit/ea3cca72cd5a9329a06b788317a17e806ee8acd0 - * @return the singleton instance - */ - public static StaticMDCBinder getSingleton() { - return SINGLETON; - } - - /** - * Currently this method always returns an instance of {@link StaticMDCBinder}. - * @return an MDC adapter - */ - public MDCAdapter getMDCA() { - return mdcAdapter; - } - - /** - * Retrieve the adapter class name. - * @return The adapter class name. - */ - public String getMDCAdapterClassStr() { - return Log4jMDCAdapter.class.getName(); - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticMarkerBinder.java b/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticMarkerBinder.java deleted file mode 100644 index d295d1016e0..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/StaticMarkerBinder.java +++ /dev/null @@ -1,54 +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.slf4j.impl; - -import org.apache.logging.slf4j.Log4jMarkerFactory; -import org.slf4j.IMarkerFactory; -import org.slf4j.spi.MarkerFactoryBinder; - -/** - * SLF4J MarkerFactoryBinder implementation using Log4j. This class is part of the required classes used to specify an - * SLF4J logging provider implementation. - */ -public class StaticMarkerBinder implements MarkerFactoryBinder { - - /** - * The unique instance of this class. - */ - public static final StaticMarkerBinder SINGLETON = new StaticMarkerBinder(); - - private final IMarkerFactory markerFactory = new Log4jMarkerFactory(); - - /** - * Returns the {@link #SINGLETON} {@link StaticMarkerBinder}. - * Added to slf4j-api 1.7.14 via https://github.com/qos-ch/slf4j/commit/ea3cca72cd5a9329a06b788317a17e806ee8acd0 - * @return the singleton instance - */ - public static StaticMarkerBinder getSingleton() { - return SINGLETON; - } - - @Override - public IMarkerFactory getMarkerFactory() { - return markerFactory; - } - - @Override - public String getMarkerFactoryClassStr() { - return Log4jMarkerFactory.class.getName(); - } -} diff --git a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/package-info.java b/log4j-slf4j-impl/src/main/java/org/slf4j/impl/package-info.java deleted file mode 100644 index aaa5b269037..00000000000 --- a/log4j-slf4j-impl/src/main/java/org/slf4j/impl/package-info.java +++ /dev/null @@ -1,25 +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. - */ -/** - * Log4j 2.0 SLF4J Binding. - */ -@Export -@Version("2.20.1") -package org.slf4j.impl; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/other/pkg/LoggerContextAnchorTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/other/pkg/LoggerContextAnchorTest.java deleted file mode 100644 index 5732c205a4e..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/other/pkg/LoggerContextAnchorTest.java +++ /dev/null @@ -1,91 +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.logging.other.pkg; - -import static org.junit.Assert.assertEquals; - -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.status.StatusData; -import org.apache.logging.log4j.status.StatusListener; -import org.apache.logging.log4j.status.StatusLogger; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -/** - * Test LoggerContext lookups by verifying the anchor class representing calling code. - */ -public class LoggerContextAnchorTest { - private static final String PREFIX = "Log4jLoggerFactory.getContext() found anchor class "; - - @Test - public void testLoggerFactoryLookupClass() { - final String fqcn = getAnchorFqcn(() -> LoggerFactory.getLogger(LoggerContextAnchorTest.class)); - assertEquals(getClass().getName(), fqcn); - } - - @Test - public void testLoggerFactoryLookupString() { - final String fqcn = getAnchorFqcn(() -> LoggerFactory.getLogger("custom.logger")); - assertEquals(getClass().getName(), fqcn); - } - - @Test - public void testLoggerFactoryGetILoggerFactoryLookup() { - final String fqcn = - getAnchorFqcn(() -> LoggerFactory.getILoggerFactory().getLogger("custom.logger")); - assertEquals(getClass().getName(), fqcn); - } - - private static String getAnchorFqcn(final Runnable runnable) { - final List results = new CopyOnWriteArrayList<>(); - final StatusListener listener = new StatusListener() { - @Override - public void log(final StatusData data) { - final String formattedMessage = data.getMessage().getFormattedMessage(); - if (formattedMessage.startsWith(PREFIX)) { - results.add(formattedMessage.substring(PREFIX.length())); - } - } - - @Override - public Level getStatusLevel() { - return Level.TRACE; - } - - @Override - public void close() { - // nop - } - }; - final StatusLogger statusLogger = StatusLogger.getLogger(); - statusLogger.registerListener(listener); - try { - runnable.run(); - if (results.isEmpty()) { - throw new AssertionError("Failed to locate an anchor lookup status message"); - } - if (results.size() > 1) { - throw new AssertionError("Found multiple anchor lines: " + results); - } - return results.get(0); - } finally { - statusLogger.removeListener(listener); - } - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java deleted file mode 100644 index efcce8b2ad0..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java +++ /dev/null @@ -1,66 +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.logging.slf4j; - -import static org.junit.Assert.assertEquals; - -import java.util.List; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.junit.ClassRule; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class CallerInformationTest { - - // config from log4j-core test-jar - private static final String CONFIG = "log4j2-calling-class.xml"; - - @ClassRule - public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG); - - @Test - public void testClassLogger() throws Exception { - final ListAppender app = ctx.getListAppender("Class").clear(); - final Logger logger = LoggerFactory.getLogger("ClassLogger"); - logger.info("Ignored message contents."); - logger.warn("Verifying the caller class is still correct."); - logger.error("Hopefully nobody breaks me!"); - final List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 3, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller class name.", this.getClass().getName(), message); - } - } - - @Test - public void testMethodLogger() throws Exception { - final ListAppender app = ctx.getListAppender("Method").clear(); - final Logger logger = LoggerFactory.getLogger("MethodLogger"); - logger.info("More messages."); - logger.warn("CATASTROPHE INCOMING!"); - logger.error("ZOMBIES!!!"); - logger.warn("brains~~~"); - logger.info("Itchy. Tasty."); - final List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 5, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller method name.", "testMethodLogger", message); - } - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/CustomFlatMarker.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/CustomFlatMarker.java deleted file mode 100644 index 3d822097282..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/CustomFlatMarker.java +++ /dev/null @@ -1,74 +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.logging.slf4j; - -import java.util.Iterator; -import org.slf4j.Marker; - -/** - * Test Marker that may contain no reference/parent Markers. - * @see LOG4J2-793 - */ -public class CustomFlatMarker implements Marker { - private static final long serialVersionUID = -4115520883240247266L; - - private final String name; - - public CustomFlatMarker(final String name) { - this.name = name; - } - - @Override - public String getName() { - return name; - } - - @Override - public void add(final Marker reference) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean remove(final Marker reference) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean hasChildren() { - return hasReferences(); - } - - @Override - public boolean hasReferences() { - return false; - } - - @Override - public Iterator iterator() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean contains(final Marker other) { - return false; - } - - @Override - public boolean contains(final String name) { - return false; - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java deleted file mode 100644 index 85d9d0dff4d..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java +++ /dev/null @@ -1,53 +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.logging.slf4j; - -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Tests logging during shutdown. - */ -public class Log4j1222Test { - - @Test - public void homepageRendersSuccessfully() { - System.setProperty("log4j.configurationFile", "log4j2-console.xml"); - Runtime.getRuntime().addShutdownHook(new ShutdownHook()); - } - - private static class ShutdownHook extends Thread { - - private static class Holder { - private static final Logger LOGGER = LoggerFactory.getLogger(Log4j1222Test.class); - } - - @Override - public void run() { - super.run(); - trigger(); - } - - private void trigger() { - Holder.LOGGER.info("Attempt to trigger"); - assertTrue("Logger is of type " + Holder.LOGGER.getClass().getName(), Holder.LOGGER instanceof Log4jLogger); - } - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j2_1482_Slf4jTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j2_1482_Slf4jTest.java deleted file mode 100644 index d8abaf65a46..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4j2_1482_Slf4jTest.java +++ /dev/null @@ -1,39 +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.logging.slf4j; - -import org.apache.logging.log4j.core.test.layout.Log4j2_1482_Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Tests https://issues.apache.org/jira/browse/LOG4J2-1482 - */ -public class Log4j2_1482_Slf4jTest extends Log4j2_1482_Test { - - @Override - protected void log(final int runNumber) { - if (runNumber == 2) { - // System.out.println("Set a breakpoint here."); - } - final Logger logger = LoggerFactory.getLogger("auditcsvfile"); - final int val1 = 9, val2 = 11, val3 = 12; - logger.info("Info Message!", val1, val2, val3); - logger.info("Info Message!", val1, val2, val3); - logger.info("Info Message!", val1, val2, val3); - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java deleted file mode 100644 index 837b392a698..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java +++ /dev/null @@ -1,46 +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.logging.slf4j; - -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class Log4jMarkerTest { - - private static Log4jMarkerFactory markerFactory; - - @BeforeClass - public static void startup() { - markerFactory = ((Log4jLoggerFactory) org.slf4j.LoggerFactory.getILoggerFactory()).getMarkerFactory(); - } - - @Test - public void testEquals() { - final Marker markerA = MarkerManager.getMarker(Log4jMarkerTest.class.getName() + "-A"); - final Marker markerB = MarkerManager.getMarker(Log4jMarkerTest.class.getName() + "-B"); - final Log4jMarker marker1 = new Log4jMarker(markerFactory, markerA); - final Log4jMarker marker2 = new Log4jMarker(markerFactory, markerA); - final Log4jMarker marker3 = new Log4jMarker(markerFactory, markerB); - Assert.assertEquals(marker1, marker2); - Assert.assertNotEquals(marker1, null); - Assert.assertNotEquals(null, marker1); - Assert.assertNotEquals(marker1, marker3); - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java deleted file mode 100644 index d77e1d4d342..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java +++ /dev/null @@ -1,43 +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.logging.slf4j; - -import static org.junit.Assert.assertTrue; - -import java.util.Set; -import org.apache.logging.log4j.core.LifeCycle; -import org.apache.logging.log4j.spi.LoggerContext; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -/** - * Tests cleanup of the LoggerContexts. - */ -public class LoggerContextTest { - - @Test - public void testCleanup() throws Exception { - final Log4jLoggerFactory factory = (Log4jLoggerFactory) LoggerFactory.getILoggerFactory(); - factory.getLogger("test"); - Set set = factory.getLoggerContexts(); - final LoggerContext ctx1 = set.toArray(LoggerContext.EMPTY_ARRAY)[0]; - assertTrue("LoggerContext is not enabled for shutdown", ctx1 instanceof LifeCycle); - ((LifeCycle) ctx1).stop(); - set = factory.getLoggerContexts(); - assertTrue("Expected no LoggerContexts", set.isEmpty()); - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java deleted file mode 100644 index c1fc9a7841e..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java +++ /dev/null @@ -1,171 +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.logging.slf4j; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.List; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.apache.logging.log4j.util.Strings; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.slf4j.Marker; -import org.slf4j.spi.LocationAwareLogger; - -/** - * - */ -public class LoggerTest { - - private static final String CONFIG = "log4j-test1.xml"; - - @ClassRule - public static LoggerContextRule ctx = new LoggerContextRule(CONFIG); - - Logger logger = LoggerFactory.getLogger("LoggerTest"); - - @Test - public void debug() { - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void debugNoParms() { - logger.debug("Debug message {}"); - verify("o.a.l.s.LoggerTest Debug message {} MDC{}" + Strings.LINE_SEPARATOR); - logger.debug("Debug message {}", (Object[]) null); - verify("o.a.l.s.LoggerTest Debug message {} MDC{}" + Strings.LINE_SEPARATOR); - ((LocationAwareLogger) logger) - .log(null, Log4jLogger.class.getName(), LocationAwareLogger.DEBUG_INT, "Debug message {}", null, null); - verify("o.a.l.s.LoggerTest Debug message {} MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void debugWithParms() { - logger.debug("Hello, {}", "World"); - verify("o.a.l.s.LoggerTest Hello, World MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void mdc() { - - MDC.put("TestYear", "2010"); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{TestYear=2010}" + Strings.LINE_SEPARATOR); - MDC.clear(); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{}" + Strings.LINE_SEPARATOR); - } - - /** - * @see LOG4J2-793 - */ - @Test - public void supportsCustomSLF4JMarkers() { - final Marker marker = new CustomFlatMarker("TEST"); - logger.debug(marker, "Test"); - verify("o.a.l.s.LoggerTest Test MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void testRootLogger() { - final Logger l = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); - assertNotNull("No Root Logger", l); - assertEquals(Logger.ROOT_LOGGER_NAME, l.getName()); - } - - @Test - public void doubleSubst() { - logger.debug("Hello, {}", "Log4j {}"); - verify("o.a.l.s.LoggerTest Hello, Log4j {} MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void testThrowable() { - final Throwable expected = new RuntimeException(); - logger.debug("Hello {}", expected); - verifyThrowable(expected); - logger.debug("Hello {}", (Object) expected); - verifyThrowable(null); - logger.debug("Hello", expected); - verifyThrowable(expected); - logger.debug("Hello {}! {}", "World!", expected); - verifyThrowable(null); - logger.debug("Hello {}!", "World!", expected); - verifyThrowable(expected); - final LocationAwareLogger lal = (LocationAwareLogger) logger; - lal.log(null, LoggerTest.class.getName(), LocationAwareLogger.DEBUG_INT, "Hello {}", null, expected); - verifyThrowable(expected); - lal.log( - null, - LoggerTest.class.getName(), - LocationAwareLogger.DEBUG_INT, - "Hello {}", - new Object[] {expected}, - null); - verifyThrowable(null); - lal.log( - null, - LoggerTest.class.getName(), - LocationAwareLogger.DEBUG_INT, - "Hello {}", - new Object[] {"World!", expected}, - null); - verifyThrowable(expected); - } - - private ListAppender getAppenderByName(final String name) { - final ListAppender listApp = ctx.getListAppender(name); - assertNotNull("Missing Appender", listApp); - return listApp; - } - - private void verify(final String expected) { - final ListAppender listApp = getAppenderByName("List"); - final List events = listApp.getMessages(); - assertEquals("Incorrect number of messages. Expected 1 Actual " + events.size(), 1, events.size()); - final String actual = events.get(0); - assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual); - listApp.clear(); - } - - private void verifyThrowable(final Throwable expected) { - final ListAppender listApp = getAppenderByName("UnformattedList"); - final List events = listApp.getEvents(); - assertEquals("Incorrect number of messages", 1, events.size()); - final LogEvent actual = events.get(0); - assertEquals("Incorrect throwable.", expected, actual.getThrown()); - listApp.clear(); - } - - @Before - @After - public void cleanup() { - MDC.clear(); - ctx.getListAppender("List").clear(); - ctx.getListAppender("UnformattedList").clear(); - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java deleted file mode 100644 index 0db3e30393e..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java +++ /dev/null @@ -1,197 +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.logging.slf4j; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * - */ -public class MarkerTest { - - private static final String CHILD_MAKER_NAME = MarkerTest.class.getSimpleName() + "-TEST"; - private static final String PARENT_MARKER_NAME = MarkerTest.class.getSimpleName() + "-PARENT"; - private static Log4jMarkerFactory markerFactory; - - @BeforeClass - public static void startup() { - markerFactory = ((Log4jLoggerFactory) org.slf4j.LoggerFactory.getILoggerFactory()).getMarkerFactory(); - } - - @Before - @After - public void clearMarkers() { - MarkerManager.clear(); - } - - @Test - public void testAddMarker() { - final String childMakerName = CHILD_MAKER_NAME + "-AM"; - final String parentMarkerName = PARENT_MARKER_NAME + "-AM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMakerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMarkerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMarkerName); - final Marker log4jMarker = MarkerManager.getMarker(childMakerName); - - assertTrue("Incorrect Marker class", slf4jMarker instanceof Log4jMarker); - assertTrue( - String.format( - "%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j", - childMakerName, parentMarkerName, log4jMarker, log4jParent), - log4jMarker.isInstanceOf(log4jParent)); - assertTrue( - String.format( - "%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J", - childMakerName, parentMarkerName, slf4jMarker, slf4jParent), - slf4jMarker.contains(slf4jParent)); - } - - @Test - public void testAddNullMarker() { - final String childMarkerName = CHILD_MAKER_NAME + "-ANM"; - final String parentMakerName = PARENT_MARKER_NAME + "-ANM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final org.slf4j.Marker nullMarker = null; - try { - log4jSlf4jParent.add(nullMarker); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - try { - log4jSlf4jMarker.add(nullMarker); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - } - - @Test - public void testAddSameMarker() { - final String childMarkerName = CHILD_MAKER_NAME + "-ASM"; - final String parentMakerName = PARENT_MARKER_NAME + "-ASM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - assertTrue( - String.format( - "%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j", - childMarkerName, parentMakerName, log4jMarker, log4jParent), - log4jMarker.isInstanceOf(log4jParent)); - assertTrue( - String.format( - "%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J", - childMarkerName, parentMakerName, slf4jMarker, slf4jParent), - slf4jMarker.contains(slf4jParent)); - } - - @Test - public void testEquals() { - final String childMarkerName = CHILD_MAKER_NAME + "-ASM"; - final String parentMakerName = PARENT_MARKER_NAME + "-ASM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jMarker2 = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Marker log4jMarker2 = MarkerManager.getMarker(childMarkerName); - assertEquals(log4jMarker, log4jMarker2); - assertEquals(slf4jMarker, slf4jMarker2); - assertNotEquals(log4jParent, log4jMarker); - assertNotEquals(slf4jParent, slf4jMarker); - } - - @Test - public void testContainsNullMarker() { - final String childMarkerName = CHILD_MAKER_NAME + "-CM"; - final String parentMakerName = PARENT_MARKER_NAME + "-CM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final org.slf4j.Marker nullMarker = null; - try { - Assert.assertFalse(log4jSlf4jParent.contains(nullMarker)); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - try { - Assert.assertFalse(log4jSlf4jMarker.contains(nullMarker)); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - } - - @Test - public void testContainsNullString() { - final String childMarkerName = CHILD_MAKER_NAME + "-CS"; - final String parentMakerName = PARENT_MARKER_NAME + "-CS"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final String nullStr = null; - Assert.assertFalse(log4jSlf4jParent.contains(nullStr)); - Assert.assertFalse(log4jSlf4jMarker.contains(nullStr)); - } - - @Test - public void testRemoveNullMarker() { - final String childMakerName = CHILD_MAKER_NAME + "-CM"; - final String parentMakerName = PARENT_MARKER_NAME + "-CM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMakerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMakerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final org.slf4j.Marker nullMarker = null; - Assert.assertFalse(log4jSlf4jParent.remove(nullMarker)); - Assert.assertFalse(log4jSlf4jMarker.remove(nullMarker)); - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OptionalTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OptionalTest.java deleted file mode 100644 index f56cd9435dc..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OptionalTest.java +++ /dev/null @@ -1,69 +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.logging.slf4j; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import java.util.List; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.apache.logging.log4j.util.Strings; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.slf4j.Marker; -import org.slf4j.MarkerFactory; - -/** - * - */ -public class OptionalTest { - - private static final String CONFIG = "log4j-test1.xml"; - - @ClassRule - public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG); - - Logger logger = LoggerFactory.getLogger("EventLogger"); - Marker marker = MarkerFactory.getMarker("EVENT"); - - @Test - public void testEventLogger() { - logger.info(marker, "This is a test"); - MDC.clear(); - verify("EventLogger", "o.a.l.s.OptionalTest This is a test" + Strings.LINE_SEPARATOR); - } - - private void verify(final String name, final String expected) { - final ListAppender listApp = CTX.getListAppender(name); - final List events = listApp.getMessages(); - assertTrue("Incorrect number of messages. Expected 1 Actual " + events.size(), events.size() == 1); - final String actual = events.get(0); - assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual); - listApp.clear(); - } - - @Before - public void cleanup() { - CTX.getListAppender("List").clear(); - CTX.getListAppender("EventLogger").clear(); - } -} diff --git a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OverflowTest.java b/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OverflowTest.java deleted file mode 100644 index a0e5b0001a2..00000000000 --- a/log4j-slf4j-impl/src/test/java/org/apache/logging/slf4j/OverflowTest.java +++ /dev/null @@ -1,41 +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.logging.slf4j; - -import static org.junit.Assert.fail; - -import org.apache.logging.log4j.LoggingException; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -/** - * Tests StackOverflow when slf4j-impl and to-slf4j are both present. - */ -public class OverflowTest { - - @Test - public void log() { - try { - LoggerFactory.getLogger(OverflowTest.class); - fail("Failed to detect inclusion of log4j-to-slf4j"); - } catch (LoggingException ex) { - // Expected exception. - } catch (StackOverflowError error) { - fail("Failed to detect inclusion of log4j-to-slf4j, caught StackOverflowError"); - } - } -} diff --git a/log4j-slf4j-impl/src/test/resources/log4j-test1.xml b/log4j-slf4j-impl/src/test/resources/log4j-test1.xml deleted file mode 100644 index b3ad6067a1d..00000000000 --- a/log4j-slf4j-impl/src/test/resources/log4j-test1.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - target/test.log - - - - - - - - - - - - - %d %p %C{1.} [%t] %m%n - - - - - - - - - - - - - > - - - - - - - - - - - - diff --git a/log4j-slf4j-impl/src/test/resources/log4j2-1482.xml b/log4j-slf4j-impl/src/test/resources/log4j2-1482.xml deleted file mode 100644 index af7af4a622c..00000000000 --- a/log4j-slf4j-impl/src/test/resources/log4j2-1482.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - target/log4j2-1482 - audit - param1,param2,param3${sys:line.separator} - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-slf4j2-impl/pom.xml b/log4j-slf4j2-impl/pom.xml deleted file mode 100644 index b48e3f50feb..00000000000 --- a/log4j-slf4j2-impl/pom.xml +++ /dev/null @@ -1,160 +0,0 @@ - - - - - 4.0.0 - - - org.apache.logging.log4j - log4j - ${revision} - ../log4j-parent - - - log4j-slf4j2-impl - - SLF4J 2 Provider for Log4j API - - SLF4J 2 provider (binding) for the Apache Log4j API. - It forwards SLF4J 2 calls to the Log4j API. - This effectively allows using Log4j as an implementation of SLF4J 2. - (Refer to the `log4j-to-slf4j` artifact for forwarding the Log4j API to SLF4J.) - - - 2.0.16 - - - - - - org.slf4j - slf4j-api - ${slf4j2.version} - - - - - - - org.osgi - org.osgi.framework - provided - - - org.apache.logging.log4j - log4j-api - - - org.slf4j - slf4j-api - - - org.apache.logging.log4j - log4j-core - runtime - - - org.apache.logging.log4j - log4j-api-test - test - - - org.apache.logging.log4j - log4j-core-test - test - - - org.apache.logging.log4j - log4j-to-slf4j - test - - - org.assertj - assertj-core - test - - - org.apache.commons - commons-csv - test - - - org.apache.commons - commons-lang3 - test - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.jupiter - junit-jupiter-params - test - - - org.junit.vintage - junit-vintage-engine - test - - - - - - - org.apache.maven.plugins - maven-surefire-plugin - - - loop-test - - test - - test - - - **/OverflowTest.java - - junit-vintage - - - - default-test - - test - - test - - - **/*Test.java - - - **/OverflowTest.java - - - org.apache.logging.log4j:log4j-to-slf4j - - - - - - - - - diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java deleted file mode 100644 index 474fb057dc5..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jEventBuilder.java +++ /dev/null @@ -1,163 +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.logging.slf4j; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Supplier; -import org.apache.logging.log4j.BridgeAware; -import org.apache.logging.log4j.CloseableThreadContext; -import org.apache.logging.log4j.CloseableThreadContext.Instance; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogBuilder; -import org.apache.logging.log4j.Logger; -import org.slf4j.Marker; -import org.slf4j.spi.CallerBoundaryAware; -import org.slf4j.spi.LoggingEventBuilder; - -public class Log4jEventBuilder implements LoggingEventBuilder, CallerBoundaryAware { - - private static final String FQCN = Log4jEventBuilder.class.getName(); - - private final Log4jMarkerFactory markerFactory; - private final Logger logger; - private final List arguments = new ArrayList<>(); - private String message = null; - private org.apache.logging.log4j.Marker marker = null; - private Throwable throwable = null; - private Map keyValuePairs = null; - private final Level level; - private String fqcn = FQCN; - - public Log4jEventBuilder(final Log4jMarkerFactory markerFactory, final Logger logger, final Level level) { - this.markerFactory = markerFactory; - this.logger = logger; - this.level = level; - } - - @Override - public LoggingEventBuilder setCause(final Throwable cause) { - this.throwable = cause; - return this; - } - - @Override - public LoggingEventBuilder addMarker(final Marker marker) { - this.marker = markerFactory.getLog4jMarker(marker); - return this; - } - - @Override - public LoggingEventBuilder addArgument(final Object p) { - arguments.add(p); - return this; - } - - @Override - public LoggingEventBuilder addArgument(final Supplier objectSupplier) { - arguments.add(objectSupplier.get()); - return this; - } - - @Override - public LoggingEventBuilder addKeyValue(final String key, final Object value) { - if (keyValuePairs == null) { - keyValuePairs = new HashMap<>(); - } - keyValuePairs.put(key, String.valueOf(value)); - return this; - } - - @Override - public LoggingEventBuilder addKeyValue(final String key, final Supplier valueSupplier) { - if (keyValuePairs == null) { - keyValuePairs = new HashMap<>(); - } - keyValuePairs.put(key, String.valueOf(valueSupplier.get())); - return this; - } - - @Override - public LoggingEventBuilder setMessage(final String message) { - this.message = message; - return this; - } - - @Override - public LoggingEventBuilder setMessage(final Supplier messageSupplier) { - this.message = messageSupplier.get(); - return this; - } - - @Override - public void log() { - final LogBuilder logBuilder = logger.atLevel(level).withMarker(marker).withThrowable(throwable); - if (logBuilder instanceof BridgeAware) { - ((BridgeAware) logBuilder).setEntryPoint(fqcn); - } - if (keyValuePairs == null || keyValuePairs.isEmpty()) { - logBuilder.log(message, arguments.toArray()); - } else { - try (final Instance c = CloseableThreadContext.putAll(keyValuePairs)) { - logBuilder.log(message, arguments.toArray()); - } - } - } - - @Override - public void log(final String message) { - setMessage(message); - log(); - } - - @Override - public void log(final String message, final Object arg) { - setMessage(message); - addArgument(arg); - log(); - } - - @Override - public void log(final String message, final Object arg0, final Object arg1) { - setMessage(message); - addArgument(arg0); - addArgument(arg1); - log(); - } - - @Override - public void log(final String message, final Object... args) { - setMessage(message); - for (final Object arg : args) { - addArgument(arg); - } - log(); - } - - @Override - public void log(final Supplier messageSupplier) { - setMessage(messageSupplier); - log(); - } - - @Override - public void setCallerBoundary(String fqcn) { - this.fqcn = fqcn; - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java deleted file mode 100644 index feb730615d9..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jLogger.java +++ /dev/null @@ -1,402 +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.logging.slf4j; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.ParameterizedMessage; -import org.apache.logging.log4j.message.SimpleMessage; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.slf4j.Marker; -import org.slf4j.spi.LocationAwareLogger; -import org.slf4j.spi.LoggingEventBuilder; - -/** - * SLF4J logger implementation that uses Log4j. - */ -public class Log4jLogger implements LocationAwareLogger { - - public static final String FQCN = Log4jLogger.class.getName(); - - private final ExtendedLogger logger; - private final String name; - private final Log4jMarkerFactory markerFactory; - - public Log4jLogger(final Log4jMarkerFactory markerFactory, final ExtendedLogger logger, final String name) { - this.markerFactory = markerFactory; - this.logger = logger; - this.name = name; - } - - @Override - public void trace(final String format) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format); - } - - @Override - public void trace(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, o); - } - - @Override - public void trace(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, arg1, arg2); - } - - @Override - public void trace(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, args); - } - - @Override - public void trace(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.TRACE, null, format, t); - } - - @Override - public boolean isTraceEnabled() { - return logger.isEnabled(Level.TRACE, null, null); - } - - @Override - public boolean isTraceEnabled(final Marker marker) { - return logger.isEnabled(Level.TRACE, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void trace(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void trace(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void trace(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void trace(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void trace(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.TRACE, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void debug(final String format) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format); - } - - @Override - public void debug(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, o); - } - - @Override - public void debug(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, arg1, arg2); - } - - @Override - public void debug(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, args); - } - - @Override - public void debug(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.DEBUG, null, format, t); - } - - @Override - public boolean isDebugEnabled() { - return logger.isEnabled(Level.DEBUG, null, null); - } - - @Override - public boolean isDebugEnabled(final Marker marker) { - return logger.isEnabled(Level.DEBUG, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void debug(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void debug(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void debug(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void debug(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void debug(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.DEBUG, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void info(final String format) { - logger.logIfEnabled(FQCN, Level.INFO, null, format); - } - - @Override - public void info(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, o); - } - - @Override - public void info(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, arg1, arg2); - } - - @Override - public void info(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, args); - } - - @Override - public void info(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.INFO, null, format, t); - } - - @Override - public boolean isInfoEnabled() { - return logger.isEnabled(Level.INFO, null, null); - } - - @Override - public boolean isInfoEnabled(final Marker marker) { - return logger.isEnabled(Level.INFO, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void info(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void info(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void info(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void info(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void info(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.INFO, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void warn(final String format) { - logger.logIfEnabled(FQCN, Level.WARN, null, format); - } - - @Override - public void warn(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, o); - } - - @Override - public void warn(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, arg1, arg2); - } - - @Override - public void warn(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, args); - } - - @Override - public void warn(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.WARN, null, format, t); - } - - @Override - public boolean isWarnEnabled() { - return logger.isEnabled(Level.WARN, null, null); - } - - @Override - public boolean isWarnEnabled(final Marker marker) { - return logger.isEnabled(Level.WARN, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void warn(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void warn(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void warn(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void warn(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void warn(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.WARN, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void error(final String format) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format); - } - - @Override - public void error(final String format, final Object o) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, o); - } - - @Override - public void error(final String format, final Object arg1, final Object arg2) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, arg1, arg2); - } - - @Override - public void error(final String format, final Object... args) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, args); - } - - @Override - public void error(final String format, final Throwable t) { - logger.logIfEnabled(FQCN, Level.ERROR, null, format, t); - } - - @Override - public boolean isErrorEnabled() { - return logger.isEnabled(Level.ERROR, null, null); - } - - @Override - public boolean isErrorEnabled(final Marker marker) { - return logger.isEnabled(Level.ERROR, markerFactory.getLog4jMarker(marker), null); - } - - @Override - public void error(final Marker marker, final String s) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s); - } - - @Override - public void error(final Marker marker, final String s, final Object o) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, o); - } - - @Override - public void error(final Marker marker, final String s, final Object o, final Object o1) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, o, o1); - } - - @Override - public void error(final Marker marker, final String s, final Object... objects) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, objects); - } - - @Override - public void error(final Marker marker, final String s, final Throwable throwable) { - logger.logIfEnabled(FQCN, Level.ERROR, markerFactory.getLog4jMarker(marker), s, throwable); - } - - @Override - public void log( - final Marker marker, - final String fqcn, - final int level, - final String message, - final Object[] params, - final Throwable throwable) { - final Level log4jLevel = getLevel(level); - final org.apache.logging.log4j.Marker log4jMarker = markerFactory.getLog4jMarker(marker); - - if (!logger.isEnabled(log4jLevel, log4jMarker, message, params)) { - return; - } - final Message msg; - final Throwable actualThrowable; - if (params == null) { - msg = new SimpleMessage(message); - actualThrowable = throwable; - } else { - msg = new ParameterizedMessage(message, params, throwable); - actualThrowable = throwable != null ? throwable : msg.getThrowable(); - } - logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, actualThrowable); - } - - @Override - public String getName() { - return name; - } - - private static Level getLevel(final int i) { - switch (i) { - case TRACE_INT: - return Level.TRACE; - case DEBUG_INT: - return Level.DEBUG; - case INFO_INT: - return Level.INFO; - case WARN_INT: - return Level.WARN; - case ERROR_INT: - return Level.ERROR; - } - return Level.ERROR; - } - - @Override - public LoggingEventBuilder makeLoggingEventBuilder(final org.slf4j.event.Level level) { - final Level log4jLevel = getLevel(level.toInt()); - return new Log4jEventBuilder(markerFactory, logger, log4jLevel); - } - - @Override - public boolean isEnabledForLevel(final org.slf4j.event.Level level) { - return logger.isEnabled(getLevel(level.toInt())); - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jLoggerFactory.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jLoggerFactory.java deleted file mode 100644 index e5940be3fe1..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jLoggerFactory.java +++ /dev/null @@ -1,71 +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.logging.slf4j; - -import java.util.function.Predicate; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.LoggingException; -import org.apache.logging.log4j.spi.AbstractLoggerAdapter; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.StackLocatorUtil; -import org.slf4j.ILoggerFactory; -import org.slf4j.Logger; - -/** - * Log4j implementation of SLF4J ILoggerFactory interface. - */ -public class Log4jLoggerFactory extends AbstractLoggerAdapter implements ILoggerFactory { - - private static final StatusLogger LOGGER = StatusLogger.getLogger(); - private static final String SLF4J_PACKAGE = "org.slf4j"; - private static final Predicate> CALLER_PREDICATE = clazz -> - !AbstractLoggerAdapter.class.equals(clazz) && !clazz.getName().startsWith(SLF4J_PACKAGE); - private static final String TO_SLF4J_CONTEXT = "org.apache.logging.slf4j.SLF4JLoggerContext"; - - private final Log4jMarkerFactory markerFactory; - - public Log4jLoggerFactory(final Log4jMarkerFactory markerFactory) { - this.markerFactory = markerFactory; - } - - @Override - protected Logger newLogger(final String name, final LoggerContext context) { - final String key = Logger.ROOT_LOGGER_NAME.equals(name) ? LogManager.ROOT_LOGGER_NAME : name; - return new Log4jLogger(markerFactory, validateContext(context).getLogger(key), name); - } - - @Override - protected LoggerContext getContext() { - final Class anchor = LogManager.getFactory().isClassLoaderDependent() - ? StackLocatorUtil.getCallerClass(Log4jLoggerFactory.class, CALLER_PREDICATE) - : null; - LOGGER.trace("Log4jLoggerFactory.getContext() found anchor {}", anchor); - return anchor == null ? LogManager.getContext(false) : getContext(anchor); - } - - Log4jMarkerFactory getMarkerFactory() { - return markerFactory; - } - - private LoggerContext validateContext(final LoggerContext context) { - if (TO_SLF4J_CONTEXT.equals(context.getClass().getName())) { - throw new LoggingException("log4j-slf4j2-impl cannot be present with log4j-to-slf4j"); - } - return context; - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMDCAdapter.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMDCAdapter.java deleted file mode 100644 index bae69e961d2..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMDCAdapter.java +++ /dev/null @@ -1,151 +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.logging.slf4j; - -import java.util.ArrayDeque; -import java.util.Deque; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.ThreadContext.ContextStack; -import org.apache.logging.log4j.status.StatusLogger; -import org.slf4j.spi.MDCAdapter; - -/** - * - */ -public class Log4jMDCAdapter implements MDCAdapter { - - private static final Logger LOGGER = StatusLogger.getLogger(); - - private final ThreadLocalMapOfStacks mapOfStacks = new ThreadLocalMapOfStacks(); - - @Override - public void put(final String key, final String val) { - ThreadContext.put(key, val); - } - - @Override - public String get(final String key) { - return ThreadContext.get(key); - } - - @Override - public void remove(final String key) { - ThreadContext.remove(key); - } - - @Override - public void clear() { - ThreadContext.clearMap(); - } - - @Override - public Map getCopyOfContextMap() { - return ThreadContext.getContext(); - } - - @Override - public void setContextMap(final Map map) { - ThreadContext.clearMap(); - ThreadContext.putAll(map); - } - - @Override - public void pushByKey(final String key, final String value) { - if (key == null) { - ThreadContext.push(value); - } else { - final String oldValue = mapOfStacks.peekByKey(key); - if (!Objects.equals(ThreadContext.get(key), oldValue)) { - LOGGER.warn("The key {} was used in both the string and stack-valued MDC.", key); - } - mapOfStacks.pushByKey(key, value); - ThreadContext.put(key, value); - } - } - - @Override - public String popByKey(final String key) { - if (key == null) { - return ThreadContext.getDepth() > 0 ? ThreadContext.pop() : null; - } - final String value = mapOfStacks.popByKey(key); - if (!Objects.equals(ThreadContext.get(key), value)) { - LOGGER.warn("The key {} was used in both the string and stack-valued MDC.", key); - } - ThreadContext.put(key, mapOfStacks.peekByKey(key)); - return value; - } - - @Override - public Deque getCopyOfDequeByKey(final String key) { - if (key == null) { - final ContextStack stack = ThreadContext.getImmutableStack(); - final Deque copy = new ArrayDeque<>(stack.size()); - stack.forEach(copy::push); - return copy; - } - return mapOfStacks.getCopyOfDequeByKey(key); - } - - @Override - public void clearDequeByKey(final String key) { - if (key == null) { - ThreadContext.clearStack(); - } else { - mapOfStacks.clearByKey(key); - ThreadContext.put(key, null); - } - } - - private static class ThreadLocalMapOfStacks { - - private final ThreadLocal>> tlMapOfStacks = ThreadLocal.withInitial(HashMap::new); - - public void pushByKey(final String key, final String value) { - tlMapOfStacks - .get() - .computeIfAbsent(key, ignored -> new ArrayDeque<>()) - .push(value); - } - - public String popByKey(final String key) { - final Deque deque = tlMapOfStacks.get().get(key); - return deque != null ? deque.poll() : null; - } - - public Deque getCopyOfDequeByKey(final String key) { - final Deque deque = tlMapOfStacks.get().get(key); - return deque != null ? new ArrayDeque<>(deque) : null; - } - - public void clearByKey(final String key) { - final Deque deque = tlMapOfStacks.get().get(key); - if (deque != null) { - deque.clear(); - } - } - - public String peekByKey(final String key) { - final Deque deque = tlMapOfStacks.get().get(key); - return deque != null ? deque.peek() : null; - } - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java deleted file mode 100644 index e2fbe624911..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMarker.java +++ /dev/null @@ -1,126 +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.logging.slf4j; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; -import org.apache.logging.log4j.MarkerManager; -import org.slf4j.IMarkerFactory; -import org.slf4j.Marker; - -/** - * Log4j/SLF4J {@link Marker} type bridge. - */ -class Log4jMarker implements Marker { - - public static final long serialVersionUID = 1590472L; - - private final IMarkerFactory factory; - - private final org.apache.logging.log4j.Marker marker; - - /** - * Constructs a Log4jMarker using an existing Log4j {@link org.apache.logging.log4j.Marker}. - * @param marker The Log4j Marker upon which to base this Marker. - */ - public Log4jMarker(final IMarkerFactory markerFactory, final org.apache.logging.log4j.Marker marker) { - this.factory = markerFactory; - this.marker = marker; - } - - @Override - public void add(final Marker marker) { - if (marker == null) { - throw new IllegalArgumentException(); - } - final Marker m = factory.getMarker(marker.getName()); - this.marker.addParents(((Log4jMarker) m).getLog4jMarker()); - } - - @Override - public boolean contains(final Marker marker) { - if (marker == null) { - throw new IllegalArgumentException(); - } - return this.marker.isInstanceOf(marker.getName()); - } - - @Override - public boolean contains(final String s) { - return s != null ? this.marker.isInstanceOf(s) : false; - } - - @Override - public boolean equals(final Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof Log4jMarker)) { - return false; - } - final Log4jMarker other = (Log4jMarker) obj; - return Objects.equals(marker, other.marker); - } - - public org.apache.logging.log4j.Marker getLog4jMarker() { - return marker; - } - - @Override - public String getName() { - return marker.getName(); - } - - @Override - public boolean hasChildren() { - return marker.hasParents(); - } - - @Override - public int hashCode() { - return 31 + Objects.hashCode(marker); - } - - @Override - public boolean hasReferences() { - return marker.hasParents(); - } - - @Override - public Iterator iterator() { - final org.apache.logging.log4j.Marker[] log4jParents = this.marker.getParents(); - if (log4jParents == null) { - return Collections.emptyIterator(); - } - final List parents = new ArrayList<>(log4jParents.length); - for (final org.apache.logging.log4j.Marker m : log4jParents) { - parents.add(factory.getMarker(m.getName())); - } - return parents.iterator(); - } - - @Override - public boolean remove(final Marker marker) { - return marker != null ? this.marker.remove(MarkerManager.getMarker(marker.getName())) : false; - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java deleted file mode 100644 index cedfc06571e..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/Log4jMarkerFactory.java +++ /dev/null @@ -1,151 +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.logging.slf4j; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Iterator; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.MarkerManager; -import org.apache.logging.log4j.status.StatusLogger; -import org.slf4j.IMarkerFactory; -import org.slf4j.Marker; - -/** - * Log4j/SLF4J bridge to create SLF4J Markers based on name or based on existing SLF4J Markers. - */ -public class Log4jMarkerFactory implements IMarkerFactory { - - private static final Logger LOGGER = StatusLogger.getLogger(); - - private final ConcurrentMap markerMap = new ConcurrentHashMap<>(); - - /** - * Returns a Log4j Marker that is compatible with SLF4J. - * @param name The name of the Marker. - * @return A Marker. - */ - @Override - public Marker getMarker(final String name) { - if (name == null) { - throw new IllegalArgumentException("Marker name must not be null"); - } - final Marker marker = markerMap.get(name); - if (marker != null) { - return marker; - } - final org.apache.logging.log4j.Marker log4jMarker = MarkerManager.getMarker(name); - return addMarkerIfAbsent(name, log4jMarker); - } - - private Marker addMarkerIfAbsent(final String name, final org.apache.logging.log4j.Marker log4jMarker) { - final Marker marker = new Log4jMarker(this, log4jMarker); - final Marker existing = markerMap.putIfAbsent(name, marker); - return existing == null ? marker : existing; - } - - /** - * Returns a Log4j Marker converted from an existing custom SLF4J Marker. - * @param marker The SLF4J Marker to convert. - * @return A converted Log4j/SLF4J Marker. - * @since 2.1 - */ - public Marker getMarker(final Marker marker) { - if (marker == null) { - throw new IllegalArgumentException("Marker must not be null"); - } - final Marker m = markerMap.get(marker.getName()); - if (m != null) { - return m; - } - return addMarkerIfAbsent(marker.getName(), convertMarker(marker)); - } - - /** - * Gets the Log4j2 marker associated to this SLF4J marker or creates a new one. - * - * @param marker a SLF4J marker - * @return a Log4j2 marker - */ - org.apache.logging.log4j.Marker getLog4jMarker(final Marker marker) { - if (marker == null) { - return null; - } else if (marker instanceof Log4jMarker) { - return ((Log4jMarker) marker).getLog4jMarker(); - } else { - return ((Log4jMarker) getMarker(marker)).getLog4jMarker(); - } - } - - static org.apache.logging.log4j.Marker convertMarker(final Marker original) { - if (original == null) { - throw new IllegalArgumentException("Marker must not be null"); - } - return convertMarker(original, new ArrayList()); - } - - private static org.apache.logging.log4j.Marker convertMarker( - final Marker original, final Collection visited) { - final org.apache.logging.log4j.Marker marker = MarkerManager.getMarker(original.getName()); - if (original.hasReferences()) { - final Iterator it = original.iterator(); - while (it.hasNext()) { - final Marker next = it.next(); - if (visited.contains(next)) { - LOGGER.warn("Found a cycle in Marker [{}]. Cycle will be broken.", next.getName()); - } else { - visited.add(next); - marker.addParents(convertMarker(next, visited)); - } - } - } - return marker; - } - - /** - * Returns true if the Marker exists. - * @param name The Marker name. - * @return {@code true} if the Marker exists, {@code false} otherwise. - */ - @Override - public boolean exists(final String name) { - return markerMap.containsKey(name); - } - - /** - * Log4j does not support detached Markers. This method always returns false. - * @param name The Marker name. - * @return {@code false} - */ - @Override - public boolean detachMarker(final String name) { - return false; - } - - /** - * Log4j does not support detached Markers for performance reasons. The returned Marker is attached. - * @param name The Marker name. - * @return The named Marker (unmodified). - */ - @Override - public Marker getDetachedMarker(final String name) { - LOGGER.warn("Log4j does not support detached Markers. Returned Marker [{}] will be unchanged.", name); - return getMarker(name); - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/SLF4JLoggingException.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/SLF4JLoggingException.java deleted file mode 100644 index 57a65e1dfb5..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/SLF4JLoggingException.java +++ /dev/null @@ -1,41 +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.logging.slf4j; - -/** - * Exception thrown when the SLF4J adapter encounters a problem. - * - */ -public class SLF4JLoggingException extends RuntimeException { - - /** - * Generated serial version ID. - */ - private static final long serialVersionUID = -1618650972455089998L; - - public SLF4JLoggingException(final String msg) { - super(msg); - } - - public SLF4JLoggingException(final String msg, final Exception ex) { - super(msg, ex); - } - - public SLF4JLoggingException(final Exception ex) { - super(ex); - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/SLF4JServiceProvider.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/SLF4JServiceProvider.java deleted file mode 100644 index abfdcd1972f..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/SLF4JServiceProvider.java +++ /dev/null @@ -1,60 +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.logging.slf4j; - -import aQute.bnd.annotation.Resolution; -import aQute.bnd.annotation.spi.ServiceProvider; -import org.slf4j.ILoggerFactory; -import org.slf4j.IMarkerFactory; -import org.slf4j.spi.MDCAdapter; - -@ServiceProvider(value = org.slf4j.spi.SLF4JServiceProvider.class, resolution = Resolution.MANDATORY) -public class SLF4JServiceProvider implements org.slf4j.spi.SLF4JServiceProvider { - - public static final String REQUESTED_API_VERSION = "2.0.99"; - - private ILoggerFactory loggerFactory; - private Log4jMarkerFactory markerFactory; - private MDCAdapter mdcAdapter; - - @Override - public ILoggerFactory getLoggerFactory() { - return loggerFactory; - } - - @Override - public IMarkerFactory getMarkerFactory() { - return markerFactory; - } - - @Override - public MDCAdapter getMDCAdapter() { - return mdcAdapter; - } - - @Override - public String getRequestedApiVersion() { - return REQUESTED_API_VERSION; - } - - @Override - public void initialize() { - markerFactory = new Log4jMarkerFactory(); - loggerFactory = new Log4jLoggerFactory(markerFactory); - mdcAdapter = new Log4jMDCAdapter(); - } -} diff --git a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/package-info.java b/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/package-info.java deleted file mode 100644 index d847bc764bb..00000000000 --- a/log4j-slf4j2-impl/src/main/java/org/apache/logging/slf4j/package-info.java +++ /dev/null @@ -1,30 +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. - */ -/** - * SLF4J support. Note that this does indeed share the same package namespace as the one found in log4j-to-slf4j; - * this is intentional. The two JARs should not be used at the same time! Thus, in an OSGi environment - * where split packages are not allowed, this error is prevented due to both JARs sharing an exported package name. - */ -@Export -@Header(name = Constants.BUNDLE_ACTIVATIONPOLICY, value = Constants.ACTIVATION_LAZY) -@Version("3.0.0") -package org.apache.logging.slf4j; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.bundle.Header; -import org.osgi.annotation.versioning.Version; -import org.osgi.framework.Constants; diff --git a/log4j-slf4j2-impl/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider b/log4j-slf4j2-impl/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider deleted file mode 100644 index 1577f12daf0..00000000000 --- a/log4j-slf4j2-impl/src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider +++ /dev/null @@ -1 +0,0 @@ -org.apache.logging.slf4j.SLF4JServiceProvider \ No newline at end of file diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/other/pkg/LoggerContextAnchorTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/other/pkg/LoggerContextAnchorTest.java deleted file mode 100644 index 5732c205a4e..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/other/pkg/LoggerContextAnchorTest.java +++ /dev/null @@ -1,91 +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.logging.other.pkg; - -import static org.junit.Assert.assertEquals; - -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.status.StatusData; -import org.apache.logging.log4j.status.StatusListener; -import org.apache.logging.log4j.status.StatusLogger; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -/** - * Test LoggerContext lookups by verifying the anchor class representing calling code. - */ -public class LoggerContextAnchorTest { - private static final String PREFIX = "Log4jLoggerFactory.getContext() found anchor class "; - - @Test - public void testLoggerFactoryLookupClass() { - final String fqcn = getAnchorFqcn(() -> LoggerFactory.getLogger(LoggerContextAnchorTest.class)); - assertEquals(getClass().getName(), fqcn); - } - - @Test - public void testLoggerFactoryLookupString() { - final String fqcn = getAnchorFqcn(() -> LoggerFactory.getLogger("custom.logger")); - assertEquals(getClass().getName(), fqcn); - } - - @Test - public void testLoggerFactoryGetILoggerFactoryLookup() { - final String fqcn = - getAnchorFqcn(() -> LoggerFactory.getILoggerFactory().getLogger("custom.logger")); - assertEquals(getClass().getName(), fqcn); - } - - private static String getAnchorFqcn(final Runnable runnable) { - final List results = new CopyOnWriteArrayList<>(); - final StatusListener listener = new StatusListener() { - @Override - public void log(final StatusData data) { - final String formattedMessage = data.getMessage().getFormattedMessage(); - if (formattedMessage.startsWith(PREFIX)) { - results.add(formattedMessage.substring(PREFIX.length())); - } - } - - @Override - public Level getStatusLevel() { - return Level.TRACE; - } - - @Override - public void close() { - // nop - } - }; - final StatusLogger statusLogger = StatusLogger.getLogger(); - statusLogger.registerListener(listener); - try { - runnable.run(); - if (results.isEmpty()) { - throw new AssertionError("Failed to locate an anchor lookup status message"); - } - if (results.size() > 1) { - throw new AssertionError("Found multiple anchor lines: " + results); - } - return results.get(0); - } finally { - statusLogger.removeListener(listener); - } - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java deleted file mode 100644 index ca22e19fe49..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java +++ /dev/null @@ -1,90 +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.logging.slf4j; - -import static org.junit.Assert.assertEquals; - -import java.util.List; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.junit.ClassRule; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.spi.CallerBoundaryAware; -import org.slf4j.spi.LoggingEventBuilder; - -public class CallerInformationTest { - - // config from log4j-core test-jar - private static final String CONFIG = "log4j2-calling-class.xml"; - - @ClassRule - public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG); - - @Test - public void testClassLogger() throws Exception { - final ListAppender app = ctx.getListAppender("Class").clear(); - final Logger logger = LoggerFactory.getLogger("ClassLogger"); - logger.info("Ignored message contents."); - logger.warn("Verifying the caller class is still correct."); - logger.error("Hopefully nobody breaks me!"); - logger.atInfo().log("Ignored message contents."); - logger.atWarn().log("Verifying the caller class is still correct."); - logger.atError().log("Hopefully nobody breaks me!"); - final List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 6, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller class name.", this.getClass().getName(), message); - } - } - - @Test - public void testMethodLogger() throws Exception { - final ListAppender app = ctx.getListAppender("Method").clear(); - final Logger logger = LoggerFactory.getLogger("MethodLogger"); - logger.info("More messages."); - logger.warn("CATASTROPHE INCOMING!"); - logger.error("ZOMBIES!!!"); - logger.warn("brains~~~"); - logger.info("Itchy. Tasty."); - logger.atInfo().log("More messages."); - logger.atWarn().log("CATASTROPHE INCOMING!"); - logger.atError().log("ZOMBIES!!!"); - logger.atWarn().log("brains~~~"); - logger.atInfo().log("Itchy. Tasty."); - final List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 10, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller method name.", "testMethodLogger", message); - } - } - - @Test - public void testFqcnLogger() throws Exception { - final ListAppender app = ctx.getListAppender("Fqcn").clear(); - final Logger logger = LoggerFactory.getLogger("FqcnLogger"); - LoggingEventBuilder loggingEventBuilder = logger.atInfo(); - ((CallerBoundaryAware) loggingEventBuilder).setCallerBoundary("MyFqcn"); - loggingEventBuilder.log("A message"); - final List messages = app.getMessages(); - assertEquals("Incorrect number of messages.", 1, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect fqcn.", "MyFqcn", message); - } - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CustomFlatMarker.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CustomFlatMarker.java deleted file mode 100644 index 3d822097282..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/CustomFlatMarker.java +++ /dev/null @@ -1,74 +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.logging.slf4j; - -import java.util.Iterator; -import org.slf4j.Marker; - -/** - * Test Marker that may contain no reference/parent Markers. - * @see LOG4J2-793 - */ -public class CustomFlatMarker implements Marker { - private static final long serialVersionUID = -4115520883240247266L; - - private final String name; - - public CustomFlatMarker(final String name) { - this.name = name; - } - - @Override - public String getName() { - return name; - } - - @Override - public void add(final Marker reference) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean remove(final Marker reference) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean hasChildren() { - return hasReferences(); - } - - @Override - public boolean hasReferences() { - return false; - } - - @Override - public Iterator iterator() { - throw new UnsupportedOperationException(); - } - - @Override - public boolean contains(final Marker other) { - return false; - } - - @Override - public boolean contains(final String name) { - return false; - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java deleted file mode 100644 index 85d9d0dff4d..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4j1222Test.java +++ /dev/null @@ -1,53 +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.logging.slf4j; - -import static org.junit.Assert.assertTrue; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Tests logging during shutdown. - */ -public class Log4j1222Test { - - @Test - public void homepageRendersSuccessfully() { - System.setProperty("log4j.configurationFile", "log4j2-console.xml"); - Runtime.getRuntime().addShutdownHook(new ShutdownHook()); - } - - private static class ShutdownHook extends Thread { - - private static class Holder { - private static final Logger LOGGER = LoggerFactory.getLogger(Log4j1222Test.class); - } - - @Override - public void run() { - super.run(); - trigger(); - } - - private void trigger() { - Holder.LOGGER.info("Attempt to trigger"); - assertTrue("Logger is of type " + Holder.LOGGER.getClass().getName(), Holder.LOGGER instanceof Log4jLogger); - } - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4j2_1482_Slf4jTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4j2_1482_Slf4jTest.java deleted file mode 100644 index d8abaf65a46..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4j2_1482_Slf4jTest.java +++ /dev/null @@ -1,39 +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.logging.slf4j; - -import org.apache.logging.log4j.core.test.layout.Log4j2_1482_Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Tests https://issues.apache.org/jira/browse/LOG4J2-1482 - */ -public class Log4j2_1482_Slf4jTest extends Log4j2_1482_Test { - - @Override - protected void log(final int runNumber) { - if (runNumber == 2) { - // System.out.println("Set a breakpoint here."); - } - final Logger logger = LoggerFactory.getLogger("auditcsvfile"); - final int val1 = 9, val2 = 11, val3 = 12; - logger.info("Info Message!", val1, val2, val3); - logger.info("Info Message!", val1, val2, val3); - logger.info("Info Message!", val1, val2, val3); - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jEventBuilderTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jEventBuilderTest.java deleted file mode 100644 index 8441bb1c92f..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jEventBuilderTest.java +++ /dev/null @@ -1,65 +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.logging.slf4j; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.List; -import org.apache.logging.log4j.core.Appender; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextSource; -import org.apache.logging.log4j.core.test.junit.Named; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@LoggerContextSource("log4j2-config.xml") -public class Log4jEventBuilderTest { - - private final Logger logger; - private final ListAppender appender; - - public Log4jEventBuilderTest(@Named("List") final Appender appender) { - logger = LoggerFactory.getLogger("org.apache.test.Log4jEventBuilderTest"); - this.appender = (ListAppender) appender; - } - - @BeforeEach - public void setUp() { - appender.clear(); - } - - @Test - public void testKeyValuePairs() { - logger.atDebug().addKeyValue("testKeyValuePairs", "ok").log(); - final List events = appender.getEvents(); - assertThat(events).hasSize(1); - assertThat(events.get(0).getContextData().toMap()).containsEntry("testKeyValuePairs", "ok"); - } - - @Test - public void testArguments() { - logger.atDebug().setMessage("{}-{}").addArgument("a").addArgument("b").log(); - logger.atDebug().log("{}-{}", "a", "b"); - logger.atDebug().addArgument("a").log("{}-{}", "b"); - logger.atDebug().log("{}-{}", new Object[] {"a", "b"}); - assertThat(appender.getEvents()).hasSize(4).allMatch(event -> "a-b" - .equals(event.getMessage().getFormattedMessage())); - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jMDCAdapterTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jMDCAdapterTest.java deleted file mode 100644 index 0559608f467..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jMDCAdapterTest.java +++ /dev/null @@ -1,61 +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.logging.slf4j; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.ArrayDeque; -import java.util.Deque; -import java.util.stream.IntStream; -import java.util.stream.Stream; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; - -public class Log4jMDCAdapterTest { - - private static final Log4jMDCAdapter MDC_ADAPTER = new Log4jMDCAdapter(); - private static final String KEY = "Log4j2"; - - private static Deque createDeque(final int size) { - final Deque result = new ArrayDeque<>(size); - IntStream.range(0, size).mapToObj(Integer::toString).forEach(result::addLast); - return result; - } - - private static Deque popDeque(final String key) { - final Deque result = new ArrayDeque<>(); - String value; - while ((value = MDC_ADAPTER.popByKey(key)) != null) { - result.addLast(value); - } - return result; - } - - static Stream keys() { - return Stream.of(KEY, "", null); - } - - @ParameterizedTest - @MethodSource("keys") - public void testPushPopByKey(final String key) { - MDC_ADAPTER.clearDequeByKey(key); - final Deque expectedValues = createDeque(100); - expectedValues.descendingIterator().forEachRemaining(v -> MDC_ADAPTER.pushByKey(key, v)); - assertThat(MDC_ADAPTER.getCopyOfDequeByKey(key)).containsExactlyElementsOf(expectedValues); - assertThat(popDeque(key)).containsExactlyElementsOf(expectedValues); - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java deleted file mode 100644 index 837b392a698..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/Log4jMarkerTest.java +++ /dev/null @@ -1,46 +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.logging.slf4j; - -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -public class Log4jMarkerTest { - - private static Log4jMarkerFactory markerFactory; - - @BeforeClass - public static void startup() { - markerFactory = ((Log4jLoggerFactory) org.slf4j.LoggerFactory.getILoggerFactory()).getMarkerFactory(); - } - - @Test - public void testEquals() { - final Marker markerA = MarkerManager.getMarker(Log4jMarkerTest.class.getName() + "-A"); - final Marker markerB = MarkerManager.getMarker(Log4jMarkerTest.class.getName() + "-B"); - final Log4jMarker marker1 = new Log4jMarker(markerFactory, markerA); - final Log4jMarker marker2 = new Log4jMarker(markerFactory, markerA); - final Log4jMarker marker3 = new Log4jMarker(markerFactory, markerB); - Assert.assertEquals(marker1, marker2); - Assert.assertNotEquals(marker1, null); - Assert.assertNotEquals(null, marker1); - Assert.assertNotEquals(marker1, marker3); - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java deleted file mode 100644 index d77e1d4d342..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerContextTest.java +++ /dev/null @@ -1,43 +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.logging.slf4j; - -import static org.junit.Assert.assertTrue; - -import java.util.Set; -import org.apache.logging.log4j.core.LifeCycle; -import org.apache.logging.log4j.spi.LoggerContext; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -/** - * Tests cleanup of the LoggerContexts. - */ -public class LoggerContextTest { - - @Test - public void testCleanup() throws Exception { - final Log4jLoggerFactory factory = (Log4jLoggerFactory) LoggerFactory.getILoggerFactory(); - factory.getLogger("test"); - Set set = factory.getLoggerContexts(); - final LoggerContext ctx1 = set.toArray(LoggerContext.EMPTY_ARRAY)[0]; - assertTrue("LoggerContext is not enabled for shutdown", ctx1 instanceof LifeCycle); - ((LifeCycle) ctx1).stop(); - set = factory.getLoggerContexts(); - assertTrue("Expected no LoggerContexts", set.isEmpty()); - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java deleted file mode 100644 index c5780437a2c..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/LoggerTest.java +++ /dev/null @@ -1,206 +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.logging.slf4j; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.List; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.config.Configurator; -import org.apache.logging.log4j.core.test.appender.ListAppender; -import org.apache.logging.log4j.core.test.junit.LoggerContextRule; -import org.apache.logging.log4j.util.Strings; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.slf4j.MDC; -import org.slf4j.Marker; -import org.slf4j.spi.LocationAwareLogger; -import org.slf4j.spi.LoggingEventBuilder; - -/** - * - */ -public class LoggerTest { - - private static final String CONFIG = "log4j-test1.xml"; - - @ClassRule - public static LoggerContextRule ctx = new LoggerContextRule(CONFIG); - - Logger logger = LoggerFactory.getLogger("LoggerTest"); - - @Test - public void debug() { - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void debugNoParms() { - logger.debug("Debug message {}"); - verify("o.a.l.s.LoggerTest Debug message {} MDC{}" + Strings.LINE_SEPARATOR); - logger.debug("Debug message {}", (Object[]) null); - verify("o.a.l.s.LoggerTest Debug message {} MDC{}" + Strings.LINE_SEPARATOR); - ((LocationAwareLogger) logger) - .log(null, Log4jLogger.class.getName(), LocationAwareLogger.DEBUG_INT, "Debug message {}", null, null); - verify("o.a.l.s.LoggerTest Debug message {} MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void debugWithParms() { - logger.debug("Hello, {}", "World"); - verify("o.a.l.s.LoggerTest Hello, World MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void mdc() { - - MDC.put("TestYear", "2010"); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{TestYear=2010}" + Strings.LINE_SEPARATOR); - MDC.clear(); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void mdcStack() { - MDC.pushByKey("TestYear", "2010"); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{TestYear=2010}" + Strings.LINE_SEPARATOR); - MDC.pushByKey("TestYear", "2011"); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{TestYear=2011}" + Strings.LINE_SEPARATOR); - MDC.popByKey("TestYear"); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{TestYear=2010}" + Strings.LINE_SEPARATOR); - MDC.clear(); - logger.debug("Debug message"); - verify("o.a.l.s.LoggerTest Debug message MDC{}" + Strings.LINE_SEPARATOR); - } - - /** - * @see LOG4J2-793 - */ - @Test - public void supportsCustomSLF4JMarkers() { - final Marker marker = new CustomFlatMarker("TEST"); - logger.debug(marker, "Test"); - verify("o.a.l.s.LoggerTest Test MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void testRootLogger() { - final Logger l = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); - assertNotNull("No Root Logger", l); - assertEquals(Logger.ROOT_LOGGER_NAME, l.getName()); - } - - @Test - public void doubleSubst() { - logger.debug("Hello, {}", "Log4j {}"); - verify("o.a.l.s.LoggerTest Hello, Log4j {} MDC{}" + Strings.LINE_SEPARATOR); - } - - @Test - public void testThrowable() { - final Throwable expected = new RuntimeException(); - logger.debug("Hello {}", expected); - verifyThrowable(expected); - logger.debug("Hello {}", (Object) expected); - verifyThrowable(null); - logger.debug("Hello", expected); - verifyThrowable(expected); - logger.debug("Hello {}! {}", "World!", expected); - verifyThrowable(null); - logger.debug("Hello {}!", "World!", expected); - verifyThrowable(expected); - final LocationAwareLogger lal = (LocationAwareLogger) logger; - lal.log(null, LoggerTest.class.getName(), LocationAwareLogger.DEBUG_INT, "Hello {}", null, expected); - verifyThrowable(expected); - lal.log( - null, - LoggerTest.class.getName(), - LocationAwareLogger.DEBUG_INT, - "Hello {}", - new Object[] {expected}, - null); - verifyThrowable(null); - lal.log( - null, - LoggerTest.class.getName(), - LocationAwareLogger.DEBUG_INT, - "Hello {}", - new Object[] {"World!", expected}, - null); - verifyThrowable(expected); - } - - @Test - public void testLazyLoggingEventBuilder() { - final ListAppender appender = ctx.getListAppender("UnformattedList"); - final Level oldLevel = ctx.getRootLogger().getLevel(); - try { - Configurator.setRootLevel(Level.ERROR); - final LoggingEventBuilder builder = logger.makeLoggingEventBuilder(org.slf4j.event.Level.DEBUG); - Configurator.setRootLevel(Level.DEBUG); - builder.log(); - assertThat(appender.getEvents()).hasSize(1).map(LogEvent::getLevel).containsExactly(Level.DEBUG); - } finally { - Configurator.setRootLevel(oldLevel); - } - } - - private ListAppender getAppenderByName(final String name) { - final ListAppender listApp = ctx.getListAppender(name); - assertNotNull("Missing Appender", listApp); - return listApp; - } - - private void verify(final String expected) { - final ListAppender listApp = getAppenderByName("List"); - final List events = listApp.getMessages(); - assertEquals("Incorrect number of messages. Expected 1 Actual " + events.size(), 1, events.size()); - final String actual = events.get(0); - assertEquals("Incorrect message. Expected " + expected + ". Actual " + actual, expected, actual); - listApp.clear(); - } - - private void verifyThrowable(final Throwable expected) { - final ListAppender listApp = getAppenderByName("UnformattedList"); - final List events = listApp.getEvents(); - assertEquals("Incorrect number of messages", 1, events.size()); - final LogEvent actual = events.get(0); - assertEquals("Incorrect throwable.", expected, actual.getThrown()); - listApp.clear(); - } - - @Before - @After - public void cleanup() { - MDC.clear(); - ctx.getListAppender("List").clear(); - ctx.getListAppender("UnformattedList").clear(); - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java deleted file mode 100644 index 0db3e30393e..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/MarkerTest.java +++ /dev/null @@ -1,197 +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.logging.slf4j; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * - */ -public class MarkerTest { - - private static final String CHILD_MAKER_NAME = MarkerTest.class.getSimpleName() + "-TEST"; - private static final String PARENT_MARKER_NAME = MarkerTest.class.getSimpleName() + "-PARENT"; - private static Log4jMarkerFactory markerFactory; - - @BeforeClass - public static void startup() { - markerFactory = ((Log4jLoggerFactory) org.slf4j.LoggerFactory.getILoggerFactory()).getMarkerFactory(); - } - - @Before - @After - public void clearMarkers() { - MarkerManager.clear(); - } - - @Test - public void testAddMarker() { - final String childMakerName = CHILD_MAKER_NAME + "-AM"; - final String parentMarkerName = PARENT_MARKER_NAME + "-AM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMakerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMarkerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMarkerName); - final Marker log4jMarker = MarkerManager.getMarker(childMakerName); - - assertTrue("Incorrect Marker class", slf4jMarker instanceof Log4jMarker); - assertTrue( - String.format( - "%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j", - childMakerName, parentMarkerName, log4jMarker, log4jParent), - log4jMarker.isInstanceOf(log4jParent)); - assertTrue( - String.format( - "%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J", - childMakerName, parentMarkerName, slf4jMarker, slf4jParent), - slf4jMarker.contains(slf4jParent)); - } - - @Test - public void testAddNullMarker() { - final String childMarkerName = CHILD_MAKER_NAME + "-ANM"; - final String parentMakerName = PARENT_MARKER_NAME + "-ANM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final org.slf4j.Marker nullMarker = null; - try { - log4jSlf4jParent.add(nullMarker); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - try { - log4jSlf4jMarker.add(nullMarker); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - } - - @Test - public void testAddSameMarker() { - final String childMarkerName = CHILD_MAKER_NAME + "-ASM"; - final String parentMakerName = PARENT_MARKER_NAME + "-ASM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - assertTrue( - String.format( - "%s (log4jMarker=%s) is not an instance of %s (log4jParent=%s) in Log4j", - childMarkerName, parentMakerName, log4jMarker, log4jParent), - log4jMarker.isInstanceOf(log4jParent)); - assertTrue( - String.format( - "%s (slf4jMarker=%s) is not an instance of %s (log4jParent=%s) in SLF4J", - childMarkerName, parentMakerName, slf4jMarker, slf4jParent), - slf4jMarker.contains(slf4jParent)); - } - - @Test - public void testEquals() { - final String childMarkerName = CHILD_MAKER_NAME + "-ASM"; - final String parentMakerName = PARENT_MARKER_NAME + "-ASM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jMarker2 = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Marker log4jMarker2 = MarkerManager.getMarker(childMarkerName); - assertEquals(log4jMarker, log4jMarker2); - assertEquals(slf4jMarker, slf4jMarker2); - assertNotEquals(log4jParent, log4jMarker); - assertNotEquals(slf4jParent, slf4jMarker); - } - - @Test - public void testContainsNullMarker() { - final String childMarkerName = CHILD_MAKER_NAME + "-CM"; - final String parentMakerName = PARENT_MARKER_NAME + "-CM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final org.slf4j.Marker nullMarker = null; - try { - Assert.assertFalse(log4jSlf4jParent.contains(nullMarker)); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - try { - Assert.assertFalse(log4jSlf4jMarker.contains(nullMarker)); - fail("Expected " + IllegalArgumentException.class.getName()); - } catch (final IllegalArgumentException e) { - // expected - } - } - - @Test - public void testContainsNullString() { - final String childMarkerName = CHILD_MAKER_NAME + "-CS"; - final String parentMakerName = PARENT_MARKER_NAME + "-CS"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMarkerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMarkerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final String nullStr = null; - Assert.assertFalse(log4jSlf4jParent.contains(nullStr)); - Assert.assertFalse(log4jSlf4jMarker.contains(nullStr)); - } - - @Test - public void testRemoveNullMarker() { - final String childMakerName = CHILD_MAKER_NAME + "-CM"; - final String parentMakerName = PARENT_MARKER_NAME + "-CM"; - final org.slf4j.Marker slf4jMarker = org.slf4j.MarkerFactory.getMarker(childMakerName); - final org.slf4j.Marker slf4jParent = org.slf4j.MarkerFactory.getMarker(parentMakerName); - slf4jMarker.add(slf4jParent); - final Marker log4jParent = MarkerManager.getMarker(parentMakerName); - final Marker log4jMarker = MarkerManager.getMarker(childMakerName); - final Log4jMarker log4jSlf4jParent = new Log4jMarker(markerFactory, log4jParent); - final Log4jMarker log4jSlf4jMarker = new Log4jMarker(markerFactory, log4jMarker); - final org.slf4j.Marker nullMarker = null; - Assert.assertFalse(log4jSlf4jParent.remove(nullMarker)); - Assert.assertFalse(log4jSlf4jMarker.remove(nullMarker)); - } -} diff --git a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/OverflowTest.java b/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/OverflowTest.java deleted file mode 100644 index a0e5b0001a2..00000000000 --- a/log4j-slf4j2-impl/src/test/java/org/apache/logging/slf4j/OverflowTest.java +++ /dev/null @@ -1,41 +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.logging.slf4j; - -import static org.junit.Assert.fail; - -import org.apache.logging.log4j.LoggingException; -import org.junit.Test; -import org.slf4j.LoggerFactory; - -/** - * Tests StackOverflow when slf4j-impl and to-slf4j are both present. - */ -public class OverflowTest { - - @Test - public void log() { - try { - LoggerFactory.getLogger(OverflowTest.class); - fail("Failed to detect inclusion of log4j-to-slf4j"); - } catch (LoggingException ex) { - // Expected exception. - } catch (StackOverflowError error) { - fail("Failed to detect inclusion of log4j-to-slf4j, caught StackOverflowError"); - } - } -} diff --git a/log4j-slf4j2-impl/src/test/resources/log4j-test1.xml b/log4j-slf4j2-impl/src/test/resources/log4j-test1.xml deleted file mode 100644 index 30126e627a4..00000000000 --- a/log4j-slf4j2-impl/src/test/resources/log4j-test1.xml +++ /dev/null @@ -1,51 +0,0 @@ - - - - - target/test.log - - - - - - - - - - %d %p %C{1.} [%t] %m%n - - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-slf4j2-impl/src/test/resources/log4j2-1482.xml b/log4j-slf4j2-impl/src/test/resources/log4j2-1482.xml deleted file mode 100644 index af7af4a622c..00000000000 --- a/log4j-slf4j2-impl/src/test/resources/log4j2-1482.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - - - target/log4j2-1482 - audit - param1,param2,param3${sys:line.separator} - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-slf4j2-impl/src/test/resources/log4j2-config.xml b/log4j-slf4j2-impl/src/test/resources/log4j2-config.xml deleted file mode 100644 index d247bd15cd6..00000000000 --- a/log4j-slf4j2-impl/src/test/resources/log4j2-config.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/log4j-spring-cloud-config-client/pom.xml b/log4j-spring-cloud-config-client/pom.xml index afdd22931d5..2b7d5667a08 100644 --- a/log4j-spring-cloud-config-client/pom.xml +++ b/log4j-spring-cloud-config-client/pom.xml @@ -35,6 +35,7 @@ 3.3.5 + 1.3.4 4.1.4 6.1.14 @@ -58,6 +59,13 @@ + + + commons-logging + commons-logging + ${commons-logging.version} + + org.springframework spring-framework-bom diff --git a/log4j-to-jul/pom.xml b/log4j-to-jul/pom.xml deleted file mode 100644 index cf0832e2aa5..00000000000 --- a/log4j-to-jul/pom.xml +++ /dev/null @@ -1,79 +0,0 @@ - - - - 4.0.0 - - org.apache.logging.log4j - log4j - ${revision} - ../log4j-parent - - log4j-to-jul - jar - Apache Log4j to JUL Bridge - The Apache Log4j binding between Log4j 2 API and java.util.logging (JUL). - 2022 - - - - - org.jspecify.*;resolution:=optional - - - - org.jspecify;transitive=false - - - - - - org.jspecify - jspecify - provided - - - org.osgi - org.osgi.framework - provided - - - org.apache.logging.log4j - log4j-api - - - org.assertj - assertj-core - test - - - com.google.guava - guava-testlib - test - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.vintage - junit-vintage-engine - test - - - diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/Activator.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/Activator.java deleted file mode 100644 index 11c7c1456d1..00000000000 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/Activator.java +++ /dev/null @@ -1,30 +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.logging.log4j.tojul; - -import org.apache.logging.log4j.util.ProviderActivator; -import org.osgi.annotation.bundle.Header; -import org.osgi.framework.Constants; - -@Header(name = Constants.BUNDLE_ACTIVATOR, value = "${@class}") -@Header(name = Constants.BUNDLE_ACTIVATIONPOLICY, value = Constants.ACTIVATION_LAZY) -public class Activator extends ProviderActivator { - - public Activator() { - super(new JULProvider()); - } -} diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java deleted file mode 100644 index 11081071574..00000000000 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLogger.java +++ /dev/null @@ -1,331 +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.logging.log4j.tojul; - -import static java.util.Objects.requireNonNull; - -import java.util.logging.Logger; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.spi.AbstractLogger; - -/** - * Implementation of {@link org.apache.logging.log4j.Logger} that's backed by a {@link Logger}. - * - * This implementation currently ignores {@link Marker}. - * - * @author Michael Vorburger.ch for Google - */ -final class JULLogger extends AbstractLogger { - private static final long serialVersionUID = 1L; - - private final Logger logger; - - // This implementation is inspired by org.apache.logging.slf4j.SLF4JLogger - - public JULLogger(final String name, final MessageFactory messageFactory, final Logger logger) { - super(name, messageFactory); - this.logger = requireNonNull(logger, "logger"); - } - - public JULLogger(final String name, final Logger logger) { - super(name); - this.logger = requireNonNull(logger, "logger"); - } - - public Logger getWrappedLogger() { - return logger; - } - - @Override - public void logMessage( - final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) { - final java.util.logging.Level julLevel = convertLevel(level); - if (!logger.isLoggable(julLevel)) { - return; - } - final LazyLog4jLogRecord record = - new LazyLog4jLogRecord(fqcn, julLevel, message.getFormattedMessage()); // NOT getFormat() - // NOT record.setParameters(message.getParameters()); BECAUSE getFormattedMessage() NOT getFormat() - record.setLoggerName(getName()); - record.setThrown(t == null ? message.getThrowable() : t); - logger.log(record); - } - - // Convert Level in Log4j scale to JUL scale. - // See getLevel() for the mapping. Note that JUL's FINEST & CONFIG are never returned because Log4j has no such - // levels, and - // that Log4j's FATAL is simply mapped to JUL's SEVERE as is Log4j's ERROR because JUL does not distinguish between - // ERROR and FATAL. - private java.util.logging.Level convertLevel(final Level level) { - switch (level.getStandardLevel()) { - // Test in logical order of likely frequency of use - // Must be kept in sync with #getLevel() - case ALL: - return java.util.logging.Level.ALL; - case TRACE: - return java.util.logging.Level.FINER; - case DEBUG: - return java.util.logging.Level.FINE; - case INFO: - return java.util.logging.Level.INFO; - case WARN: - return java.util.logging.Level.WARNING; - case ERROR: - return java.util.logging.Level.SEVERE; - case FATAL: - return java.util.logging.Level.SEVERE; - case OFF: - return java.util.logging.Level.OFF; - default: - // This is tempting: throw new IllegalStateException("Impossible Log4j Level encountered: " + - // level.intLevel()); - // But it's not a great idea, security wise. If an attacker *SOMEHOW* managed to create a Log4j Level - // instance - // with an unexpected level (through JVM de-serialization, despite readResolve() { return - // Level.valueOf(this.name); }, - // or whatever other means), then we would blow up in a very unexpected place and way. Let us therefore - // instead just - // return SEVERE for unexpected values, because that's more likely to be noticed than a FINER. - // Greetings, Michael Vorburger.ch , for Google, on 2021.12.24. - return java.util.logging.Level.SEVERE; - } - } - - /** - * Level in Log4j scale. - * JUL Levels are mapped as follows: - *
    - *
  • OFF => OFF - *
  • SEVERE => ERROR - *
  • WARNING => WARN - *
  • INFO => INFO - *
  • CONFIG => INFO - *
  • FINE => DEBUG - *
  • FINER => TRACE (as in https://github.com/apache/logging-log4j2/blob/a58a06bf2365165ac5abdde931bb4ecd1adf0b3c/log4j-jul/src/main/java/org/apache/logging/log4j/jul/DefaultLevelConverter.java#L55-L75) - *
  • FINEST => TRACE - *
  • ALL => ALL - *
- * - * Numeric JUL Levels that don't match the known levels are matched to the closest one. - * For example, anything between OFF (Integer.MAX_VALUE) and SEVERE (1000) is returned as a Log4j FATAL. - */ - @Override - public Level getLevel() { - final int julLevel = getEffectiveJULLevel().intValue(); - // Test in logical order of likely frequency of use - // Must be kept in sync with #convertLevel() - if (julLevel == java.util.logging.Level.ALL.intValue()) { - return Level.ALL; - } - if (julLevel <= java.util.logging.Level.FINER.intValue()) { - return Level.TRACE; - } - if (julLevel <= java.util.logging.Level.FINE.intValue()) { // includes FINER - return Level.DEBUG; - } - if (julLevel <= java.util.logging.Level.INFO.intValue()) { // includes CONFIG - return Level.INFO; - } - if (julLevel <= java.util.logging.Level.WARNING.intValue()) { - return Level.WARN; - } - if (julLevel <= java.util.logging.Level.SEVERE.intValue()) { - return Level.ERROR; - } - return Level.OFF; - } - - private java.util.logging.Level getEffectiveJULLevel() { - Logger current = logger; - while (current.getLevel() == null && current.getParent() != null) { - current = current.getParent(); - } - if (current.getLevel() != null) { - return current.getLevel(); - } - // This is a safety fallback that is typically never reached, because usually the root Logger.getLogger("") has - // a Level. - // Since JDK 8 the LogManager$RootLogger does not have a default level, just a default effective level of INFO. - return java.util.logging.Level.INFO; - } - - private boolean isEnabledFor(final Level level, final Marker marker) { - // E.g. we're logging WARN and more, so getLevel() is 300, if we're asked if we're - // enabled for level ERROR which is 200, isLessSpecificThan() tests for >= so return true. - return getLevel().isLessSpecificThan(level); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final Message data, final Throwable t) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final CharSequence data, final Throwable t) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final Object data, final Throwable t) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String data) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String data, final Object... p1) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, final Marker marker, final String message, final Object p0, final Object p1) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6, - final Object p7) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6, - final Object p7, - final Object p8) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6, - final Object p7, - final Object p8, - final Object p9) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String data, final Throwable t) { - return isEnabledFor(level, marker); - } -} diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContext.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContext.java deleted file mode 100644 index 7d2c2351c6b..00000000000 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContext.java +++ /dev/null @@ -1,72 +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.logging.log4j.tojul; - -import java.util.logging.Logger; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.spi.LoggerRegistry; - -/** - * Implementation of Log4j {@link LoggerContext} SPI. - * This is a factory to produce {@link JULLogger} instances. - * - * @author Michael Vorburger.ch for Google - */ -class JULLoggerContext implements LoggerContext { - private final LoggerRegistry loggerRegistry = new LoggerRegistry<>(); - - // This implementation is strongly inspired by org.apache.logging.slf4j.SLF4JLoggerContext - - @Override - public Object getExternalContext() { - return null; - } - - @Override - public ExtendedLogger getLogger(final String name) { - if (!loggerRegistry.hasLogger(name)) { - loggerRegistry.putIfAbsent(name, null, new JULLogger(name, Logger.getLogger(name))); - } - return loggerRegistry.getLogger(name); - } - - @Override - public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) { - if (!loggerRegistry.hasLogger(name, messageFactory)) { - loggerRegistry.putIfAbsent( - name, messageFactory, new JULLogger(name, messageFactory, Logger.getLogger(name))); - } - return loggerRegistry.getLogger(name, messageFactory); - } - - @Override - public boolean hasLogger(final String name) { - return loggerRegistry.hasLogger(name); - } - - @Override - public boolean hasLogger(final String name, final MessageFactory messageFactory) { - return loggerRegistry.hasLogger(name, messageFactory); - } - - @Override - public boolean hasLogger(final String name, final Class messageFactoryClass) { - return loggerRegistry.hasLogger(name, messageFactoryClass); - } -} diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContextFactory.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContextFactory.java deleted file mode 100644 index f241e075ae4..00000000000 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULLoggerContextFactory.java +++ /dev/null @@ -1,76 +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.logging.log4j.tojul; - -import java.net.URI; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.spi.LoggerContextFactory; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.LoaderUtil; - -/** - * Implementation of Log4j {@link LoggerContextFactory} SPI. - * This is a factory to produce the (one and only) {@link JULLoggerContext} instance. - * - * @author Michael Vorburger.ch for Google - */ -public class JULLoggerContextFactory implements LoggerContextFactory { - private static final StatusLogger LOGGER = StatusLogger.getLogger(); - private static final LoggerContext context = new JULLoggerContext(); - - // This implementation is strongly inspired by org.apache.logging.slf4j.SLF4JLoggerContextFactory - - public JULLoggerContextFactory() { - boolean misconfigured = false; - try { - LoaderUtil.loadClass("org.apache.logging.log4j.jul.LogManager"); - misconfigured = true; - } catch (final ClassNotFoundException classNotFoundIsGood) { - LOGGER.debug("org.apache.logging.log4j.jul.LogManager is not on classpath. Good!"); - } - if (misconfigured) { - throw new IllegalStateException("log4j-jul JAR is mutually exclusive with the log4j-to-jul JAR" - + "(the first routes calls from Log4j to JUL, the second from Log4j to JUL)"); - } - } - - @Override - public LoggerContext getContext( - final String fqcn, final ClassLoader loader, final Object externalContext, final boolean currentContext) { - return context; - } - - @Override - public LoggerContext getContext( - final String fqcn, - final ClassLoader loader, - final Object externalContext, - final boolean currentContext, - final URI configLocation, - final String name) { - return context; - } - - @Override - public void removeContext(final LoggerContext ignored) {} - - @Override - public boolean isClassLoaderDependent() { - // context is always used - return false; - } -} diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULProvider.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULProvider.java deleted file mode 100644 index 7497e484f85..00000000000 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/JULProvider.java +++ /dev/null @@ -1,51 +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.logging.log4j.tojul; - -import aQute.bnd.annotation.Resolution; -import aQute.bnd.annotation.spi.ServiceProvider; -import org.apache.logging.log4j.spi.LoggerContextFactory; -import org.apache.logging.log4j.spi.NoOpThreadContextMap; -import org.apache.logging.log4j.spi.Provider; -import org.apache.logging.log4j.spi.ThreadContextMap; -import org.jspecify.annotations.NullMarked; - -/** - * Bind the Log4j API to JUL. - * - * @author Michael Vorburger.ch for Google - */ -@NullMarked -@ServiceProvider(value = Provider.class, resolution = Resolution.OPTIONAL) -public class JULProvider extends Provider { - private static final LoggerContextFactory CONTEXT_FACTORY = new JULLoggerContextFactory(); - - public JULProvider() { - super(20, CURRENT_VERSION, JULLoggerContextFactory.class, NoOpThreadContextMap.class); - } - - @Override - public LoggerContextFactory getLoggerContextFactory() { - return CONTEXT_FACTORY; - } - - @Override - public ThreadContextMap getThreadContextMapInstance() { - // JUL does not provide an MDC implementation - return NoOpThreadContextMap.INSTANCE; - } -} diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/LazyLog4jLogRecord.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/LazyLog4jLogRecord.java deleted file mode 100644 index 014aa8c4fdc..00000000000 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/LazyLog4jLogRecord.java +++ /dev/null @@ -1,70 +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.logging.log4j.tojul; - -import java.util.logging.Level; -import java.util.logging.LogRecord; -import org.apache.logging.log4j.util.StackLocatorUtil; - -/** - * Extension of {@link java.util.logging.LogRecord} with lazy get source related methods based on Log4j's {@link StackLocatorUtil#calcLocation(String)}. - */ -final class LazyLog4jLogRecord extends LogRecord { - - private static final long serialVersionUID = 6798134264543826471L; - - // parent class LogRecord already has a needToInferCaller but it's private - private transient boolean inferCaller = true; - - private final String fqcn; - - LazyLog4jLogRecord(final String fqcn, final Level level, final String msg) { - super(level, msg); - this.fqcn = fqcn; - } - - @Override - public String getSourceClassName() { - if (inferCaller) { - inferCaller(); - } - return super.getSourceClassName(); - } - - @Override - public String getSourceMethodName() { - if (inferCaller) { - inferCaller(); - } - return super.getSourceMethodName(); - } - - private void inferCaller() { - StackTraceElement location = null; - if (fqcn != null) { - location = StackLocatorUtil.calcLocation(fqcn); - } - if (location != null) { - setSourceClassName(location.getClassName()); - setSourceMethodName(location.getMethodName()); - } else { - setSourceClassName(null); - setSourceMethodName(null); - } - inferCaller = false; - } -} diff --git a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/package-info.java b/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/package-info.java deleted file mode 100644 index aa117c7995c..00000000000 --- a/log4j-to-jul/src/main/java/org/apache/logging/log4j/tojul/package-info.java +++ /dev/null @@ -1,28 +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. - */ -/** - * Java JDK java.util.logging (JUL) bridge. - * This sends all Log4j logs to JUL (not the other way around, there is another module for the opposite direction). - * - * @author Michael Vorburger.ch for Google - */ -@Export -@Version("2.24.0") -package org.apache.logging.log4j.tojul; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/log4j-to-jul/src/main/resources/META-INF/services/org.apache.logging.log4j.spi.Provider b/log4j-to-jul/src/main/resources/META-INF/services/org.apache.logging.log4j.spi.Provider deleted file mode 100644 index 2ac36b5b8bd..00000000000 --- a/log4j-to-jul/src/main/resources/META-INF/services/org.apache.logging.log4j.spi.Provider +++ /dev/null @@ -1,18 +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. - -org.apache.logging.log4j.tojul.JULProvider diff --git a/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/JULLoggerTest.java b/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/JULLoggerTest.java deleted file mode 100644 index 9f70a01b28b..00000000000 --- a/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/JULLoggerTest.java +++ /dev/null @@ -1,34 +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.logging.log4j.tojul; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.apache.logging.log4j.Level; -import org.junit.jupiter.api.Test; - -public class JULLoggerTest { - - @Test - public void testNotNullEffectiveLevel() { - // Emulates the root logger found in Tomcat, with a null level - // See: https://bz.apache.org/bugzilla/show_bug.cgi?id=66184 - final java.util.logging.Logger julLogger = new java.util.logging.Logger("", null) {}; - final JULLogger logger = new JULLogger("", julLogger); - assertEquals(Level.INFO, logger.getLevel()); - } -} diff --git a/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/LoggerTest.java b/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/LoggerTest.java deleted file mode 100644 index c2d65199aae..00000000000 --- a/log4j-to-jul/src/test/java/org/apache/logging/log4j/tojul/LoggerTest.java +++ /dev/null @@ -1,270 +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.logging.log4j.tojul; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.google.common.testing.TestLogHandler; -import java.io.IOException; -import java.util.List; -import java.util.logging.Level; -import java.util.logging.LogRecord; -import org.apache.logging.log4j.LogManager; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -public class LoggerTest { - - // Save levels so that we can reset them @After clearLogs() - private static final java.util.logging.Logger globalLogger = java.util.logging.Logger.getGlobal(); - private static final java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger(""); - private static final Level globalLevel = globalLogger.getLevel(); - private static final Level rootLevel = rootLogger.getLevel(); - - private org.apache.logging.log4j.Logger log4jLogger; - private java.util.logging.Logger julLogger; - private Level julLoggerDefaultLevel; - - // https://javadoc.io/doc/com.google.guava/guava-testlib/latest/com/google/common/testing/TestLogHandler.html - private TestLogHandler handler; - - @BeforeEach - public void setupLogCapture() { - handler = new TestLogHandler(); - // Beware, the order here should not be changed! - // Let the bridge do whatever it does BEFORE we create a JUL Logger (which SHOULD be the same) - log4jLogger = LogManager.getLogger(getClass()); - assertThat(log4jLogger).isInstanceOf(JULLogger.class); - julLogger = java.util.logging.Logger.getLogger(getClass().getName()); - assertThat(julLogger).isSameAs(((JULLogger) log4jLogger).getWrappedLogger()); - julLogger.addHandler(handler); - - julLoggerDefaultLevel = julLogger.getLevel(); - - // Check that there is no configuration file which invalidates our assumption that the root logger is the parent - // of our julLogger - assertThat(julLogger.getParent()).isEqualTo(rootLogger); - } - - @AfterEach - public void clearLogs() { - julLogger.removeHandler(handler); - // Reset all Levels what any tests set anymore - julLogger.setLevel(julLoggerDefaultLevel); - rootLogger.setLevel(rootLevel); - globalLogger.setLevel(globalLevel); - } - - @Test - public void infoAtInfo() { - julLogger.setLevel(Level.INFO); - log4jLogger.info("hello, world"); - - final List logs = handler.getStoredLogRecords(); - assertThat(logs).hasSize(1); - final LogRecord log1 = logs.get(0); - assertThat(log1.getLoggerName()).isEqualTo(getClass().getName()); - assertThat(log1.getLevel()).isEqualTo(java.util.logging.Level.INFO); - assertThat(log1.getMessage()).isEqualTo("hello, world"); - assertThat(log1.getParameters()).isNull(); - assertThat(log1.getThrown()).isNull(); - assertThat(log1.getSourceClassName()).isEqualTo(getClass().getName()); - assertThat(log1.getSourceMethodName()).isEqualTo("infoAtInfo"); - } - - @Test - public void infoAtInfoWithParameters() { - julLogger.setLevel(Level.INFO); - log4jLogger.info("hello, {}", "world"); - - final List logs = handler.getStoredLogRecords(); - assertThat(logs).hasSize(1); - final LogRecord log1 = logs.get(0); - assertThat(log1.getMessage()).isEqualTo("hello, world"); - assertThat(log1.getParameters()).isNull(); - assertThat(log1.getThrown()).isNull(); - } - - @Test - public void errorAtSevereWithException() { - julLogger.setLevel(Level.SEVERE); - log4jLogger.error("hello, {}", "world", new IOException("Testing, testing")); - - final List logs = handler.getStoredLogRecords(); - assertThat(logs).hasSize(1); - final LogRecord log1 = logs.get(0); - assertThat(log1.getMessage()).isEqualTo("hello, world"); - assertThat(log1.getParameters()).isNull(); - assertThat(log1.getThrown()).isInstanceOf(IOException.class); - } - - @Test - public void infoAtInfoWithLogBuilder() { - julLogger.setLevel(Level.INFO); - log4jLogger.atInfo().log("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - } - - @Test - public void infoAtInfoOnParent() { - julLogger.getParent().setLevel(Level.INFO); - log4jLogger.info("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - } - - @Test - public void infoWithoutAnyLevel() { - // We're not setting any level. - log4jLogger.info("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - } - - @Test - public void debugAtInfo() { - julLogger.setLevel(Level.INFO); - log4jLogger.debug("hello, world"); - assertThat(handler.getStoredLogRecords()).isEmpty(); - } - - @Test - public void debugAtFiner() { - julLogger.setLevel(Level.FINER); - log4jLogger.debug("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - } - - @Test - public void traceAtFine() { - julLogger.setLevel(Level.FINE); - log4jLogger.trace("hello, world"); - assertThat(handler.getStoredLogRecords()).isEmpty(); - } - - @Test - public void traceAtAllOnParent() { - julLogger.getParent().setLevel(Level.ALL); - log4jLogger.trace("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - } - - @Test - public void fatalAtOff() { - julLogger.getParent().setLevel(Level.OFF); - log4jLogger.fatal("hello, world"); - assertThat(handler.getStoredLogRecords()).isEmpty(); - } - - @Test - public void fatalAtSevere() { - julLogger.getParent().setLevel(Level.SEVERE); - log4jLogger.atFatal().log("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - } - - @Test - public void warnAtFatal() { - julLogger.getParent().setLevel(Level.SEVERE); - log4jLogger.atWarn().log("hello, world"); - assertThat(handler.getStoredLogRecords()).isEmpty(); - } - - @Test - public void customLevelJustUnderWarning() { - julLogger.getParent().setLevel(new CustomLevel("Just under Warning", Level.WARNING.intValue() - 1)); - - log4jLogger.info("hello, world"); - assertThat(handler.getStoredLogRecords()).isEmpty(); - - log4jLogger.warn("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - - log4jLogger.error("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(2); - } - - @Test - public void customLevelJustAboveWarning() { - julLogger.getParent().setLevel(new CustomLevel("Just above Warning", Level.WARNING.intValue() + 1)); - - log4jLogger.info("hello, world"); - assertThat(handler.getStoredLogRecords()).isEmpty(); - - log4jLogger.warn("hello, world"); - assertThat(handler.getStoredLogRecords()).isEmpty(); - - log4jLogger.error("hello, world"); - assertThat(handler.getStoredLogRecords()).hasSize(1); - } - - @SuppressWarnings("serial") - private static class CustomLevel extends Level { - CustomLevel(final String name, final int value) { - super(name, value); - } - } - - /** - * Test that the {@link LogRecord#getSourceClassName()}, which we already tested above in infoAtInfo() - * also works as expected if the logging happened in a class that we have called (indirect), not in the test method itself. - */ - @Test - public void indirectSource() { - java.util.logging.Logger.getLogger(Another.class.getName()).setLevel(Level.INFO); - new Another(handler); - final List logs = handler.getStoredLogRecords(); - assertThat(logs).hasSize(1); - final LogRecord log1 = logs.get(0); - assertThat(log1.getSourceClassName()).isEqualTo(Another.class.getName()); - assertThat(log1.getSourceMethodName()).isEqualTo(""); - } - - static class Another { - org.apache.logging.log4j.Logger anotherLog4jLogger = LogManager.getLogger(getClass()); - java.util.logging.Logger anotherJULLogger = - java.util.logging.Logger.getLogger(getClass().getName()); - - Another(final TestLogHandler handler) { - anotherJULLogger.addHandler(handler); - anotherLog4jLogger.info("hello, another world"); - } - } - - @Test - public void placeholdersInFormat() { - julLogger.setLevel(Level.INFO); - log4jLogger.info("hello, {0} {}", "world"); - - final List logs = handler.getStoredLogRecords(); - assertThat(logs).hasSize(1); - final LogRecord log1 = logs.get(0); - final String formattedMessage = new java.util.logging.SimpleFormatter().formatMessage(log1); - assertThat(formattedMessage).isEqualTo("hello, {0} world"); - } - - @Test - public void placeholdersInFormattedMessage() { - julLogger.setLevel(Level.INFO); - log4jLogger.info("hello, {}", "{0} world"); - - final List logs = handler.getStoredLogRecords(); - assertThat(logs).hasSize(1); - final LogRecord log1 = logs.get(0); - final String formattedMessage = new java.util.logging.SimpleFormatter().formatMessage(log1); - assertThat(formattedMessage).isEqualTo("hello, {0} world"); - } -} diff --git a/log4j-to-slf4j/pom.xml b/log4j-to-slf4j/pom.xml deleted file mode 100644 index 2240022d4cb..00000000000 --- a/log4j-to-slf4j/pom.xml +++ /dev/null @@ -1,163 +0,0 @@ - - - - - 4.0.0 - - - org.apache.logging.log4j - log4j - ${revision} - ../log4j-parent - - - log4j-to-slf4j - - Log4j API to SLF4J Adapter - - Forwards the Log4j API calls to SLF4J. - (Refer to the `log4j-slf4j[2]-impl` artifacts for forwarding SLF4J to the Log4j API.) - - - - - [1.7,3) - - - org.jspecify.*;resolution:=optional, - - org.slf4j.*;version="${slf4j.support.range}" - - - - org.jspecify;transitive=false - - - 2.0.16 - - - - - - org.slf4j - slf4j-api - ${slf4j2.version} - - - - - - - org.osgi - org.osgi.framework - provided - - - org.jspecify - jspecify - provided - - - org.apache.logging.log4j - log4j-api - - - org.slf4j - slf4j-api - - - org.assertj - assertj-core - test - - - org.hamcrest - hamcrest - test - - - org.junit.jupiter - junit-jupiter-engine - test - - - org.junit.jupiter - junit-jupiter-params - test - - - org.junit.vintage - junit-vintage-engine - test - - - ch.qos.logback - logback-classic - test - - - ch.qos.logback - logback-core - test - - - ch.qos.logback - logback-core - test-jar - test - - - org.apache.logging.log4j - log4j-api-test - test - - - org.mockito - mockito-core - test - - - - - - - - org.apache.maven.plugins - maven-enforcer-plugin - - - ban-logging-dependencies - - - - - ch.qos.logback:*:*:*:test - - - - - - - - - - diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/Activator.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/Activator.java deleted file mode 100644 index 5080869f5dd..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/Activator.java +++ /dev/null @@ -1,31 +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.logging.slf4j; - -import org.apache.logging.log4j.util.ProviderActivator; -import org.osgi.annotation.bundle.Header; - -@Header(name = org.osgi.framework.Constants.BUNDLE_ACTIVATOR, value = "${@class}") -@Header( - name = org.osgi.framework.Constants.BUNDLE_ACTIVATIONPOLICY, - value = org.osgi.framework.Constants.ACTIVATION_LAZY) -public class Activator extends ProviderActivator { - - public Activator() { - super(new SLF4JProvider()); - } -} diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java deleted file mode 100644 index 4f2ef190bb4..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/MDCContextMap.java +++ /dev/null @@ -1,107 +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.logging.slf4j; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import org.apache.logging.log4j.spi.CleanableThreadContextMap; -import org.apache.logging.log4j.util.SortedArrayStringMap; -import org.apache.logging.log4j.util.StringMap; -import org.slf4j.MDC; - -/** - * Bind the ThreadContextMap to the SLF4J MDC. - */ -public class MDCContextMap implements CleanableThreadContextMap { - - private static final StringMap EMPTY_CONTEXT_DATA = new SortedArrayStringMap(1); - - static { - EMPTY_CONTEXT_DATA.freeze(); - } - - @Override - public void put(final String key, final String value) { - MDC.put(key, value); - } - - @Override - public void putAll(final Map m) { - for (final Entry entry : m.entrySet()) { - MDC.put(entry.getKey(), entry.getValue()); - } - } - - @Override - public String get(final String key) { - return MDC.get(key); - } - - @Override - public void remove(final String key) { - MDC.remove(key); - } - - @Override - public void removeAll(final Iterable keys) { - for (final String key : keys) { - MDC.remove(key); - } - } - - @Override - public void clear() { - MDC.clear(); - } - - @Override - public boolean containsKey(final String key) { - final Map map = MDC.getCopyOfContextMap(); - return map != null && map.containsKey(key); - } - - @Override - public Map getCopy() { - final Map contextMap = MDC.getCopyOfContextMap(); - return contextMap != null ? contextMap : new HashMap<>(); - } - - @Override - public Map getImmutableMapOrNull() { - return MDC.getCopyOfContextMap(); - } - - @Override - public boolean isEmpty() { - final Map map = MDC.getCopyOfContextMap(); - return map == null || map.isEmpty(); - } - - @Override - public StringMap getReadOnlyContextData() { - final Map copy = getCopy(); - if (copy.isEmpty()) { - return EMPTY_CONTEXT_DATA; - } - final StringMap result = new SortedArrayStringMap(); - for (final Entry entry : copy.entrySet()) { - result.putValue(entry.getKey(), entry.getValue()); - } - return result; - } -} diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLogBuilder.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLogBuilder.java deleted file mode 100644 index 0d6df98dbe4..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLogBuilder.java +++ /dev/null @@ -1,267 +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.logging.slf4j; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogBuilder; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.SimpleMessage; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.LambdaUtil; -import org.apache.logging.log4j.util.StackLocatorUtil; -import org.apache.logging.log4j.util.Supplier; - -public class SLF4JLogBuilder implements LogBuilder { - - private static Message EMPTY_MESSAGE = new SimpleMessage(""); - private static final String FQCN = SLF4JLogBuilder.class.getName(); - private static final Logger LOGGER = StatusLogger.getLogger(); - - private ExtendedLogger logger; - private Level level; - private Marker marker; - private Throwable throwable; - private volatile boolean inUse; - private final long threadId; - - public SLF4JLogBuilder(final SLF4JLogger logger, final Level level) { - this.logger = logger; - this.level = level; - this.threadId = Thread.currentThread().getId(); - this.inUse = level != null; - } - - public SLF4JLogBuilder() { - this(null, null); - } - - public LogBuilder reset(final SLF4JLogger logger, final Level level) { - this.logger = logger; - this.level = level; - this.marker = null; - this.throwable = null; - this.inUse = true; - return this; - } - - public boolean isInUse() { - return this.inUse; - } - - private boolean isValid() { - if (!inUse) { - LOGGER.warn("Attempt to reuse LogBuilder was ignored. {}", StackLocatorUtil.getCallerClass(2)); - return false; - } - if (this.threadId != Thread.currentThread().getId()) { - LOGGER.warn("LogBuilder can only be used on the owning thread. {}", StackLocatorUtil.getCallerClass(2)); - return false; - } - return true; - } - - private void logMessage(Message message) { - try { - logger.logMessage(FQCN, level, marker, message, throwable); - } finally { - inUse = false; - } - } - - @Override - public LogBuilder withMarker(final Marker marker) { - this.marker = marker; - return this; - } - - @Override - public LogBuilder withThrowable(final Throwable throwable) { - this.throwable = throwable; - return this; - } - - @Override - public LogBuilder withLocation() { - LOGGER.info("Call to withLocation() ignored since SLF4J does not support setting location information."); - return this; - } - - @Override - public LogBuilder withLocation(final StackTraceElement location) { - return withLocation(); - } - - @Override - public void log(CharSequence message) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message)); - } - } - - @Override - public void log(String message) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message)); - } - } - - @Override - public void log(String message, Object... params) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, params)); - } - } - - @Override - public void log(String message, Supplier... params) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, LambdaUtil.getAll(params))); - } - } - - @Override - public void log(Message message) { - if (isValid()) { - logMessage(message); - } - } - - @Override - public void log(final Supplier messageSupplier) { - if (isValid()) { - logMessage(messageSupplier.get()); - } - } - - @Override - public Message logAndGet(final Supplier messageSupplier) { - Message message = null; - if (isValid()) { - logMessage(message = messageSupplier.get()); - } - return message; - } - - @Override - public void log(Object message) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message)); - } - } - - @Override - public void log(String message, Object p0) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0)); - } - } - - @Override - public void log(String message, Object p0, Object p1) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1)); - } - } - - @Override - public void log(String message, Object p0, Object p1, Object p2) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2)); - } - } - - @Override - public void log(String message, Object p0, Object p1, Object p2, Object p3) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2, p3)); - } - } - - @Override - public void log(String message, Object p0, Object p1, Object p2, Object p3, Object p4) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2, p3, p4)); - } - } - - @Override - public void log(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2, p3, p4, p5)); - } - } - - @Override - public void log(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2, p3, p4, p5, p6)); - } - } - - @Override - public void log( - String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2, p3, p4, p5, p6, p7)); - } - } - - @Override - public void log( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2, p3, p4, p5, p6, p7, p8)); - } - } - - @Override - public void log( - String message, - Object p0, - Object p1, - Object p2, - Object p3, - Object p4, - Object p5, - Object p6, - Object p7, - Object p8, - Object p9) { - if (isValid()) { - logMessage(logger.getMessageFactory().newMessage(message, p0, p1, p2, p3, p4, p5, p6, p7, p8, p9)); - } - } - - @Override - public void log() { - if (isValid()) { - logMessage(EMPTY_MESSAGE); - } - } -} diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLogger.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLogger.java deleted file mode 100644 index 26e94c67b35..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLogger.java +++ /dev/null @@ -1,381 +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.logging.slf4j; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.LogBuilder; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.message.LoggerNameAwareMessage; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.spi.AbstractLogger; -import org.apache.logging.log4j.util.Constants; -import org.slf4j.LoggerFactory; -import org.slf4j.MarkerFactory; -import org.slf4j.spi.LocationAwareLogger; - -public class SLF4JLogger extends AbstractLogger { - - private static final long serialVersionUID = 1L; - /** - * Logback supports turbo filters, that can override the logger's level. - * Therefore we can never return a no-op builder. - */ - private static final boolean LAZY_LEVEL_CHECK = "ch.qos.logback.classic.LoggerContext" - .equals(LoggerFactory.getILoggerFactory().getClass().getName()); - - private static final ThreadLocal logBuilder = ThreadLocal.withInitial(SLF4JLogBuilder::new); - - private final org.slf4j.Logger logger; - private final LocationAwareLogger locationAwareLogger; - - public SLF4JLogger(final String name, final MessageFactory messageFactory, final org.slf4j.Logger logger) { - super(name, messageFactory); - this.logger = logger; - this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null; - } - - public SLF4JLogger(final String name, final org.slf4j.Logger logger) { - super(name); - this.logger = logger; - this.locationAwareLogger = logger instanceof LocationAwareLogger ? (LocationAwareLogger) logger : null; - } - - private int convertLevel(final Level level) { - switch (level.getStandardLevel()) { - case DEBUG: - return LocationAwareLogger.DEBUG_INT; - case TRACE: - return LocationAwareLogger.TRACE_INT; - case INFO: - return LocationAwareLogger.INFO_INT; - case WARN: - return LocationAwareLogger.WARN_INT; - case ERROR: - return LocationAwareLogger.ERROR_INT; - default: - return LocationAwareLogger.ERROR_INT; - } - } - - @Override - public Level getLevel() { - if (logger.isTraceEnabled()) { - return Level.TRACE; - } - if (logger.isDebugEnabled()) { - return Level.DEBUG; - } - if (logger.isInfoEnabled()) { - return Level.INFO; - } - if (logger.isWarnEnabled()) { - return Level.WARN; - } - if (logger.isErrorEnabled()) { - return Level.ERROR; - } - // Option: throw new IllegalStateException("Unknown SLF4JLevel"); - // Option: return Level.ALL; - return Level.OFF; - } - - public org.slf4j.Logger getLogger() { - return locationAwareLogger != null ? locationAwareLogger : logger; - } - - private static org.slf4j.Marker getMarker(final Marker marker) { - // No marker is provided in the common case, small methods - // are optimized more effectively. - return marker == null ? null : convertMarker(marker); - } - - private static org.slf4j.Marker convertMarker(final Marker marker) { - final org.slf4j.Marker slf4jMarker = MarkerFactory.getMarker(marker.getName()); - final Marker[] parents = marker.getParents(); - if (parents != null) { - for (final Marker parent : parents) { - final org.slf4j.Marker slf4jParent = getMarker(parent); - if (!slf4jMarker.contains(slf4jParent)) { - slf4jMarker.add(slf4jParent); - } - } - } - return slf4jMarker; - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final Message data, final Throwable t) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final CharSequence data, final Throwable t) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final Object data, final Throwable t) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String data) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String data, final Object... p1) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String message, final Object p0) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, final Marker marker, final String message, final Object p0, final Object p1) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6, - final Object p7) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6, - final Object p7, - final Object p8) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled( - final Level level, - final Marker marker, - final String message, - final Object p0, - final Object p1, - final Object p2, - final Object p3, - final Object p4, - final Object p5, - final Object p6, - final Object p7, - final Object p8, - final Object p9) { - return isEnabledFor(level, marker); - } - - @Override - public boolean isEnabled(final Level level, final Marker marker, final String data, final Throwable t) { - return isEnabledFor(level, marker); - } - - private boolean isEnabledFor(final Level level, final Marker marker) { - final org.slf4j.Marker slf4jMarker = getMarker(marker); - switch (level.getStandardLevel()) { - case DEBUG: - return logger.isDebugEnabled(slf4jMarker); - case TRACE: - return logger.isTraceEnabled(slf4jMarker); - case INFO: - return logger.isInfoEnabled(slf4jMarker); - case WARN: - return logger.isWarnEnabled(slf4jMarker); - case ERROR: - return logger.isErrorEnabled(slf4jMarker); - default: - return logger.isErrorEnabled(slf4jMarker); - } - } - - @Override - public void logMessage( - final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) { - final org.slf4j.Marker slf4jMarker = getMarker(marker); - final String formattedMessage = message.getFormattedMessage(); - if (locationAwareLogger != null) { - if (message instanceof LoggerNameAwareMessage) { - ((LoggerNameAwareMessage) message).setLoggerName(getName()); - } - locationAwareLogger.log(slf4jMarker, fqcn, convertLevel(level), formattedMessage, null, t); - } else { - switch (level.getStandardLevel()) { - case DEBUG: - logger.debug(slf4jMarker, formattedMessage, t); - break; - case TRACE: - logger.trace(slf4jMarker, formattedMessage, t); - break; - case INFO: - logger.info(slf4jMarker, formattedMessage, t); - break; - case WARN: - logger.warn(slf4jMarker, formattedMessage, t); - break; - case ERROR: - logger.error(slf4jMarker, formattedMessage, t); - break; - default: - logger.error(slf4jMarker, formattedMessage, t); - break; - } - } - } - - @Override - public LogBuilder always() { - return atLevel(Level.OFF); - } - - @Override - public LogBuilder atTrace() { - return atLevel(Level.TRACE); - } - - @Override - public LogBuilder atDebug() { - return atLevel(Level.DEBUG); - } - - @Override - public LogBuilder atInfo() { - return atLevel(Level.INFO); - } - - @Override - public LogBuilder atWarn() { - return atLevel(Level.WARN); - } - - @Override - public LogBuilder atError() { - return atLevel(Level.ERROR); - } - - @Override - public LogBuilder atFatal() { - return atLevel(Level.TRACE); - } - - @Override - protected LogBuilder getLogBuilder(final Level level) { - final SLF4JLogBuilder builder = logBuilder.get(); - return Constants.ENABLE_THREADLOCALS && !builder.isInUse() - ? builder.reset(this, level) - : new SLF4JLogBuilder(this, level); - } - - @Override - public LogBuilder atLevel(final Level level) { - // TODO: wrap SLF4J 2.x LoggingEventBuilder - if (LAZY_LEVEL_CHECK) { - return getLogBuilder(level); - } - return super.atLevel(level); - } -} diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContext.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContext.java deleted file mode 100644 index f0aee0af414..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContext.java +++ /dev/null @@ -1,64 +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.logging.slf4j; - -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.spi.ExtendedLogger; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.spi.LoggerRegistry; -import org.slf4j.LoggerFactory; - -public class SLF4JLoggerContext implements LoggerContext { - private final LoggerRegistry loggerRegistry = new LoggerRegistry<>(); - - @Override - public Object getExternalContext() { - return null; - } - - @Override - public ExtendedLogger getLogger(final String name) { - if (!loggerRegistry.hasLogger(name)) { - loggerRegistry.putIfAbsent(name, null, new SLF4JLogger(name, LoggerFactory.getLogger(name))); - } - return loggerRegistry.getLogger(name); - } - - @Override - public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) { - if (!loggerRegistry.hasLogger(name, messageFactory)) { - loggerRegistry.putIfAbsent( - name, messageFactory, new SLF4JLogger(name, messageFactory, LoggerFactory.getLogger(name))); - } - return loggerRegistry.getLogger(name, messageFactory); - } - - @Override - public boolean hasLogger(final String name) { - return loggerRegistry.hasLogger(name); - } - - @Override - public boolean hasLogger(final String name, final MessageFactory messageFactory) { - return loggerRegistry.hasLogger(name, messageFactory); - } - - @Override - public boolean hasLogger(final String name, final Class messageFactoryClass) { - return loggerRegistry.hasLogger(name, messageFactoryClass); - } -} diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContextFactory.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContextFactory.java deleted file mode 100644 index e1ca5d44831..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JLoggerContextFactory.java +++ /dev/null @@ -1,69 +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.logging.slf4j; - -import java.net.URI; -import org.apache.logging.log4j.spi.LoggerContext; -import org.apache.logging.log4j.spi.LoggerContextFactory; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.LoaderUtil; - -public class SLF4JLoggerContextFactory implements LoggerContextFactory { - private static final StatusLogger LOGGER = StatusLogger.getLogger(); - private static final LoggerContext context = new SLF4JLoggerContext(); - - public SLF4JLoggerContextFactory() { - // LOG4J2-230, LOG4J2-204 (improve error reporting when misconfigured) - boolean misconfigured = false; - try { - LoaderUtil.loadClass("org.slf4j.helpers.Log4jLoggerFactory"); - misconfigured = true; - } catch (final ClassNotFoundException classNotFoundIsGood) { - LOGGER.debug("org.slf4j.helpers.Log4jLoggerFactory is not on classpath. Good!"); - } - if (misconfigured) { - throw new IllegalStateException("slf4j-impl jar is mutually exclusive with log4j-to-slf4j jar " - + "(the first routes calls from SLF4J to Log4j, the second from Log4j to SLF4J)"); - } - } - - @Override - public LoggerContext getContext( - final String fqcn, final ClassLoader loader, final Object externalContext, final boolean currentContext) { - return context; - } - - @Override - public LoggerContext getContext( - final String fqcn, - final ClassLoader loader, - final Object externalContext, - final boolean currentContext, - final URI configLocation, - final String name) { - return context; - } - - @Override - public void removeContext(final LoggerContext ignored) {} - - @Override - public boolean isClassLoaderDependent() { - // context is always used - return false; - } -} diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JProvider.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JProvider.java deleted file mode 100644 index 52a11a55603..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/SLF4JProvider.java +++ /dev/null @@ -1,49 +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.logging.slf4j; - -import aQute.bnd.annotation.Resolution; -import aQute.bnd.annotation.spi.ServiceProvider; -import org.apache.logging.log4j.spi.LoggerContextFactory; -import org.apache.logging.log4j.spi.Provider; -import org.apache.logging.log4j.spi.ThreadContextMap; -import org.jspecify.annotations.NullMarked; - -/** - * Bind the Log4j API to SLF4J. - */ -@NullMarked -@ServiceProvider(value = Provider.class, resolution = Resolution.OPTIONAL) -public class SLF4JProvider extends Provider { - - private static final LoggerContextFactory CONTEXT_FACTORY = new SLF4JLoggerContextFactory(); - private static final ThreadContextMap THREAD_CONTEXT_MAP = new MDCContextMap(); - - public SLF4JProvider() { - super(15, CURRENT_VERSION, SLF4JLoggerContextFactory.class, MDCContextMap.class); - } - - @Override - public LoggerContextFactory getLoggerContextFactory() { - return CONTEXT_FACTORY; - } - - @Override - public ThreadContextMap getThreadContextMapInstance() { - return THREAD_CONTEXT_MAP; - } -} diff --git a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/package-info.java b/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/package-info.java deleted file mode 100644 index ba4cb130be1..00000000000 --- a/log4j-to-slf4j/src/main/java/org/apache/logging/slf4j/package-info.java +++ /dev/null @@ -1,25 +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. - */ -/** - * SLF4J support. - */ -@Export -@Version("2.24.0") -package org.apache.logging.slf4j; - -import org.osgi.annotation.bundle.Export; -import org.osgi.annotation.versioning.Version; diff --git a/log4j-to-slf4j/src/main/resources/META-INF/services/org.apache.logging.log4j.spi.Provider b/log4j-to-slf4j/src/main/resources/META-INF/services/org.apache.logging.log4j.spi.Provider deleted file mode 100644 index c66b5c946ab..00000000000 --- a/log4j-to-slf4j/src/main/resources/META-INF/services/org.apache.logging.log4j.spi.Provider +++ /dev/null @@ -1 +0,0 @@ -org.apache.logging.slf4j.SLF4JProvider \ No newline at end of file diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java deleted file mode 100644 index b43a4ea68ff..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java +++ /dev/null @@ -1,61 +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.logging.slf4j; - -import static org.junit.Assert.assertEquals; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.testUtil.StringListAppender; -import java.util.List; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.test.junit.UsingStatusListener; -import org.junit.jupiter.api.Test; - -@UsingStatusListener -@LoggerContextSource -public class CallerInformationTest { - - @Test - public void testClassLogger() throws Exception { - final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("ClassLogger"); - final StringListAppender app = TestUtil.getListAppender(logger, "Class"); - logger.info("Ignored message contents."); - logger.warn("Verifying the caller class is still correct."); - logger.error("Hopefully nobody breaks me!"); - final List messages = app.strList; - assertEquals("Incorrect number of messages.", 3, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller class name.", this.getClass().getName(), message); - } - } - - @Test - public void testMethodLogger() throws Exception { - final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("MethodLogger"); - final StringListAppender app = TestUtil.getListAppender(logger, "Method"); - logger.info("More messages."); - logger.warn("CATASTROPHE INCOMING!"); - logger.error("ZOMBIES!!!"); - logger.warn("brains~~~"); - logger.info("Itchy. Tasty."); - final List messages = app.strList; - assertEquals("Incorrect number of messages.", 5, messages.size()); - for (final String message : messages) { - assertEquals("Incorrect caller method name.", "testMethodLogger", message); - } - } -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/Log4j2Jira1688Test.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/Log4j2Jira1688Test.java deleted file mode 100644 index 18779b11eeb..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/Log4j2Jira1688Test.java +++ /dev/null @@ -1,68 +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.logging.slf4j; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.util.Arrays; -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Tests LOG4J2-1688 Multiple loggings of arguments are setting these arguments to null. - */ -public class Log4j2Jira1688Test { - - @Test - public void testLog4j2() { - - // Argument-array creation - final int limit = 37; - final Object[] args = createArray(limit); - final Object[] originalArgs = Arrays.copyOf(args, args.length); - - System.out.println("args " + Arrays.toString(args)); - - // Logger definition - final String someFormat = "test {}"; - final Logger logger = LoggerFactory.getLogger(this.getClass()); - - // First logging of args - logger.error(someFormat, args); // Only the first element (args[0]) of args will be logged - why? - // GG: because the pattern {} picks up the 1 st argument, not the whole array - assertThat(args).containsExactly(originalArgs); - - // Bug: The second logging of args sets all elements of args to null - logger.error(someFormat, args); - // GG: All is well args is still intact - System.out.println("args " + Arrays.toString(args)); - assertThat(args).containsExactly(originalArgs); - } - - /** - * @param size - * @return - */ - private static Object[] createArray(final int size) { - final Object[] args = new Object[size]; - for (int i = 0; i < args.length; i++) { - args[i] = i; - } - return args; - } -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LogBuilderTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LogBuilderTest.java deleted file mode 100644 index 8d073072b9e..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LogBuilderTest.java +++ /dev/null @@ -1,106 +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.logging.slf4j; - -import static org.assertj.core.api.Assertions.assertThat; - -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.testUtil.StringListAppender; -import java.util.function.Consumer; -import java.util.stream.Stream; -import org.apache.logging.log4j.CloseableThreadContext; -import org.apache.logging.log4j.CloseableThreadContext.Instance; -import org.apache.logging.log4j.LogBuilder; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.message.Message; -import org.apache.logging.log4j.message.SimpleMessage; -import org.apache.logging.log4j.test.junit.UsingStatusListener; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.MethodSource; - -@UsingStatusListener -@LoggerContextSource -public class LogBuilderTest { - - private static final CharSequence CHAR_SEQUENCE = "CharSequence"; - private static final String STRING = "String"; - private static final Message MESSAGE = new SimpleMessage(); - private static final Object[] P = {"p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9"}; - private static final Object OBJECT = "Object"; - - // Log4j objects - private static Logger logger; - // Logback objects - private static LoggerContext context; - private static StringListAppender list; - - @BeforeAll - public static void setUp() throws Exception { - final org.slf4j.Logger slf4jLogger = context.getLogger(LogBuilderTest.class); - logger = LogManager.getLogger(LogBuilderTest.class); - assertThat(slf4jLogger).isSameAs(((SLF4JLogger) logger).getLogger()); - final ch.qos.logback.classic.Logger rootLogger = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); - rootLogger.detachAppender("console"); - list = TestUtil.getListAppender(rootLogger, "LIST"); - assertThat(list).isNotNull().extracting("strList").isNotNull(); - list.strList.clear(); - } - - static Stream> logBuilderMethods() { - return Stream.of( - logBuilder -> logBuilder.log(), - logBuilder -> logBuilder.log(CHAR_SEQUENCE), - logBuilder -> logBuilder.log(MESSAGE), - logBuilder -> logBuilder.log(OBJECT), - logBuilder -> logBuilder.log(STRING), - logBuilder -> logBuilder.log(STRING, P[0]), - logBuilder -> logBuilder.log(STRING, P[0], P[1]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2], P[3]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2], P[3], P[4]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2], P[3], P[4], P[5]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2], P[3], P[4], P[5], P[6]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2], P[3], P[4], P[5], P[6], P[7]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2], P[3], P[4], P[5], P[6], P[7], P[8]), - logBuilder -> logBuilder.log(STRING, P[0], P[1], P[2], P[3], P[4], P[5], P[6], P[7], P[8], P[9]), - logBuilder -> logBuilder.log(STRING, P), - logBuilder -> logBuilder.log(STRING, () -> OBJECT), - logBuilder -> logBuilder.log(() -> MESSAGE)); - } - - @ParameterizedTest - @MethodSource("logBuilderMethods") - void testTurboFilter(final Consumer consumer) { - consumer.accept(logger.atTrace()); - try (final Instance c = CloseableThreadContext.put("callerId", "Log4j2")) { - consumer.accept(logger.atTrace()); - assertThat(list.strList).hasSize(1); - } - list.strList.clear(); - } - - @ParameterizedTest - @MethodSource("logBuilderMethods") - void testLevelThreshold(final Consumer consumer) { - consumer.accept(logger.atInfo()); - assertThat(list.strList).hasSize(1); - list.strList.clear(); - } -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextResolver.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextResolver.java deleted file mode 100644 index dc9dd5d343b..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextResolver.java +++ /dev/null @@ -1,166 +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.logging.slf4j; - -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.joran.JoranConfigurator; -import ch.qos.logback.core.joran.spi.JoranException; -import java.net.URL; -import org.apache.logging.log4j.test.junit.ExtensionContextAnchor; -import org.junit.jupiter.api.extension.BeforeAllCallback; -import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ExtensionContext.Store; -import org.junit.jupiter.api.extension.ExtensionContextException; -import org.junit.jupiter.api.extension.ParameterContext; -import org.junit.jupiter.api.extension.ParameterResolutionException; -import org.junit.jupiter.api.extension.support.TypeBasedParameterResolver; -import org.junit.platform.commons.support.AnnotationSupport; -import org.junit.platform.commons.support.HierarchyTraversalMode; -import org.junit.platform.commons.support.ModifierSupport; -import org.junit.platform.commons.support.ReflectionSupport; -import org.slf4j.LoggerFactory; - -class LoggerContextResolver extends TypeBasedParameterResolver - implements BeforeAllCallback, BeforeEachCallback { - - private static final Object KEY = LoggerContextHolder.class; - - @Override - public void beforeEach(ExtensionContext extensionContext) throws Exception { - final Class testClass = extensionContext.getRequiredTestClass(); - if (AnnotationSupport.isAnnotated(testClass, LoggerContextSource.class)) { - final LoggerContextHolder holder = - ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); - if (holder == null) { - throw new IllegalStateException( - "Specified @LoggerContextSource but no LoggerContext found for test class " - + testClass.getCanonicalName()); - } - } - AnnotationSupport.findAnnotation(extensionContext.getRequiredTestMethod(), LoggerContextSource.class) - .ifPresent(source -> { - final LoggerContextHolder holder = new LoggerContextHolder(source, extensionContext); - ExtensionContextAnchor.setAttribute(KEY, holder, extensionContext); - }); - final LoggerContextHolder holder = - ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); - if (holder != null) { - ReflectionSupport.findFields( - extensionContext.getRequiredTestClass(), - f -> ModifierSupport.isNotStatic(f) && f.getType().equals(LoggerContext.class), - HierarchyTraversalMode.TOP_DOWN) - .forEach(f -> { - try { - f.setAccessible(true); - f.set(extensionContext.getRequiredTestInstance(), holder.getLoggerContext()); - } catch (ReflectiveOperationException e) { - throw new ExtensionContextException("Failed to inject field " + f, e); - } - }); - } - } - - @Override - public void beforeAll(ExtensionContext extensionContext) throws Exception { - final Class testClass = extensionContext.getRequiredTestClass(); - AnnotationSupport.findAnnotation(testClass, LoggerContextSource.class).ifPresent(testSource -> { - final LoggerContextHolder holder = new LoggerContextHolder(testSource, extensionContext); - ExtensionContextAnchor.setAttribute(KEY, holder, extensionContext); - }); - final LoggerContextHolder holder = - ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); - if (holder != null) { - ReflectionSupport.findFields( - extensionContext.getRequiredTestClass(), - f -> ModifierSupport.isStatic(f) && f.getType().equals(LoggerContext.class), - HierarchyTraversalMode.TOP_DOWN) - .forEach(f -> { - try { - f.setAccessible(true); - f.set(null, holder.getLoggerContext()); - } catch (ReflectiveOperationException e) { - throw new ExtensionContextException("Failed to inject field " + f, e); - } - }); - } - } - - @Override - public LoggerContext resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - throws ParameterResolutionException { - return ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext) - .getLoggerContext(); - } - - static final class LoggerContextHolder implements Store.CloseableResource { - - private final LoggerContext context; - private final Logger logger; - - private LoggerContextHolder(final LoggerContextSource source, final ExtensionContext extensionContext) { - this.context = (LoggerContext) LoggerFactory.getILoggerFactory(); - Class clazz = extensionContext.getRequiredTestClass(); - this.logger = context.getLogger(clazz); - - final JoranConfigurator configurator = new JoranConfigurator(); - final URL configLocation = getConfigLocation(source, extensionContext); - configurator.setContext(context); - try { - configurator.doConfigure(configLocation); - } catch (final JoranException e) { - throw new ExtensionContextException("Failed to initialize Logback logger context for " + clazz, e); - } - } - - private static URL getConfigLocation( - final LoggerContextSource source, final ExtensionContext extensionContext) { - final String value = source.value(); - Class clazz = extensionContext.getRequiredTestClass(); - URL url = null; - if (value.isEmpty()) { - while (clazz != null) { - url = clazz.getResource(clazz.getSimpleName() + ".xml"); - if (url != null) { - break; - } - clazz = clazz.getSuperclass(); - } - } else { - url = clazz.getClassLoader().getResource(value); - } - if (url != null) { - return url; - } - throw new ExtensionContextException("Failed to find a default configuration for " + clazz); - } - - public LoggerContext getLoggerContext() { - return context; - } - - public Logger getLogger() { - return logger; - } - - @Override - public void close() { - context.stop(); - } - } -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextSource.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextSource.java deleted file mode 100644 index d71921c21f0..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextSource.java +++ /dev/null @@ -1,52 +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.logging.slf4j; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import org.apache.logging.log4j.test.junit.TempLoggingDirectory; -import org.junit.jupiter.api.Tag; -import org.junit.jupiter.api.extension.ExtendWith; - -/** - * Specifies a configuration file to use for unit tests. - * - * This is similar to the org.apache.logging.log4j.core.junit.LoggerContextSource annotation for Log4j. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) -@Documented -@Inherited -@Tag("functional") -@ExtendWith(TempLoggingDirectory.class) -@ExtendWith(LoggerContextResolver.class) -@ExtendWith(LoggerResolver.class) -public @interface LoggerContextSource { - /** - * Specifies the name of the configuration file to use for the annotated test. - *

- * Defaults to the fully qualified name of the test class with '.xml' appended. - * E.g. this class would have a default of - * {@code org/apache/logging/log4j/core/test/junit/LoggerContextSource.xml}. - *

- */ - String value() default ""; -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerResolver.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerResolver.java deleted file mode 100644 index 28f94113a30..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerResolver.java +++ /dev/null @@ -1,84 +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.logging.slf4j; - -import ch.qos.logback.classic.Logger; -import org.apache.logging.log4j.test.junit.ExtensionContextAnchor; -import org.apache.logging.slf4j.LoggerContextResolver.LoggerContextHolder; -import org.junit.jupiter.api.extension.BeforeAllCallback; -import org.junit.jupiter.api.extension.BeforeEachCallback; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ExtensionContextException; -import org.junit.jupiter.api.extension.ParameterContext; -import org.junit.jupiter.api.extension.ParameterResolutionException; -import org.junit.jupiter.api.extension.support.TypeBasedParameterResolver; -import org.junit.platform.commons.support.HierarchyTraversalMode; -import org.junit.platform.commons.support.ModifierSupport; -import org.junit.platform.commons.support.ReflectionSupport; - -public class LoggerResolver extends TypeBasedParameterResolver - implements BeforeAllCallback, BeforeEachCallback { - - private static final Object KEY = LoggerContextHolder.class; - - @Override - public Logger resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) - throws ParameterResolutionException { - return ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext) - .getLogger(); - } - - @Override - public void beforeAll(ExtensionContext extensionContext) throws Exception { - final LoggerContextHolder holder = - ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); - if (holder != null) { - ReflectionSupport.findFields( - extensionContext.getRequiredTestClass(), - f -> ModifierSupport.isStatic(f) && f.getType().equals(Logger.class), - HierarchyTraversalMode.TOP_DOWN) - .forEach(f -> { - try { - f.setAccessible(true); - f.set(null, holder.getLogger()); - } catch (ReflectiveOperationException e) { - throw new ExtensionContextException("Failed to inject field " + f, e); - } - }); - } - } - - @Override - public void beforeEach(ExtensionContext extensionContext) throws Exception { - final LoggerContextHolder holder = - ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext); - if (holder != null) { - ReflectionSupport.findFields( - extensionContext.getRequiredTestClass(), - f -> ModifierSupport.isNotStatic(f) && f.getType().equals(Logger.class), - HierarchyTraversalMode.TOP_DOWN) - .forEach(f -> { - try { - f.setAccessible(true); - f.set(extensionContext.getRequiredTestInstance(), holder.getLogger()); - } catch (ReflectiveOperationException e) { - throw new ExtensionContextException("Failed to inject field " + f, e); - } - }); - } - } -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java deleted file mode 100644 index 5f89d5a5261..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java +++ /dev/null @@ -1,268 +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.logging.slf4j; - -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.theInstance; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.testUtil.StringListAppender; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Proxy; -import java.util.Date; -import java.util.List; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.message.MessageFactory; -import org.apache.logging.log4j.message.ParameterizedMessageFactory; -import org.apache.logging.log4j.message.StringFormatterMessageFactory; -import org.apache.logging.log4j.spi.AbstractLogger; -import org.apache.logging.log4j.spi.MessageFactory2Adapter; -import org.apache.logging.log4j.test.junit.UsingStatusListener; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.slf4j.MDC; - -@UsingStatusListener -@LoggerContextSource -public class LoggerTest { - - private static final Object OBJ = new Object(); - // Log4j objects - private Logger logger; - // Logback objects - private LoggerContext context; - private StringListAppender list; - - @BeforeEach - public void setUp() throws Exception { - final org.slf4j.Logger slf4jLogger = context.getLogger(getClass()); - logger = LogManager.getLogger(); - assertThat(slf4jLogger, is(theInstance(((SLF4JLogger) logger).getLogger()))); - final ch.qos.logback.classic.Logger rootLogger = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); - rootLogger.detachAppender("console"); - list = TestUtil.getListAppender(rootLogger, "LIST"); - assertThat(list, is(notNullValue())); - assertThat(list.strList, is(notNullValue())); - list.strList.clear(); - } - - @Test - public void basicFlow() { - logger.traceEntry(); - logger.traceExit(); - assertThat(list.strList, hasSize(2)); - } - - @Test - public void basicFlowDepreacted() { - logger.entry(); - logger.exit(); - assertThat(list.strList, hasSize(2)); - } - - @Test - public void simpleFlowDeprecated() { - logger.entry(OBJ); - logger.exit(0); - assertThat(list.strList, hasSize(2)); - } - - @Test - public void simpleFlow() { - logger.entry(OBJ); - logger.traceExit(0); - assertThat(list.strList, hasSize(2)); - } - - @Test - public void throwing() { - logger.throwing(new IllegalArgumentException("Test Exception")); - assertThat(list.strList, hasSize(1)); - } - - @Test - public void catching() { - try { - throw new NullPointerException(); - } catch (final Exception e) { - logger.catching(e); - } - assertThat(list.strList, hasSize(1)); - } - - @Test - public void debug() { - logger.debug("Debug message"); - assertThat(list.strList, hasSize(1)); - } - - @Test - public void getLogger_String_MessageFactoryMismatch() { - final Logger testLogger = testMessageFactoryMismatch( - "getLogger_String_MessageFactoryMismatch", - StringFormatterMessageFactory.INSTANCE, - ParameterizedMessageFactory.INSTANCE); - testLogger.debug("%,d", Integer.MAX_VALUE); - assertThat(list.strList, hasSize(1)); - assertThat(list.strList, hasItem(String.format("%,d", Integer.MAX_VALUE))); - } - - @Test - public void getLogger_String_MessageFactoryMismatchNull() { - final Logger testLogger = testMessageFactoryMismatch( - "getLogger_String_MessageFactoryMismatchNull", StringFormatterMessageFactory.INSTANCE, null); - testLogger.debug("%,d", Integer.MAX_VALUE); - assertThat(list.strList, hasSize(1)); - assertThat(list.strList, hasItem(String.format("%,d", Integer.MAX_VALUE))); - } - - private Logger testMessageFactoryMismatch( - final String name, final MessageFactory messageFactory1, final MessageFactory messageFactory2) { - final Logger testLogger = LogManager.getLogger(name, messageFactory1); - assertThat(testLogger, is(notNullValue())); - checkMessageFactory(messageFactory1, testLogger); - final Logger testLogger2 = LogManager.getLogger(name, messageFactory2); - checkMessageFactory(messageFactory2, testLogger2); - return testLogger; - } - - private static void checkMessageFactory(final MessageFactory messageFactory1, final Logger testLogger1) { - if (messageFactory1 == null) { - assertEquals( - AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS, - testLogger1.getMessageFactory().getClass()); - } else { - MessageFactory actual = testLogger1.getMessageFactory(); - if (actual instanceof MessageFactory2Adapter) { - actual = ((MessageFactory2Adapter) actual).getOriginal(); - } - assertEquals(messageFactory1, actual); - } - } - - @Test - public void debugObject() { - logger.debug(new Date()); - assertThat(list.strList, hasSize(1)); - } - - @Test - public void debugWithParms() { - logger.debug("Hello, {}", "World"); - assertThat(list.strList, hasSize(1)); - final String message = list.strList.get(0); - assertEquals("Hello, World", message); - } - - @Test - public void paramIncludesSubstitutionMarker_locationAware() { - logger.info("Hello, {}", "foo {} bar"); - assertThat(list.strList, hasSize(1)); - final String message = list.strList.get(0); - assertEquals("Hello, foo {} bar", message); - } - - @Test - public void paramIncludesSubstitutionMarker_nonLocationAware() { - final org.slf4j.Logger slf4jLogger = context.getLogger(getClass()); - final Logger nonLocationAwareLogger = - new SLF4JLogger(slf4jLogger.getName(), (org.slf4j.Logger) Proxy.newProxyInstance( - getClass().getClassLoader(), new Class[] {org.slf4j.Logger.class}, (proxy, method, args) -> { - try { - return method.invoke(slf4jLogger, args); - } catch (InvocationTargetException e) { - throw e.getCause(); - } - })); - nonLocationAwareLogger.info("Hello, {}", "foo {} bar"); - assertThat(list.strList, hasSize(1)); - final String message = list.strList.get(0); - assertEquals("Hello, foo {} bar", message); - } - - @Test - public void testImpliedThrowable() { - logger.debug("This is a test", new Throwable("Testing")); - final List msgs = list.strList; - assertThat(msgs, hasSize(1)); - final String expected = "java.lang.Throwable: Testing"; - assertTrue("Incorrect message data", msgs.get(0).contains(expected)); - } - - @SuppressWarnings("unchecked") - @Test - public void mdc() { - ThreadContext.put("TestYear", Integer.toString(2010)); - logger.debug("Debug message"); - ThreadContext.clearMap(); - logger.debug("Debug message"); - assertThat(list.strList, hasSize(2)); - assertTrue("Incorrect year", list.strList.get(0).startsWith("2010")); - } - - @Test - public void mdcNullBackedIsEmpty() { - assertNull("Setup wrong", MDC.getCopyOfContextMap()); - assertTrue(ThreadContext.isEmpty()); - } - - @Test - public void mdcNullBackedContainsKey() { - assertNull("Setup wrong", MDC.getCopyOfContextMap()); - assertFalse(ThreadContext.containsKey("something")); - } - - @Test - public void mdcNullBackedContainsNullKey() { - assertNull("Setup wrong", MDC.getCopyOfContextMap()); - assertFalse(ThreadContext.containsKey(null)); - } - - @Test - public void mdcContainsNullKey() { - try { - ThreadContext.put("some", "thing"); - assertNotNull("Setup wrong", MDC.getCopyOfContextMap()); - assertFalse(ThreadContext.containsKey(null)); - } finally { - ThreadContext.clearMap(); - } - } - - @Test - public void mdcCannotContainNullKey() { - try { - ThreadContext.put(null, "something"); - fail("should throw"); - } catch (IllegalArgumentException | NullPointerException e) { - // expected - } - } -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/MDCContextMapTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/MDCContextMapTest.java deleted file mode 100644 index 6673de4597a..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/MDCContextMapTest.java +++ /dev/null @@ -1,50 +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.logging.slf4j; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -import org.apache.logging.log4j.spi.ThreadContextMap; -import org.junit.jupiter.api.Test; -import org.junitpioneer.jupiter.Issue; -import org.slf4j.MDCTestHelper; -import org.slf4j.spi.MDCAdapter; - -class MDCContextMapTest { - - @Test - @Issue("https://github.com/apache/logging-log4j2/issues/1426") - void nonNullGetCopy() { - final ThreadContextMap contextMap = new MDCContextMap(); - final MDCAdapter mockAdapter = mock(MDCAdapter.class); - when(mockAdapter.getCopyOfContextMap()).thenReturn(null); - final MDCAdapter adapter = MDCTestHelper.replaceMDCAdapter(mockAdapter); - try { - assertThat(contextMap.getImmutableMapOrNull()).isNull(); - assertThat(contextMap.getCopy()).isNotNull(); - verify(mockAdapter, times(2)).getCopyOfContextMap(); - verifyNoMoreInteractions(mockAdapter); - } finally { - MDCTestHelper.replaceMDCAdapter(adapter); - } - } -} diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/TestUtil.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/TestUtil.java deleted file mode 100644 index afcd9a81d4e..00000000000 --- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/TestUtil.java +++ /dev/null @@ -1,47 +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.logging.slf4j; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.core.spi.AppenderAttachable; -import ch.qos.logback.core.testUtil.StringListAppender; -import org.slf4j.Logger; - -/** - * Utility methods for unit tests integrating with Logback. - * - * @since 2.1 - */ -public final class TestUtil { - - public static StringListAppender getListAppender(final SLF4JLogger slf4jLogger, final String name) { - final Logger logger = slf4jLogger.getLogger(); - if (!(logger instanceof AppenderAttachable)) { - throw new AssertionError("SLF4JLogger.getLogger() did not return an instance of AppenderAttachable"); - } - @SuppressWarnings("unchecked") - final AppenderAttachable attachable = (AppenderAttachable) logger; - return getListAppender(attachable, name); - } - - public static StringListAppender getListAppender( - final AppenderAttachable logger, final String name) { - return (StringListAppender) logger.getAppender(name); - } - - private TestUtil() {} -} diff --git a/log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java b/log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java deleted file mode 100644 index 0256131b8f2..00000000000 --- a/log4j-to-slf4j/src/test/java/org/slf4j/MDCTestHelper.java +++ /dev/null @@ -1,28 +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.slf4j; - -import org.slf4j.spi.MDCAdapter; - -public class MDCTestHelper { - - public static MDCAdapter replaceMDCAdapter(final MDCAdapter adapter) { - final MDCAdapter old = MDC.mdcAdapter; - MDC.mdcAdapter = adapter; - return old; - } -} diff --git a/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/CallerInformationTest.xml b/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/CallerInformationTest.xml deleted file mode 100644 index 9cef52d471c..00000000000 --- a/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/CallerInformationTest.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - %class - - - - - - - - - %method - - - - - - diff --git a/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LogBuilderTest.xml b/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LogBuilderTest.xml deleted file mode 100644 index 9305cc8772a..00000000000 --- a/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LogBuilderTest.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - ACCEPT - callerId - INFO - - Log4j2 - TRACE - - - - - - %msg - - - - - - - diff --git a/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LoggerTest.xml b/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LoggerTest.xml deleted file mode 100644 index ec38ed6fc0c..00000000000 --- a/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LoggerTest.xml +++ /dev/null @@ -1,29 +0,0 @@ - - - - - - - %X{TestYear}%msg - - - - - - - diff --git a/pom.xml b/pom.xml index c1ad72ab70c..674a6851ba4 100644 --- a/pom.xml +++ b/pom.xml @@ -232,7 +232,6 @@ - jul-to-log4j log4j-async-logger log4j-compress log4j-config-jackson @@ -250,7 +249,6 @@ log4j-jdbc-jndi log4j-jndi log4j-jndi-test - log4j-jpl log4j-jul log4j-kit log4j-layout-template-json @@ -262,11 +260,7 @@ log4j-plugins log4j-plugins-test log4j-script - log4j-slf4j2-impl - log4j-slf4j-impl log4j-spring-cloud-config-client - log4j-to-jul - log4j-to-slf4j @@ -378,12 +372,6 @@ Modules that are not used anywhere (e.g., `log4j-core-its`, `log4j-osgi-test`) should not be placed here. Modules that are used only internally (e.g., `log4j-layout-template-json-test`) should be added to `dependencyManagement > dependencies` in `log4j-parent/pom.xml`. --> - - org.apache.logging.log4j - jul-to-log4j - ${project.version} - - org.apache.logging.log4j log4j-api @@ -501,7 +489,7 @@ org.apache.logging.log4j log4j-jpl - ${project.version} + ${log4j-api.version} @@ -555,13 +543,13 @@ org.apache.logging.log4j log4j-slf4j2-impl - ${project.version} + ${log4j-api.version} org.apache.logging.log4j log4j-slf4j-impl - ${project.version} + ${log4j-api.version} @@ -573,13 +561,13 @@ org.apache.logging.log4j log4j-to-jul - ${project.version} + ${log4j-api.version} org.apache.logging.log4j log4j-to-slf4j - ${project.version} + ${log4j-api.version}
diff --git a/src/changelog/.3.x.x/remove_bridges.xml b/src/changelog/.3.x.x/remove_bridges.xml new file mode 100644 index 00000000000..58fa7efa591 --- /dev/null +++ b/src/changelog/.3.x.x/remove_bridges.xml @@ -0,0 +1,9 @@ + + + + Remove logging bridges from distribution and reference their 2.x equivalents in `log4j-bom`. + + diff --git a/src/site/antora/modules/ROOT/pages/components.adoc b/src/site/antora/modules/ROOT/pages/components.adoc index dc718711481..f7ff0efc357 100644 --- a/src/site/antora/modules/ROOT/pages/components.adoc +++ b/src/site/antora/modules/ROOT/pages/components.adoc @@ -355,7 +355,8 @@ See xref:log4j-jul.adoc#bridge-handler[Using `j.u.l.Handler`] for more details. [IMPORTANT] ==== -Don't deploy this artifact together with <>. +Don't deploy this artifact together with +{log4j2-url}/components.html#log4j-to-jul[`log4j-to-jul`]. ==== include::partial$components/log4j-jul.adoc[] @@ -463,50 +464,6 @@ See xref:manual/scripts.adoc[] for more details. include::partial$components/log4j-script.adoc[] -[#log4j-slf4j2-impl] -== `log4j-slf4j2-impl` - -[cols="1h,5"] -|=== -| JPMS module -| `org.apache.logging.log4j.slf4j2.impl` -|=== - -The `log4j-slf4j2-impl` artifact contains a bridge from -https://www.slf4j.org/[SLF4J 2 API] -to the xref:manual/api.adoc[]. - -See xref:manual/installation.adoc#impl-core-bridge-slf4j[Installing the SLF4J-to-Log4j API bridge] for more details. - -[IMPORTANT] -==== -Don't deploy this artifact together with either <> or <>. -==== - -include::partial$components/log4j-slf4j2-impl.adoc[] - -[#log4j-slf4j-impl] -== `log4j-slf4j-impl` - -[cols="1h,5"] -|=== -| JPMS module -| `org.apache.logging.log4j.slf4j.impl` -|=== - -The `log4j-slf4j-impl` artifact contains a bridge from -https://www.slf4j.org/[SLF4J 1 API] -to the xref:manual/api.adoc[]. - -See xref:manual/installation.adoc#impl-core-bridge-slf4j[Installing the SLF4J-to-Log4j API bridge] for more details. - -[IMPORTANT] -==== -Don't deploy this artifact together with either <> or <>. -==== - -include::partial$components/log4j-slf4j-impl.adoc[] - [#log4j-spring-cloud-config-client] == `log4j-spring-cloud-config-client` @@ -523,47 +480,3 @@ or earlier versions. See xref:log4j-spring-cloud-config-client.adoc[] for more details. include::partial$components/log4j-spring-cloud-config-client.adoc[] - -[#log4j-to-jul] -== `log4j-to-jul` - -|=== -| JPMS module -| `org.apache.logging.log4j.to.jul` -|=== - -The `log4j-jul` artifact contains an implementation of the -xref:manual/api.adoc[] -that logs to -https://docs.oracle.com/javase/8/docs/api/java/util/logging/package-summary.html[`java.util.logging`]. - -See xref:manual/installation.adoc#impl-jul[Installing JUL] for more details. - -[IMPORTANT] -==== -Don't deploy this artifact together with <>. -==== - -include::partial$components/log4j-to-jul.adoc[] - -[#log4j-to-slf4j] -== `log4j-to-slf4j` - -|=== -| JPMS module -| `org.apache.logging.log4j.to.slf4j` -|=== - -The `log4j-jul` artifact contains an implementation of the -xref:manual/api.adoc[] -that logs to -https://www.slf4j.org/[SLF4J API]. - -See xref:manual/installation.adoc#impl-logback[Installing Logback] for more details. - -[IMPORTANT] -==== -Don't deploy this artifact together with either <> or <>. -==== - -include::partial$components/log4j-to-slf4j.adoc[] diff --git a/src/site/antora/modules/ROOT/pages/manual/installation.adoc b/src/site/antora/modules/ROOT/pages/manual/installation.adoc index 4d21ed7be5f..621a2522c00 100644 --- a/src/site/antora/modules/ROOT/pages/manual/installation.adoc +++ b/src/site/antora/modules/ROOT/pages/manual/installation.adoc @@ -231,14 +231,14 @@ The following sections explain the installation of Log4j-provided bridges. You can translate {slf4j-url}[SLF4J] calls to Log4j API using the `log4j-slf4j2-impl` artifact: -include::partial$components/log4j-slf4j2-impl.adoc[] +include::partial$features/log4j-slf4j2-impl.adoc[] .Are you still using SLF4J 1.x? [%collapsible] ===== Add this example instead: -include::partial$components/log4j-slf4j-impl.adoc[] +include::partial$features/log4j-slf4j-impl.adoc[] ===== [#impl-core-bridge-jul] @@ -465,6 +465,8 @@ Since it is embedded in the platform, it only requires the addition of bridges f Maven:: + +We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. ++ [source,xml,subs="+attributes"] ---- @@ -491,6 +493,8 @@ Maven:: Gradle:: + +We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. ++ [source,groovy,subs="+attributes"] ---- runtimeOnly 'org.apache.logging.log4j:log4j-to-jul' // Log4j-to-JUL bridge @@ -511,6 +515,8 @@ To install {logback-url}[Logback] as the logging implementation, you only need t Maven:: + +We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. ++ [source,xml] ---- @@ -535,6 +541,8 @@ Maven:: Gradle:: + +We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. ++ [source,groovy,subs="+attributes"] ---- runtimeOnly 'ch.qos.logback:logback-classic:{logback-version}' diff --git a/src/site/antora/modules/ROOT/partials/components/log4j-to-jul.adoc b/src/site/antora/modules/ROOT/partials/components/log4j-to-jul.adoc deleted file mode 100644 index 7f0e1b6dfbc..00000000000 --- a/src/site/antora/modules/ROOT/partials/components/log4j-to-jul.adoc +++ /dev/null @@ -1,41 +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. -//// - -[tabs] -==== -Maven:: -+ -We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. -+ -[source,xml,subs="+attributes"] ----- - - org.apache.logging.log4j - log4j-to-jul - runtime - ----- - -Gradle:: -+ -We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. -+ -[source,groovy,subs="+attributes"] ----- -runtimeOnly 'org.apache.logging.log4j:log4j-to-jul' ----- -==== \ No newline at end of file diff --git a/src/site/antora/modules/ROOT/partials/components/log4j-to-slf4j.adoc b/src/site/antora/modules/ROOT/partials/components/log4j-to-slf4j.adoc deleted file mode 100644 index 59221a0d3ff..00000000000 --- a/src/site/antora/modules/ROOT/partials/components/log4j-to-slf4j.adoc +++ /dev/null @@ -1,41 +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. -//// - -[tabs] -==== -Maven:: -+ -We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. -+ -[source,xml,subs="+attributes"] ----- - - org.apache.logging.log4j - log4j-to-slf4j - runtime - ----- - -Gradle:: -+ -We assume you use xref:components.adoc#log4j-bom[`log4j-bom`] for dependency management. -+ -[source,groovy,subs="+attributes"] ----- -runtimeOnly 'org.apache.logging.log4j:log4j-to-slf4j' ----- -==== \ No newline at end of file diff --git a/src/site/antora/modules/ROOT/partials/components/log4j-slf4j-impl.adoc b/src/site/antora/modules/ROOT/partials/features/log4j-slf4j-impl.adoc similarity index 100% rename from src/site/antora/modules/ROOT/partials/components/log4j-slf4j-impl.adoc rename to src/site/antora/modules/ROOT/partials/features/log4j-slf4j-impl.adoc diff --git a/src/site/antora/modules/ROOT/partials/components/log4j-slf4j2-impl.adoc b/src/site/antora/modules/ROOT/partials/features/log4j-slf4j2-impl.adoc similarity index 100% rename from src/site/antora/modules/ROOT/partials/components/log4j-slf4j2-impl.adoc rename to src/site/antora/modules/ROOT/partials/features/log4j-slf4j2-impl.adoc