Skip to content

Commit

Permalink
fix SLF4J-463
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed Aug 9, 2019
1 parent d507426 commit acc9150
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
1 change: 1 addition & 0 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Expand Up @@ -148,6 +148,7 @@ private final static void bind() {
reportMultipleBindingAmbiguity(providersList);
if (providersList != null && !providersList.isEmpty()) {
PROVIDER = providersList.get(0);
// SLF4JServiceProvider.initialize() is intended to be called here and nowhere else.
PROVIDER.initialize();
INITIALIZATION_STATE = SUCCESSFUL_INITIALIZATION;
reportActualBinding(providersList);
Expand Down
1 change: 0 additions & 1 deletion slf4j-api/src/main/java/org/slf4j/MarkerFactory.java
Expand Up @@ -52,7 +52,6 @@ private MarkerFactory() {
static {
SLF4JServiceProvider provider = LoggerFactory.getProvider();
if (provider != null) {
provider.initialize();
MARKER_FACTORY = provider.getMarkerFactory();
} else {
Util.report("Failed to find provider");
Expand Down
29 changes: 24 additions & 5 deletions slf4j-api/src/main/java/org/slf4j/spi/SLF4JServiceProvider.java
Expand Up @@ -2,32 +2,51 @@

import org.slf4j.ILoggerFactory;
import org.slf4j.IMarkerFactory;
import org.slf4j.LoggerFactory;

/**
* This interface based on {@link java.util.ServiceLoader} paradigm.
*
* <p>It replaces the old static-binding mechanism used in SLF4J versions 1.0.x to 1.7.x.
*
* @author Ceki G&uml;lc&uml;
* @since 1.8
*/
public interface SLF4JServiceProvider {


/**
* Return the instance of {@link ILoggerFactory} that
* {@link org.slf4j.LoggerFactory} class should bind to.
*
* @return the instance of {@link ILoggerFactory} that
* {@link org.slf4j.LoggerFactory} class should bind to.
* @return instance of {@link ILoggerFactory}
*/
public ILoggerFactory getLoggerFactory();

/**
* Return the instance of {@link IMarkerFactory} that
* {@link org.slf4j.MarkerFactory} class should bind to.
*
* @return the instance of {@link IMarkerFactory} that
* {@link org.slf4j.MarkerFactory} class should bind to.
* @return instance of {@link IMarkerFactory}
*/
public IMarkerFactory getMarkerFactory();


/**
* Return the instnace of {@link MDCAdapter} that
* {@link MDC} should bind to.
*
* @return instance of {@link MDCAdapter}
*/
public MDCAdapter getMDCAdapter();

public String getRequesteApiVersion();

/**
* Initialize the logging back-end.
*
* <p><b>WARNING:</b> This method is intended to be called once by
* {@link LoggerFactory} class and from nowhere else.
*
*/
public void initialize();
}
@@ -0,0 +1,37 @@
package org.slf4j.simple;

This comment has been minimized.

Copy link
@degroves

degroves Aug 9, 2019

Shouldn't this file be in src/test instead of src/main?


import static org.junit.Assert.fail;

import org.junit.Test;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.slf4j.MarkerFactory;

public class DoubleInitializationPitfallTest {


@Test
public void verifyImpactOfMarkerFactory() {
ILoggerFactory firstFactory = LoggerFactory.getILoggerFactory();
MarkerFactory.getMarker("DOUBLE_INIT");
ILoggerFactory secondFactory = LoggerFactory.getILoggerFactory();

if(firstFactory != secondFactory) {
fail("MarkerFactory.getMarker causes multiple provider initialization");
}
}

@Test
public void verifyImpactOfMDC() {
ILoggerFactory firstFactory = LoggerFactory.getILoggerFactory();
MDC.put("DoubleInitializationPitfallTest", "a");
ILoggerFactory secondFactory = LoggerFactory.getILoggerFactory();

if(firstFactory != secondFactory) {
fail("MarkerFactory.getMarker causes multiple provider initialization");
}
}


}
11 changes: 11 additions & 0 deletions slf4j-site/src/site/pages/news.html
Expand Up @@ -37,6 +37,17 @@ <h1>SLF4J News</h1>
class names in <code/>
-->


<hr noshade="noshade" size="1"/>
<h3> 2019 - Release of SLF4J 2.0.0-alpha1</h3>


<p>&bull; Fix the double back-end initializatoin problem reported
in <a href="https://jira.qos.ch/browse/SLF4J-463">SLF4J-463</a> by
Dan Groves who also provided a test-case to reptoduce the problem
and a relevant fix.
</p>

<hr noshade="noshade" size="1"/>

<h3>6th of August, 2019 - Release of SLF4J 1.7.27</h3>
Expand Down

0 comments on commit acc9150

Please sign in to comment.