Skip to content

Commit

Permalink
Merge branch 'master' of github.com:qos-ch/slf4j
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Apr 29, 2016
2 parents 19bfd0f + c246302 commit 1c6cbf0
Show file tree
Hide file tree
Showing 27 changed files with 309 additions and 220 deletions.
2 changes: 1 addition & 1 deletion integration/pom.xml
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>
</parent>

<artifactId>integration</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion jcl-over-slf4j/pom.xml
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion jul-to-slf4j/pom.xml
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>
</parent>

<artifactId>jul-to-slf4j</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion log4j-over-slf4j/pom.xml
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>
</parent>


Expand Down
2 changes: 1 addition & 1 deletion osgi-over-slf4j/pom.xml
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>
</parent>

<artifactId>osgi-over-slf4j</artifactId>
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>

<packaging>pom</packaging>
<name>SLF4J</name>
Expand Down Expand Up @@ -74,6 +74,7 @@
<module>integration</module>
<module>slf4j-site</module>
<module>slf4j-migrator</module>
<module>slf4j-bom</module>
</modules>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion slf4j-android/pom.xml
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>
</parent>

<artifactId>slf4j-android</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion slf4j-api/pom.xml
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-parent</artifactId>
<version>1.7.21-SNAPSHOT</version>
<version>1.7.22-SNAPSHOT</version>
</parent>

<artifactId>slf4j-api</artifactId>
Expand Down
87 changes: 47 additions & 40 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Expand Up @@ -54,12 +54,12 @@
* <p/>
* <p/>
* Please note that all methods in <code>LoggerFactory</code> are static.
*
*
*
*
* @author Alexander Dorokhine
* @author Robert Elliot
* @author Ceki G&uuml;lc&uuml;
*
*
*/
public final class LoggerFactory {

Expand Down Expand Up @@ -140,7 +140,8 @@ private static boolean messageContainsOrgSlf4jImplStaticLoggerBinder(String msg)
private final static void bind() {
try {
Set<URL> staticLoggerBinderPathSet = null;
// skip check under android, see also http://jira.qos.ch/browse/SLF4J-328
// skip check under android, see also
// http://jira.qos.ch/browse/SLF4J-328
if (!isAndroid()) {
staticLoggerBinderPathSet = findPossibleStaticLoggerBinderPathSet();
reportMultipleBindingAmbiguity(staticLoggerBinderPathSet);
Expand All @@ -149,6 +150,7 @@ private final static void bind() {
StaticLoggerBinder.getSingleton();
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
reportActualBinding(staticLoggerBinderPathSet);
fixSubstituteLoggers();
replayEvents();
// release all resources in SUBST_FACTORY
SUBST_FACTORY.clear();
Expand Down Expand Up @@ -178,25 +180,22 @@ private final static void bind() {
}
}

private static void fixSubstituteLoggers() {
synchronized (SUBST_FACTORY) {
SUBST_FACTORY.postInitialization();
for (SubstituteLogger substLogger : SUBST_FACTORY.getLoggers()) {
Logger logger = getLogger(substLogger.getName());
substLogger.setDelegate(logger);
}
}

}

static void failedBinding(Throwable t) {
INITIALIZATION_STATE = FAILED_INITIALIZATION;
Util.report("Failed to instantiate SLF4J LoggerFactory", t);
}

private final static void fixSubstitutedLoggers() {
List<SubstituteLogger> loggers = SUBST_FACTORY.getLoggers();

if (loggers.isEmpty()) {
return;
}

for (SubstituteLogger subLogger : loggers) {
Logger logger = getLogger(subLogger.getName());
subLogger.setDelegate(logger);
}

SUBST_FACTORY.clear();
}
private static void replayEvents() {
final LinkedBlockingQueue<SubstituteLoggingEvent> queue = SUBST_FACTORY.getEventQueue();
final int queueSize = queue.size();
Expand Down Expand Up @@ -285,13 +284,15 @@ private final static void versionSanityCheck() {
}
}

// We need to use the name of the StaticLoggerBinder class, but we can't reference
// We need to use the name of the StaticLoggerBinder class, but we can't
// reference
// the class itself.
private static String STATIC_LOGGER_BINDER_PATH = "org/slf4j/impl/StaticLoggerBinder.class";

static Set<URL> findPossibleStaticLoggerBinderPathSet() {
// use Set instead of list in order to deal with bug #138
// LinkedHashSet appropriate here because it preserves insertion order during iteration
// LinkedHashSet appropriate here because it preserves insertion order
// during iteration
Set<URL> staticLoggerBinderPathSet = new LinkedHashSet<URL>();
try {
ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();
Expand All @@ -316,9 +317,9 @@ private static boolean isAmbiguousStaticLoggerBinderPathSet(Set<URL> binderPathS
}

/**
* Prints a warning message on the console if multiple bindings were found on the class path.
* No reporting is done otherwise.
*
* Prints a warning message on the console if multiple bindings were found
* on the class path. No reporting is done otherwise.
*
*/
private static void reportMultipleBindingAmbiguity(Set<URL> binderPathSet) {
if (isAmbiguousStaticLoggerBinderPathSet(binderPathSet)) {
Expand All @@ -345,10 +346,11 @@ private static void reportActualBinding(Set<URL> binderPathSet) {
}

/**
* Return a logger named according to the name parameter using the statically
* bound {@link ILoggerFactory} instance.
*
* @param name The name of the logger.
* Return a logger named according to the name parameter using the
* statically bound {@link ILoggerFactory} instance.
*
* @param name
* The name of the logger.
* @return logger
*/
public static Logger getLogger(String name) {
Expand All @@ -357,20 +359,25 @@ public static Logger getLogger(String name) {
}

/**
* Return a logger named corresponding to the class passed as parameter, using
* the statically bound {@link ILoggerFactory} instance.
*
* <p>In case the the <code>clazz</code> parameter differs from the name of
* the caller as computed internally by SLF4J, a logger name mismatch warning will be
* printed but only if the <code>slf4j.detectLoggerNameMismatch</code> system property is
* set to true. By default, this property is not set and no warnings will be printed
* even in case of a logger name mismatch.
* Return a logger named corresponding to the class passed as parameter,
* using the statically bound {@link ILoggerFactory} instance.
*
* <p>
* In case the the <code>clazz</code> parameter differs from the name of the
* caller as computed internally by SLF4J, a logger name mismatch warning
* will be printed but only if the
* <code>slf4j.detectLoggerNameMismatch</code> system property is set to
* true. By default, this property is not set and no warnings will be
* printed even in case of a logger name mismatch.
*
* @param clazz the returned logger will be named after clazz
* @param clazz
* the returned logger will be named after clazz
* @return logger
*
*
* @see <a href="http://www.slf4j.org/codes.html#loggerNameMismatch">Detected logger name mismatch</a>
*
*
* @see <a
* href="http://www.slf4j.org/codes.html#loggerNameMismatch">Detected
* logger name mismatch</a>
*/
public static Logger getLogger(Class<?> clazz) {
Logger logger = getLogger(clazz.getName());
Expand All @@ -394,7 +401,7 @@ private static boolean nonMatchingClasses(Class<?> clazz, Class<?> autoComputedC
* <p/>
* <p/>
* ILoggerFactory instance is bound with this class at compile time.
*
*
* @return the ILoggerFactory instance in use
*/
public static ILoggerFactory getILoggerFactory() {
Expand Down
Expand Up @@ -25,9 +25,9 @@
package org.slf4j.helpers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import org.slf4j.ILoggerFactory;
Expand All @@ -44,17 +44,15 @@ public class SubstituteLoggerFactory implements ILoggerFactory {

boolean postInitialization = false;

final ConcurrentMap<String, SubstituteLogger> loggers = new ConcurrentHashMap<String, SubstituteLogger>();
final Map<String, SubstituteLogger> loggers = new HashMap<String, SubstituteLogger>();

final LinkedBlockingQueue<SubstituteLoggingEvent> eventQueue = new LinkedBlockingQueue<SubstituteLoggingEvent>();

public Logger getLogger(String name) {
synchronized public Logger getLogger(String name) {
SubstituteLogger logger = loggers.get(name);
if (logger == null) {
logger = new SubstituteLogger(name, eventQueue);
SubstituteLogger oldLogger = loggers.putIfAbsent(name, logger);
if (oldLogger != null)
logger = oldLogger;
logger = new SubstituteLogger(name, eventQueue, postInitialization);
loggers.put(name, logger);
}
return logger;
}
Expand All @@ -71,6 +69,10 @@ public LinkedBlockingQueue<SubstituteLoggingEvent> getEventQueue() {
return eventQueue;
}

public void postInitialization() {
postInitialization = true;
}

public void clear() {
loggers.clear();
eventQueue.clear();
Expand Down
8 changes: 6 additions & 2 deletions slf4j-api/src/test/java/org/slf4j/LoggerAccessingThread.java
Expand Up @@ -24,6 +24,7 @@
*/
package org.slf4j;

import java.util.List;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicLong;

Expand All @@ -33,9 +34,11 @@ public class LoggerAccessingThread extends Thread {
final CyclicBarrier barrier;
final int count;
final AtomicLong eventCount;

public LoggerAccessingThread(final CyclicBarrier barrier, final int count, final AtomicLong eventCount) {
List<Logger> loggerList;

public LoggerAccessingThread(final CyclicBarrier barrier, List<Logger> loggerList, final int count, final AtomicLong eventCount) {
this.barrier = barrier;
this.loggerList = loggerList;
this.count = count;
this.eventCount = eventCount;
}
Expand All @@ -50,6 +53,7 @@ public void run() {
String loggerNamePrefix = this.getClass().getName();
for (int i = 0; i < LOOP_LEN; i++) {
Logger logger = LoggerFactory.getLogger(loggerNamePrefix + "-" + count + "-" + i);
loggerList.add(logger);
Thread.yield();
logger.info("in run method");
eventCount.getAndIncrement();
Expand Down

0 comments on commit 1c6cbf0

Please sign in to comment.