diff --git a/log4j-core-test/pom.xml b/log4j-core-test/pom.xml index 47ca1a22d8c..c2cff70fe67 100644 --- a/log4j-core-test/pom.xml +++ b/log4j-core-test/pom.xml @@ -235,13 +235,6 @@ test - - - log4j - log4j - test - - com.github.ivandzf log4j2-custom-layout @@ -282,13 +275,6 @@ test - - - org.slf4j - slf4j-api - test - - org.graalvm.truffle truffle-api 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-mongodb/pom.xml b/log4j-mongodb/pom.xml index 45620a23c67..b77b1a61da7 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 2cce92c1fbf..c64066df10b 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 @@ -125,10 +124,8 @@ 4.13.2 5.10.3 2.2.0 - 1.2.17 1.1.0 0.18 - 1.4.14 3.9.9 5.13.0 15.4 @@ -140,7 +137,6 @@ 1.0.1 4.13.5 3.5.1 - 2.0.15 2.1.7 10.1.30 1.7 @@ -290,12 +286,6 @@ ${commons-lang3.version} - - commons-logging - commons-logging - ${commons-logging.version} - - org.apache.commons commons-pool2 @@ -433,12 +423,6 @@ ${junit-pioneer.version} - - log4j - log4j - ${log4j.version} - - com.github.ivandzf @@ -459,32 +443,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 @@ -575,12 +533,6 @@ ${plexus-utils.version} - - org.slf4j - slf4j-api - ${slf4j2.version} - - uk.org.webcompere system-stubs-core diff --git a/log4j-perf-test/pom.xml b/log4j-perf-test/pom.xml index aadc3c8933d..0f2516dbd3f 100644 --- a/log4j-perf-test/pom.xml +++ b/log4j-perf-test/pom.xml @@ -37,22 +37,68 @@ true true org.apache.logging.log4j.perf + + + 1.2.25 + 1.6.0 + 1.4.14 + 2.0.16 + + + + + co.elastic.logging + log4j2-ecs-layout + ${log4j2-ecs-layout.version} + + + + ch.qos.logback + logback-classic + ${logback.version} + + + + ch.qos.logback + logback-core + ${logback.version} + + + + ch.qos.reload4j + reload4j + ${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 @@ -61,71 +107,90 @@ 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.reload4j + reload4j + + - log4j - log4j + co.elastic.logging + log4j2-ecs-layout + ch.qos.logback logback-classic - compile + ch.qos.logback logback-core - compile + org.slf4j slf4j-api + @@ -158,6 +223,7 @@ ch.qos.logback:* + ch.qos.reload4j:* @@ -165,6 +231,7 @@ + org.apache.maven.plugins maven-shade-plugin @@ -181,6 +248,10 @@ ${uberjar.name} + + + META-INF/sisu/javax.inject.Named + org.openjdk.jmh.Main @@ -191,15 +262,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 7019645401f..00000000000 --- a/log4j-slf4j2-impl/pom.xml +++ /dev/null @@ -1,158 +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.) - - - - - false - - - org.slf4j;substitute="slf4j-api" - - - - - 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 92290cead6b..4fadc7f8124 100644 --- a/log4j-spring-cloud-config-client/pom.xml +++ b/log4j-spring-cloud-config-client/pom.xml @@ -34,6 +34,7 @@ + 1.3.4 3.3.3 4.1.4 6.1.13 @@ -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 413fde7a59d..00000000000 --- a/log4j-to-slf4j/pom.xml +++ /dev/null @@ -1,150 +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 - - - - - 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 dc412611b06..5392bb209bc 100644 --- a/pom.xml +++ b/pom.xml @@ -261,11 +261,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 @@ -545,13 +541,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} @@ -563,13 +559,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/site/antora/modules/ROOT/pages/components.adoc b/src/site/antora/modules/ROOT/pages/components.adoc index 4b99ac7335e..7a4f9a51ad0 100644 --- a/src/site/antora/modules/ROOT/pages/components.adoc +++ b/src/site/antora/modules/ROOT/pages/components.adoc @@ -330,7 +330,8 @@ See xref:manual/installation.adoc#impl-core-bridge-jul[Installing the JUL-to-Log [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[] @@ -438,50 +439,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` @@ -498,47 +455,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 719e097fb2d..2fec226a38f 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