tests pass except log4j-over-slf4j

This commit is contained in:
Ceki Gulcu 2017-03-23 23:41:47 +01:00
parent 99e3569748
commit 1c13755b25
78 changed files with 891 additions and 391 deletions

View File

@ -0,0 +1,4 @@
module org.slf4j.jcl_over_slf4j {
requires org.slf4j;
exports org.apache.commons.logging;
}

View File

@ -47,7 +47,7 @@ public class SLF4JLocationAwareLog implements Log, Serializable {
private static final String FQCN = SLF4JLocationAwareLog.class.getName();
SLF4JLocationAwareLog(LocationAwareLogger logger) {
public SLF4JLocationAwareLog(LocationAwareLogger logger) {
this.logger = logger;
this.name = logger.getName();
}

View File

@ -44,7 +44,7 @@ public class SLF4JLog implements Log, Serializable {
// logger instance is transient
private transient Logger logger;
SLF4JLog(Logger logger) {
public SLF4JLog(Logger logger) {
this.logger = logger;
this.name = logger.getName();
}

View File

@ -23,11 +23,14 @@
*
*/
package org.apache.commons.logging;
package org.apache.commons.logging.test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
public class InvokeJCLTest {

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.apache.commons.logging.impl;
package org.apache.commons.logging.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -32,10 +32,12 @@ import java.io.ObjectOutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.SLF4JLocationAwareLog;
import org.apache.commons.logging.impl.SLF4JLog;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.impl.JDK14LoggerFactory;
import org.slf4j.jul.JDK14LoggerFactory;
import org.slf4j.spi.LocationAwareLogger;
public class SerializationTest {

View File

@ -0,0 +1,8 @@
module org.slf4j.log4j_over_slf4j {
requires org.slf4j;
requires java.xml;
exports org.apache.log4j;
exports org.apache.log4j.helpers;
exports org.apache.log4j.spi;
exports org.apache.log4j.xml;
}

View File

@ -48,21 +48,25 @@ public class Priority {
/**
* @deprecated Use {@link Level#ERROR} instead.
*/
@Deprecated
final static public Priority ERROR = new Level(ERROR_INT, "ERROR", 3);
/**
* @deprecated Use {@link Level#WARN} instead.
*/
@Deprecated
final static public Priority WARN = new Level(WARN_INT, "WARN", 4);
/**
* @deprecated Use {@link Level#INFO} instead.
*/
@Deprecated
final static public Priority INFO = new Level(INFO_INT, "INFO", 6);
/**
* @deprecated Use {@link Level#DEBUG} instead.
*/
@Deprecated
final static public Priority DEBUG = new Level(DEBUG_INT, "DEBUG", 7);
/**
@ -122,6 +126,7 @@ public class Priority {
@deprecated This method will be removed with no replacement.
*/
@Deprecated
public static Priority[] getAllPossiblePriorities() {
return new Priority[] { Priority.FATAL, Priority.ERROR, Level.WARN, Priority.INFO, Priority.DEBUG };
}
@ -143,6 +148,7 @@ public class Priority {
/**
* @deprecated Please use the {@link Level#toLevel(String)} method instead.
*/
@Deprecated
public static Priority toPriority(String sArg) {
return Level.toLevel(sArg);
}
@ -150,6 +156,7 @@ public class Priority {
/**
* @deprecated Please use the {@link Level#toLevel(int)} method instead.
*/
@Deprecated
public static Priority toPriority(int val) {
return toPriority(val, Priority.DEBUG);
}
@ -157,6 +164,7 @@ public class Priority {
/**
* @deprecated Please use the {@link Level#toLevel(int, Level)} method instead.
*/
@Deprecated
public static Priority toPriority(int val, Priority defaultPriority) {
return Level.toLevel(val, (Level) defaultPriority);
}
@ -164,6 +172,7 @@ public class Priority {
/**
* @deprecated Please use the {@link Level#toLevel(String, Level)} method instead.
*/
@Deprecated
public static Priority toPriority(String sArg, Priority defaultPriority) {
return Level.toLevel(sArg, (Level) defaultPriority);
}

15
pom.xml
View File

@ -132,19 +132,6 @@
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
@ -201,7 +188,7 @@
<version>2.19.1</version>
<configuration>
<forkCount>2C</forkCount>
<reuseForks>true</reuseForks>
<reuseForks>false</reuseForks>
<reportFormat>plain</reportFormat>
<trimStackTrace>false</trimStackTrace>
<excludes>

View File

@ -149,8 +149,9 @@ public final class LoggerFactory {
List<SLF4JServiceProvider> providersList = findServiceProviders();
reportMultipleBindingAmbiguity(providersList);
if (providersList != null && !providersList.isEmpty()) {
PROVIDER = providersList.get(0);
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
PROVIDER = providersList.get(0);
PROVIDER.initialize();
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
reportActualBinding(providersList);
fixSubstituteLoggers();
replayEvents();

View File

@ -0,0 +1,31 @@
package org.slf4j;
/**
* All methods in this class are reserved for internal use, for testing purposes.
* <p/>
* <p/>They can can be modified, renamed or removed at any time without notice.
* <p/>
* You are strongly discouraged calling any of the methods of this class.
*
* @since 1.8.0
*
* @author Ceki G&uuml;lc&uuml;
*/
public class LoggerFactoryFriend {
/*
* Force LoggerFactory to consider itself uninitialized. <p/>
*/
static public void reset() {
LoggerFactory.reset();
}
/**
* Set LoggerFactory.DETECT_LOGGER_NAME_MISMATCH variable.
*
* @param enabled
*/
public static void setDetectLoggerNameMismatch(boolean enabled) {
LoggerFactory.DETECT_LOGGER_NAME_MISMATCH = enabled;
}
}

View File

@ -52,6 +52,7 @@ public class MarkerFactory {
static {
SLF4JServiceProvider provider = LoggerFactory.getProvider();
if (provider != null) {
provider.initialize();
MARKER_FACTORY = provider.getMarkerFactory();
} else {
Util.report("Failed to find provider");

View File

@ -2,39 +2,40 @@ package org.slf4j.helpers;
import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.helpers.BasicMarkerFactory;
import org.slf4j.helpers.NOPLoggerFactory;
import org.slf4j.helpers.NOPMDCAdapter;
import org.slf4j.spi.MDCAdapter;
import org.slf4j.spi.SLF4JServiceProvider;
public class NOPServiceProvider implements SLF4JServiceProvider {
/**
* Declare the version of the SLF4J API this implementation is compiled against.
* The value of this field is modified with each major release.
*/
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.8.99"; // !final
/**
* Declare the version of the SLF4J API this implementation is compiled
* against. The value of this field is modified with each major release.
*/
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.8.99"; // !final
private ILoggerFactory loggerFactory = new NOPLoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new NOPMDCAdapter();
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
private ILoggerFactory loggerFactory = new NOPLoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new NOPMDCAdapter();
public IMarkerFactory getMarkerFactory() {
return markerFactory;
}
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
public MDCAdapter getMDCAdapter() {
return mdcAdapter;
}
public IMarkerFactory getMarkerFactory() {
return markerFactory;
}
public String getRequesteApiVersion() {
return REQUESTED_API_VERSION;
}
public MDCAdapter getMDCAdapter() {
return mdcAdapter;
}
public String getRequesteApiVersion() {
return REQUESTED_API_VERSION;
}
@Override
public void initialize() {
// already initialized
}
}

View File

@ -9,7 +9,8 @@ public class SubstitureServiceProvider implements SLF4JServiceProvider {
private SubstituteLoggerFactory loggerFactory = new SubstituteLoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new BasicMDCAdapter();
@Override
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
@ -18,16 +19,25 @@ public class SubstitureServiceProvider implements SLF4JServiceProvider {
return loggerFactory;
}
@Override
public IMarkerFactory getMarkerFactory() {
return markerFactory;
}
@Override
public MDCAdapter getMDCAdapter() {
return mdcAdapter;
}
@Override
public String getRequesteApiVersion() {
throw new UnsupportedOperationException();
}
@Override
public void initialize() {
}
}

View File

@ -329,7 +329,7 @@ public class SubstituteLogger implements Logger {
* Return the delegate logger instance if set. Otherwise, return a {@link NOPLogger}
* instance.
*/
Logger delegate() {
public Logger delegate() {
if(_delegate != null) {
return _delegate;
}

View File

@ -29,4 +29,5 @@ public interface SLF4JServiceProvider {
public String getRequesteApiVersion();
public void initialize();
}

View File

@ -1,4 +1,4 @@
package org.slf4j.helpers;
package org.slf4j.testHarness;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -16,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerAccessingThread;
import org.slf4j.LoggerFactory;
import org.slf4j.event.EventRecodingLogger;
import org.slf4j.helpers.SubstituteLogger;
abstract public class MultithreadedInitializationTest {
final protected static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;

View File

@ -1,5 +1,7 @@
module org.slf4j.jdk14 {
module org.slf4j.jul {
requires org.slf4j;
requires org.slf4j.spi;
provides org.slf4j.spi.SLF4JServiceProvider with org.slf4j.impl.JULServiceProvider;
requires java.logging;
exports org.slf4j.jul;
provides org.slf4j.spi.SLF4JServiceProvider with org.slf4j.jul.JULServiceProvider;
}

View File

@ -25,6 +25,7 @@
package org.slf4j.impl;
import org.slf4j.ILoggerFactory;
import org.slf4j.jul.JDK14LoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;
/**
@ -60,7 +61,7 @@ public class StaticLoggerBinder implements LoggerFactoryBinder {
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.6.99"; // !final
private static final String loggerFactoryClassStr = org.slf4j.impl.JDK14LoggerFactory.class.getName();
private static final String LOGGER_FACTORY_CLASS_STR = JDK14LoggerFactory.class.getName();
/** The ILoggerFactory instance returned by the {@link #getLoggerFactory} method
* should always be the same object
@ -68,8 +69,7 @@ public class StaticLoggerBinder implements LoggerFactoryBinder {
private final ILoggerFactory loggerFactory;
private StaticLoggerBinder() {
// Note: JCL gets substituted at build time by an appropriate Ant task
loggerFactory = new org.slf4j.impl.JDK14LoggerFactory();
loggerFactory = new JDK14LoggerFactory();
}
public ILoggerFactory getLoggerFactory() {
@ -77,6 +77,6 @@ public class StaticLoggerBinder implements LoggerFactoryBinder {
}
public String getLoggerFactoryClassStr() {
return loggerFactoryClassStr;
return LOGGER_FACTORY_CLASS_STR;
}
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.jul;
import java.util.logging.Level;
import java.util.logging.LogRecord;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.jul;
import org.slf4j.Logger;
import org.slf4j.ILoggerFactory;

View File

@ -0,0 +1,44 @@
package org.slf4j.jul;
import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.helpers.BasicMDCAdapter;
import org.slf4j.helpers.BasicMarkerFactory;
import org.slf4j.spi.MDCAdapter;
import org.slf4j.spi.SLF4JServiceProvider;
public class JULServiceProvider implements SLF4JServiceProvider {
/**
* Declare the version of the SLF4J API this implementation is compiled
* against. The value of this field is modified with each major release.
*/
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.8.99"; // !final
private ILoggerFactory loggerFactory;
private IMarkerFactory markerFactory;
private MDCAdapter mdcAdapter;
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
public IMarkerFactory getMarkerFactory() {
return markerFactory;
}
public MDCAdapter getMDCAdapter() {
return mdcAdapter;
}
public String getRequesteApiVersion() {
return REQUESTED_API_VERSION;
}
public void initialize() {
loggerFactory = new JDK14LoggerFactory();
markerFactory = new BasicMarkerFactory();
mdcAdapter = new BasicMDCAdapter();
}
}

View File

@ -4,7 +4,7 @@ Bundle-SymbolicName: slf4j.jdk14
Bundle-Name: slf4j-jdk14
Bundle-Vendor: SLF4J.ORG
Bundle-RequiredExecutionEnvironment: J2SE-1.6
Export-Package: org.slf4j.impl;version=${parsedVersion.osgiVersion}
Export-Package: org.slf4j.jul;version=${parsedVersion.osgiVersion}
Import-Package: org.slf4j;version=${parsedVersion.osgiVersion},
org.slf4j.spi;version=${parsedVersion.osgiVersion},
org.slf4j.helpers;version=${parsedVersion.osgiVersion},

View File

@ -1 +1 @@
org.slf4j.impl.JULServiceProvider
org.slf4j.jul.JULServiceProvider

View File

@ -1,7 +0,0 @@
package org.slf4j;
public class LoggerFactoryFriend {
static public void reset() {
LoggerFactory.reset();
}
}

View File

@ -1,4 +1,4 @@
package org.slf4j.helpers;
package org.slf4j.jul;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Handler;

View File

@ -22,12 +22,13 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;
package org.slf4j.jul;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.*;
import java.util.ArrayList;
import java.util.List;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.jul;
import static org.junit.Assert.assertNotNull;
@ -34,6 +34,7 @@ import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.jul.JDK14LoggerFactory;
public class JDK14AdapterLoggerNameTest {
private MockHandler mockHandler;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.helpers;
package org.slf4j.jul;
import static org.junit.Assert.fail;
@ -30,6 +30,7 @@ import java.util.logging.Handler;
import org.junit.After;
import org.junit.Before;
import org.slf4j.testHarness.MultithreadedInitializationTest;
public class JDK14MultithreadedInitializationTest extends MultithreadedInitializationTest {

View File

@ -37,4 +37,29 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<reportFormat>plain</reportFormat>
<trimStackTrace>false</trimStackTrace>
<excludes>
<exclude>**/AllTest.java</exclude>
<exclude>**/PackageTest.java</exclude>
</excludes>
<argLine>-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8001</argLine>
<!--<argLine>XXadd-opens log4j/org.apache.log4j=org.slf4j.log4j12</argLine>-->
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,5 +0,0 @@
module org.slf4j.log4j12 {
requires org.slf4j;
requires org.slf4j.spi;
provides org.slf4j.spi.SLF4JServiceProvider with org.slf4j.impl.Log4j12ServiceProvider
}

View File

@ -3,31 +3,55 @@ package org.apache.log4j;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.log4j.MDC;
import org.apache.log4j.helpers.ThreadLocalMap;
public class MDCFriend {
public static void fixForJava9() {
if (MDC.mdc.tlm == null) {
MDC.mdc.tlm = new ThreadLocalMap();
MDC.mdc.java1 = false;
setRemoveMethod(MDC.mdc);
}
public static void fixForJava9() {
try {
Field mdcField = MDC.class.getDeclaredField("mdc");
}
MDC mdcSingleton = (MDC) mdcField.get(null);
Field tlmField = MDC.class.getDeclaredField("tlm");
private static void setRemoveMethod(MDC mdc) {
try {
Method removeMethod = ThreadLocal.class.getMethod("remove");
Field removeMethodField = MDC.class.getDeclaredField("removeMethod");
removeMethodField.setAccessible(true);
removeMethodField.set(mdc, removeMethod);
} catch (NoSuchMethodException e) {
} catch (SecurityException e) {
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
Field java1Field = MDC.class.getDeclaredField("java1");
Object mdcSingleton_tlm = tlmField.get(mdcSingleton);
}
if (mdcSingleton_tlm == null) {
tlmField.set(mdcSingleton, new ThreadLocalMap());
java1Field.setBoolean(mdcSingleton, false);
setRemoveMethod(mdcSingleton);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private static void setRemoveMethod(org.apache.log4j.MDC mdc) {
try {
Method removeMethod = ThreadLocal.class.getMethod("remove");
Field removeMethodField = MDC.class.getDeclaredField("removeMethod");
removeMethodField.setAccessible(true);
removeMethodField.set(mdc, removeMethod);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

View File

@ -1,98 +0,0 @@
/**
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.MDCFriend;
import org.slf4j.spi.MDCAdapter;
public class Log4jMDCAdapter implements MDCAdapter {
static {
if (VersionUtil.getJavaMajorVersion() >= 9) {
MDCFriend.fixForJava9();
}
}
public void clear() {
@SuppressWarnings("rawtypes")
Map map = org.apache.log4j.MDC.getContext();
if (map != null) {
map.clear();
}
}
public String get(String key) {
return (String) org.apache.log4j.MDC.get(key);
}
/**
* Put a context value (the <code>val</code> parameter) as identified with
* the <code>key</code> parameter into the current thread's context map. The
* <code>key</code> parameter cannot be null. Log4j does <em>not</em>
* support null for the <code>val</code> parameter.
*
* <p>
* This method delegates all work to log4j's MDC.
*
* @throws IllegalArgumentException
* in case the "key" or <b>"val"</b> parameter is null
*/
public void put(String key, String val) {
org.apache.log4j.MDC.put(key, val);
}
public void remove(String key) {
org.apache.log4j.MDC.remove(key);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map getCopyOfContextMap() {
Map old = org.apache.log4j.MDC.getContext();
if (old != null) {
return new HashMap(old);
} else {
return null;
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setContextMap(Map contextMap) {
Map old = org.apache.log4j.MDC.getContext();
if (old == null) {
Iterator entrySetIterator = contextMap.entrySet().iterator();
while (entrySetIterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) entrySetIterator.next();
org.apache.log4j.MDC.put((String) mapEntry.getKey(), mapEntry.getValue());
}
} else {
old.clear();
old.putAll(contextMap);
}
}
}

View File

@ -27,6 +27,7 @@ package org.slf4j.impl;
import org.apache.log4j.Level;
import org.slf4j.ILoggerFactory;
import org.slf4j.helpers.Util;
import org.slf4j.log4j12.Log4jLoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;
/**

View File

@ -24,6 +24,7 @@
*/
package org.slf4j.impl;
import org.slf4j.log4j12.Log4jMDCAdapter;
import org.slf4j.spi.MDCAdapter;
/**

View File

@ -1,33 +0,0 @@
package org.slf4j.impl;
import java.lang.reflect.Method;
import org.slf4j.helpers.Util;
public class VersionUtil {
static final int MINIMAL_VERSION = 5;
static public int getJavaMajorVersion() {
String javaVersionString = Util.safeGetSystemProperty("java.version");
return getJavaMajorVersion(javaVersionString);
}
static int getJavaMajorVersion(String versionString) {
if (versionString == null)
return MINIMAL_VERSION;
if (versionString.startsWith("1.")) {
return versionString.charAt(2) - '0';
} else {
// we running under Java 9 or later
try {
Method versionMethod = Runtime.class.getMethod("version");
Object versionObj = versionMethod.invoke(null);
Method majorMethod = versionObj.getClass().getMethod("major");
Integer resultInteger = (Integer) majorMethod.invoke(versionObj);
return resultInteger.intValue();
} catch (Exception e) {
return MINIMAL_VERSION;
}
}
}
}

View File

@ -1,4 +1,4 @@
package org.slf4j.impl;
package org.slf4j.log4j12;
import org.apache.log4j.Level;
import org.slf4j.ILoggerFactory;
@ -17,10 +17,10 @@ public class Log4j12ServiceProvider implements SLF4JServiceProvider {
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.8.99"; // !final
private ILoggerFactory loggerFactory = new Log4jLoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new Log4jMDCAdapter();
private ILoggerFactory loggerFactory;
private IMarkerFactory markerFactory;
private MDCAdapter mdcAdapter;
public Log4j12ServiceProvider() {
try {
@SuppressWarnings("unused")
@ -30,6 +30,13 @@ public class Log4j12ServiceProvider implements SLF4JServiceProvider {
}
}
@Override
public void initialize() {
loggerFactory = new Log4jLoggerFactory();
markerFactory = new BasicMarkerFactory();
mdcAdapter = new Log4jMDCAdapter();
}
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.log4j12;
import static org.slf4j.event.EventConstants.NA_SUBST;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.log4j12;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -64,6 +64,7 @@ public class Log4jLoggerFactory implements ILoggerFactory {
loggerMap = new ConcurrentHashMap<String, Logger>();
// force log4j to initialize
org.apache.log4j.LogManager.getRootLogger();
System.out.println("*********post initializaiton");
}
/*

View File

@ -0,0 +1,98 @@
/**
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.log4j12;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.MDCFriend;
import org.slf4j.spi.MDCAdapter;
public class Log4jMDCAdapter implements MDCAdapter {
static {
if (VersionUtil.getJavaMajorVersion() >= 9) {
MDCFriend.fixForJava9();
}
}
public void clear() {
@SuppressWarnings("rawtypes")
Map map = org.apache.log4j.MDC.getContext();
if (map != null) {
map.clear();
}
}
public String get(String key) {
return (String) org.apache.log4j.MDC.get(key);
}
/**
* Put a context value (the <code>val</code> parameter) as identified with
* the <code>key</code> parameter into the current thread's context map. The
* <code>key</code> parameter cannot be null. Log4j does <em>not</em>
* support null for the <code>val</code> parameter.
*
* <p>
* This method delegates all work to log4j's MDC.
*
* @throws IllegalArgumentException
* in case the "key" or <b>"val"</b> parameter is null
*/
public void put(String key, String val) {
org.apache.log4j.MDC.put(key, val);
}
public void remove(String key) {
org.apache.log4j.MDC.remove(key);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Map getCopyOfContextMap() {
Map old = org.apache.log4j.MDC.getContext();
if (old != null) {
return new HashMap(old);
} else {
return null;
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setContextMap(Map contextMap) {
Map old = org.apache.log4j.MDC.getContext();
if (old == null) {
Iterator entrySetIterator = contextMap.entrySet().iterator();
while (entrySetIterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) entrySetIterator.next();
org.apache.log4j.MDC.put((String) mapEntry.getKey(), mapEntry.getValue());
}
} else {
old.clear();
old.putAll(contextMap);
}
}
}

View File

@ -0,0 +1,57 @@
package org.slf4j.log4j12;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.apache.log4j.MDC;
import org.apache.log4j.helpers.ThreadLocalMap;
public class MDCFriend {
public static void fixForJava9() {
try {
Field mdcField = MDC.class.getDeclaredField("mdc");
MDC mdcSingleton = (MDC) mdcField.get(null);
Field tlmField = MDC.class.getDeclaredField("tlm");
Field java1Field = MDC.class.getDeclaredField("java1");
Object mdcSingleton_tlm = tlmField.get(mdcSingleton);
if (mdcSingleton_tlm == null) {
tlmField.set(mdcSingleton_tlm, new ThreadLocalMap());
java1Field.setBoolean(mdcSingleton_tlm, false);
setRemoveMethod(mdcSingleton);
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private static void setRemoveMethod(org.apache.log4j.MDC mdc) {
try {
Method removeMethod = ThreadLocal.class.getMethod("remove");
Field removeMethodField = MDC.class.getDeclaredField("removeMethod");
removeMethodField.setAccessible(true);
removeMethodField.set(mdc, removeMethod);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,33 @@
package org.slf4j.log4j12;
import java.lang.reflect.Method;
import org.slf4j.helpers.Util;
public class VersionUtil {
static final int MINIMAL_VERSION = 5;
static public int getJavaMajorVersion() {
String javaVersionString = Util.safeGetSystemProperty("java.version");
return getJavaMajorVersion(javaVersionString);
}
static public int getJavaMajorVersion(String versionString) {
if (versionString == null)
return MINIMAL_VERSION;
if (versionString.startsWith("1.")) {
return versionString.charAt(2) - '0';
} else {
// we running under Java 9 or later
try {
Method versionMethod = Runtime.class.getMethod("version");
Object versionObj = versionMethod.invoke(null);
Method majorMethod = versionObj.getClass().getMethod("major");
Integer resultInteger = (Integer) majorMethod.invoke(versionObj);
return resultInteger.intValue();
} catch (Exception e) {
return MINIMAL_VERSION;
}
}
}
}

View File

@ -4,7 +4,7 @@ Bundle-SymbolicName: slf4j.log4j12
Bundle-Name: slf4j-log4j12
Bundle-Vendor: SLF4J.ORG
Bundle-RequiredExecutionEnvironment: J2SE-1.6
Export-Package: org.slf4j.impl;version=${parsedVersion.osgiVersion}
Export-Package: org.slf4j.log4j12;version=${parsedVersion.osgiVersion}
Import-Package: org.slf4j;version=${parsedVersion.osgiVersion},
org.slf4j.spi;version=${parsedVersion.osgiVersion},
org.slf4j.helpers;version=${parsedVersion.osgiVersion},

View File

@ -1 +1 @@
org.slf4j.impl.Log4j12ServiceProvider
org.slf4j.log4j12.Log4j12ServiceProvider

View File

@ -1,32 +0,0 @@
package org.apache.log4j;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Random;
import org.junit.Test;
import org.slf4j.impl.VersionUtil;
public class MDCFriendTest {
private static Random random = new Random();
int diff = random.nextInt(1024*8);
@Test
public void smoke() {
if(VersionUtil.getJavaMajorVersion() < 9)
return;
MDCFriend.fixForJava9();
String key = "MDCFriendTest.smoke"+diff;
String val = "val"+diff;
MDC.put(key, val);
assertEquals(val, MDC.get(key));
MDC.clear();
assertNull(MDC.get(key));
}
}

View File

@ -1,24 +0,0 @@
package org.slf4j.impl;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
public class UtilVersionTest {
@Test
public void test() {
System.out.println(System.getProperty("java.version"));
assertEquals(6, VersionUtil.getJavaMajorVersion("1.6"));
assertEquals(7, VersionUtil.getJavaMajorVersion("1.7.0_21-b11"));
assertEquals(8, VersionUtil.getJavaMajorVersion("1.8.0_25"));
}
@Ignore
@Test // requires Java 9 to pass
public void testJava9() {
assertEquals(9, VersionUtil.getJavaMajorVersion("9ea"));
}
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;
package org.slf4j.log4j12;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -36,6 +36,11 @@ import org.apache.log4j.spi.LoggingEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
/**
* Test whether invoking the SLF4J API causes problems or not.

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;
package org.slf4j.log4j12;
import java.util.ArrayList;
import java.util.List;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.log4j12;
import java.util.List;
@ -32,9 +32,10 @@ import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.assertNotNull;
import org.slf4j.helpers.MultithreadedInitializationTest;
import org.slf4j.LoggerFactoryFriend;
import org.slf4j.log4j12.testHarness.RecursiveAppender;
public class Log4j12MultithreadedInitializationTest extends MultithreadedInitializationTest {
public class Log4j12MultithreadedInitializationTest extends org.slf4j.testHarness.MultithreadedInitializationTest {
static int NUM_LINES_BY_RECURSIVE_APPENDER = 3;
// value of LogManager.DEFAULT_CONFIGURATION_KEY;
@ -64,7 +65,6 @@ public class Log4j12MultithreadedInitializationTest extends MultithreadedInitial
private List<LoggingEvent> getRecordedEvents() {
org.apache.log4j.Logger root = LogManager.getRootLogger();
RecursiveAppender ra = (RecursiveAppender) root.getAppender("RECURSIVE");
assertNotNull(ra);
return ra.events;

View File

@ -0,0 +1,33 @@
package org.slf4j.log4j12;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import java.util.Random;
import org.apache.log4j.MDC;
import org.apache.log4j.MDCFriend;
import org.junit.Test;
import org.slf4j.log4j12.VersionUtil;
public class MDCFriendTest {
private static Random random = new Random();
int diff = random.nextInt(1024 * 8);
@Test
public void smoke() {
if (VersionUtil.getJavaMajorVersion() < 9)
return;
MDCFriend.fixForJava9();
String key = "MDCFriendTest.smoke" + diff;
String val = "val" + diff;
MDC.put(key, val);
assertEquals(val, MDC.get(key));
MDC.clear();
assertNull(MDC.get(key));
}
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.log4j12;
import java.util.Random;

View File

@ -0,0 +1,24 @@
package org.slf4j.log4j12;
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
public class UtilVersionTest {
@Test
public void test() {
System.out.println(System.getProperty("java.version"));
assertEquals(6, VersionUtil.getJavaMajorVersion("1.6"));
assertEquals(7, VersionUtil.getJavaMajorVersion("1.7.0_21-b11"));
assertEquals(8, VersionUtil.getJavaMajorVersion("1.8.0_25"));
}
@Ignore
@Test // requires Java 9 to pass
public void testJava9() {
assertEquals(9, VersionUtil.getJavaMajorVersion("9ea"));
}
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.log4j12.testHarness;
import java.util.ArrayList;
import java.util.List;
@ -39,10 +39,10 @@ public class RecursiveAppender extends AppenderSkeleton {
int activationDelay = 0;
String loggerName = "org.slf4j.impl.RecursiveAppender" + diff;
List<LoggingEvent> events = new ArrayList<LoggingEvent>();
public List<LoggingEvent> events = new ArrayList<LoggingEvent>();
public RecursiveAppender() {
System.out.println("entering RecursiveAppender constructor");
System.out.println("XXXXXXX entering RecursiveAppender constructor");
Logger logger = LoggerFactory.getLogger(loggerName);
logger.info("Calling a logger in the constructor");
System.out.println("exiting RecursiveAppender constructor");

View File

@ -5,4 +5,5 @@ log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d [%t] %c - %m%n
log4j.appender.RECURSIVE=org.slf4j.impl.RecursiveAppender
log4j.appender.RECURSIVE=org.slf4j.log4j12.testHarness.RecursiveAppender

View File

@ -5,5 +5,5 @@ log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=CON %d [%t] %c - %m%n
log4j.appender.RECURSIVE=org.slf4j.impl.RecursiveAppender
log4j.appender.RECURSIVE.activationDelay=10
log4j.appender.RECURSIVE=org.slf4j.log4j12.testHarness.RecursiveAppender
log4j.appender.RECURSIVE.activationDelay=10

View File

@ -1,5 +1,4 @@
module org.slf4j.nop {
requires org.slf4j;
requires org.slf4j.spi;
provides org.slf4j.spi.SLF4JServiceProvider with org.slf4j.helpers.NOPServiceProvider;
provides org.slf4j.spi.SLF4JServiceProvider with org.slf4j.nop.NOPServiceProvider;
}

View File

@ -0,0 +1,220 @@
/**
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.nop;
import org.slf4j.Logger;
import org.slf4j.helpers.MarkerIgnoringBase;
/**
* A direct NOP (no operation) implementation of {@link Logger}.
*
* @author Ceki G&uuml;lc&uuml;
*/
public class NOPLogger extends MarkerIgnoringBase {
private static final long serialVersionUID = -517220405410904473L;
/**
* The unique instance of NOPLogger.
*/
public static final NOPLogger NOP_LOGGER = new NOPLogger();
/**
* There is no point in creating multiple instances of NOPLogger,
* except by derived classes, hence the protected access for the constructor.
*/
protected NOPLogger() {
}
/**
* Always returns the string value "NOP".
*/
public String getName() {
return "NOP";
}
/**
* Always returns false.
* @return always false
*/
final public boolean isTraceEnabled() {
return false;
}
/** A NOP implementation. */
final public void trace(String msg) {
// NOP
}
/** A NOP implementation. */
final public void trace(String format, Object arg) {
// NOP
}
/** A NOP implementation. */
public final void trace(String format, Object arg1, Object arg2) {
// NOP
}
/** A NOP implementation. */
public final void trace(String format, Object... argArray) {
// NOP
}
/** A NOP implementation. */
final public void trace(String msg, Throwable t) {
// NOP
}
/**
* Always returns false.
* @return always false
*/
final public boolean isDebugEnabled() {
return false;
}
/** A NOP implementation. */
final public void debug(String msg) {
// NOP
}
/** A NOP implementation. */
final public void debug(String format, Object arg) {
// NOP
}
/** A NOP implementation. */
public final void debug(String format, Object arg1, Object arg2) {
// NOP
}
/** A NOP implementation. */
public final void debug(String format, Object... argArray) {
// NOP
}
/** A NOP implementation. */
final public void debug(String msg, Throwable t) {
// NOP
}
/**
* Always returns false.
* @return always false
*/
final public boolean isInfoEnabled() {
// NOP
return false;
}
/** A NOP implementation. */
final public void info(String msg) {
// NOP
}
/** A NOP implementation. */
final public void info(String format, Object arg1) {
// NOP
}
/** A NOP implementation. */
final public void info(String format, Object arg1, Object arg2) {
// NOP
}
/** A NOP implementation. */
public final void info(String format, Object... argArray) {
// NOP
}
/** A NOP implementation. */
final public void info(String msg, Throwable t) {
// NOP
}
/**
* Always returns false.
* @return always false
*/
final public boolean isWarnEnabled() {
return false;
}
/** A NOP implementation. */
final public void warn(String msg) {
// NOP
}
/** A NOP implementation. */
final public void warn(String format, Object arg1) {
// NOP
}
/** A NOP implementation. */
final public void warn(String format, Object arg1, Object arg2) {
// NOP
}
/** A NOP implementation. */
public final void warn(String format, Object... argArray) {
// NOP
}
/** A NOP implementation. */
final public void warn(String msg, Throwable t) {
// NOP
}
/** A NOP implementation. */
final public boolean isErrorEnabled() {
return false;
}
/** A NOP implementation. */
final public void error(String msg) {
// NOP
}
/** A NOP implementation. */
final public void error(String format, Object arg1) {
// NOP
}
/** A NOP implementation. */
final public void error(String format, Object arg1, Object arg2) {
// NOP
}
/** A NOP implementation. */
public final void error(String format, Object... argArray) {
// NOP
}
/** A NOP implementation. */
final public void error(String msg, Throwable t) {
// NOP
}
}

View File

@ -22,45 +22,27 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.nop;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.BogoPerf;
import org.slf4j.helpers.NOPLogger;
@Ignore
public class PerfTest {
/**
* NOPLoggerFactory is an trivial implementation of {@link
* ILoggerFactory} which always returns the unique instance of
* NOPLogger.
*
* @author Ceki G&uuml;lc&uuml;
*/
public class NOPLoggerFactory implements ILoggerFactory {
static long REFERENCE_BIPS = 9000;
@Test
public void issue63() {
int LEN = 1000 * 1000 * 10;
debugLoop(LEN); // warm up
double avg = debugLoop(LEN);
long referencePerf = 93;
BogoPerf.assertDuration(avg, referencePerf, REFERENCE_BIPS);
// when the code is guarded by a logger.isLoggable condition,
// duration is about 16 *micro*seconds for 1000 iterations
// when it is not guarded the figure is 90 milliseconds,
// i.e. a ration of 1 to 5000
public NOPLoggerFactory() {
// nothing to do
}
double debugLoop(int len) {
Logger logger = LoggerFactory.getLogger(PerfTest.class);
long start = System.currentTimeMillis();
for (int i = 0; i < len; i++) {
logger.debug("hello");
}
long end = System.currentTimeMillis();
long duration = end - start;
return duration;
public Logger getLogger(String name) {
return NOPLogger.NOP_LOGGER;
}
}

View File

@ -0,0 +1,63 @@
/**
* Copyright (c) 2004-2011 QOS.ch
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.nop;
import java.util.Map;
import org.slf4j.spi.MDCAdapter;
/**
* This adapter is an empty implementation of the {@link MDCAdapter} interface.
* It is used for all logging systems which do not support mapped
* diagnostic contexts such as JDK14, simple and NOP.
*
* @author Ceki G&uuml;lc&uuml;
*
* @since 1.4.1
*/
public class NOPMDCAdapter implements MDCAdapter {
public void clear() {
}
public String get(String key) {
return null;
}
public void put(String key, String val) {
}
public void remove(String key) {
}
public Map<String, String> getCopyOfContextMap() {
return null;
}
public void setContextMap(Map<String, String> contextMap) {
// NOP
}
}

View File

@ -1,13 +1,14 @@
package org.slf4j.impl;
package org.slf4j.nop;
import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.helpers.BasicMarkerFactory;
import org.slf4j.helpers.NOPLoggerFactory;
import org.slf4j.helpers.NOPMDCAdapter;
import org.slf4j.spi.MDCAdapter;
import org.slf4j.spi.SLF4JServiceProvider;
public class SimpleServiceProvider implements SLF4JServiceProvider {
public class NOPServiceProvider implements SLF4JServiceProvider {
/**
* Declare the version of the SLF4J API this implementation is compiled against.
@ -16,10 +17,10 @@ public class SimpleServiceProvider implements SLF4JServiceProvider {
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.8.99"; // !final
private ILoggerFactory loggerFactory = new SimpleLoggerFactory();
private ILoggerFactory loggerFactory = new NOPLoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new NOPMDCAdapter();
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
@ -36,4 +37,7 @@ public class SimpleServiceProvider implements SLF4JServiceProvider {
return REQUESTED_API_VERSION;
}
public void initialize() {
}
}

View File

@ -4,7 +4,7 @@ Bundle-SymbolicName: slf4j.nop
Bundle-Name: slf4j-nop
Bundle-Vendor: SLF4J.ORG
Bundle-RequiredExecutionEnvironment: J2SE-1.6
Export-Package: org.slf4j.impl;version=${parsedVersion.osgiVersion}
Export-Package: org.slf4j.nop;version=${parsedVersion.osgiVersion}
Import-Package: org.slf4j;version=${parsedVersion.osgiVersion},
org.slf4j.spi;version=${parsedVersion.osgiVersion},
org.slf4j.helpers;version=${parsedVersion.osgiVersion},

View File

@ -1,7 +0,0 @@
package org.slf4j;
public class LoggerFactoryFriend {
static public void reset() {
LoggerFactory.reset();
}
}

View File

@ -22,11 +22,16 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;
package org.slf4j.nop;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
/**
* Test whether invoking the SLF4J API causes problems or not.

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.nop;
import static org.junit.Assert.assertEquals;

View File

@ -1,5 +1,4 @@
module org.slf4j.simple {
requires org.slf4j;
requires org.slf4j.spi;
provides org.slf4j.spi.SLF4JServiceProvider with org.slf4j.impl.SimpleServiceProvider;
provides org.slf4j.spi.SLF4JServiceProvider with org.slf4j.simple.SimpleServiceProvider;
}

View File

@ -26,6 +26,7 @@ package org.slf4j.impl;
import org.slf4j.ILoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;
import org.slf4j.simple.SimpleLoggerFactory;
/**
* As of SLF4J version 1.8.0, the static binder mechanism is deprecated.

View File

@ -1,4 +1,4 @@
package org.slf4j.impl;
package org.slf4j.simple;
import java.io.PrintStream;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.simple;
import java.io.PrintStream;
import java.util.Date;

View File

@ -1,4 +1,4 @@
package org.slf4j.impl;
package org.slf4j.simple;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@ -11,7 +11,7 @@ import java.text.SimpleDateFormat;
import java.util.Properties;
import org.slf4j.helpers.Util;
import org.slf4j.impl.OutputChoice.OutputChoiceType;
import org.slf4j.simple.OutputChoice.OutputChoiceType;
/**
* This class holds configuration values for {@link SimpleLogger}. The

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.simple;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

View File

@ -1,13 +1,13 @@
package org.slf4j.impl;
package org.slf4j.simple;
import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.helpers.BasicMDCAdapter;
import org.slf4j.helpers.BasicMarkerFactory;
import org.slf4j.helpers.NOPMDCAdapter;
import org.slf4j.spi.MDCAdapter;
import org.slf4j.spi.SLF4JServiceProvider;
public class JULServiceProvider implements SLF4JServiceProvider {
public class SimpleServiceProvider implements SLF4JServiceProvider {
/**
* Declare the version of the SLF4J API this implementation is compiled against.
@ -16,10 +16,10 @@ public class JULServiceProvider implements SLF4JServiceProvider {
// to avoid constant folding by the compiler, this field must *not* be final
public static String REQUESTED_API_VERSION = "1.8.99"; // !final
private ILoggerFactory loggerFactory = new org.slf4j.impl.JDK14LoggerFactory();
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new BasicMDCAdapter();
private ILoggerFactory loggerFactory;
private IMarkerFactory markerFactory;
private MDCAdapter mdcAdapter;
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
@ -35,4 +35,14 @@ public class JULServiceProvider implements SLF4JServiceProvider {
public String getRequesteApiVersion() {
return REQUESTED_API_VERSION;
}
@Override
public void initialize() {
loggerFactory = new SimpleLoggerFactory();
markerFactory = new BasicMarkerFactory();
mdcAdapter = new NOPMDCAdapter();
}
}

View File

@ -5,7 +5,7 @@ Bundle-Name: slf4j-simple
Bundle-Vendor: SLF4J.ORG
Require-Bundle: slf4j.api
Bundle-RequiredExecutionEnvironment: J2SE-1.6
Export-Package: org.slf4j.impl;version=${parsedVersion.osgiVersion}
Export-Package: org.slf4j.simple;version=${parsedVersion.osgiVersion}
Import-Package: org.slf4j;version=${parsedVersion.osgiVersion},
org.slf4j.spi;version=${parsedVersion.osgiVersion},
org.slf4j.helpers;version=${parsedVersion.osgiVersion},

View File

@ -1 +1 @@
org.slf4j.impl.SimpleServiceProvider
org.slf4j.simple.SimpleServiceProvider

View File

@ -1,7 +0,0 @@
package org.slf4j;
public class LoggerFactoryFriend {
static public void reset() {
LoggerFactory.reset();
}
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;
package org.slf4j.simple;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -30,6 +30,9 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.LoggerFactoryFriend;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -48,6 +51,8 @@ public class DetectLoggerNameMismatchTest {
private static final String MISMATCH_STRING = "Detected logger name mismatch";
static String NAME_OF_THIS_CLASS = DetectLoggerNameMismatchTest.class.getName();
private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
private final PrintStream oldErr = System.err;
@ -90,8 +95,8 @@ public class DetectLoggerNameMismatchTest {
public void testTriggerWholeMessage() {
setTrialEnabled(true);
LoggerFactory.getLogger(String.class);
boolean success = String.valueOf(byteArrayOutputStream).contains(
"Detected logger name mismatch. Given name: \"java.lang.String\"; " + "computed name: \"org.slf4j.DetectLoggerNameMismatchTest\".");
boolean success = String.valueOf(byteArrayOutputStream)
.contains("Detected logger name mismatch. Given name: \"java.lang.String\"; " + "computed name: \"" + NAME_OF_THIS_CLASS + "\".");
assertTrue("Actual value of byteArrayOutputStream: " + String.valueOf(byteArrayOutputStream), success);
}
@ -102,7 +107,7 @@ public class DetectLoggerNameMismatchTest {
public void testPassIfMatch() {
setTrialEnabled(true);
Logger logger = LoggerFactory.getLogger(DetectLoggerNameMismatchTest.class);
assertEquals("org.slf4j.DetectLoggerNameMismatchTest", logger.getName());
assertEquals(DetectLoggerNameMismatchTest.class.getName(), logger.getName());
assertMismatchDetected(false);
}
@ -114,7 +119,7 @@ public class DetectLoggerNameMismatchTest {
public void verifyLoggerDefinedInBaseWithOverridenGetClassMethod() {
setTrialEnabled(true);
Square square = new Square();
assertEquals("org.slf4j.Square", square.logger.getName());
assertEquals(Square.class.getName(), square.logger.getName());
assertMismatchDetected(false);
}
@ -122,7 +127,7 @@ public class DetectLoggerNameMismatchTest {
// The system property is read into a static variable at initialization time
// so we cannot just reset the system property to test this feature.
// Therefore we set the variable directly.
LoggerFactory.DETECT_LOGGER_NAME_MISMATCH = enabled;
LoggerFactoryFriend.setDetectLoggerNameMismatch(enabled);
}
}

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;
package org.slf4j.simple;
import static org.junit.Assert.assertNull;
@ -31,6 +31,11 @@ import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
/**
* Test whether invoking the SLF4J API causes problems or not.

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j;
package org.slf4j.simple;
import java.io.PrintStream;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.helpers;
package org.slf4j.simple;
import java.io.PrintStream;
import java.util.ArrayList;
@ -32,7 +32,8 @@ import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.slf4j.LoggerFactoryFriend;
import org.slf4j.impl.SimpleLogger;
import org.slf4j.testHarness.MultithreadedInitializationTest;
import org.slf4j.simple.SimpleLogger;
public class SimpleLoggerMultithreadedInitializationTest extends MultithreadedInitializationTest {
// final static int THREAD_COUNT = 4 + Runtime.getRuntime().availableProcessors() * 2;

View File

@ -22,7 +22,7 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.slf4j.impl;
package org.slf4j.simple;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -36,6 +36,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.simple.*;
public class SimpleLoggerTest {
String A_KEY = SimpleLogger.LOG_KEY_PREFIX + "a";
@ -104,7 +105,7 @@ public class SimpleLoggerTest {
System.setErr(replacement);
simpleLogger.info("hello");
replacement.flush();
assertTrue(bout.toString().contains("INFO org.slf4j.impl.SimpleLoggerTest - hello"));
assertTrue(bout.toString().contains("INFO "+this.getClass().getName()+" - hello"));
}
@Test
@ -118,6 +119,6 @@ public class SimpleLoggerTest {
simpleLogger.info("hello");
replacement.flush();
assertTrue(bout.toString().contains("INFO org.slf4j.impl.SimpleLoggerTest - hello"));
assertTrue(bout.toString().contains("INFO "+this.getClass().getName()+" - hello"));
}
}