Skip to content

Commit

Permalink
In SLF4JProviders, move initialization of the markerFactory and
Browse files Browse the repository at this point in the history
mdcAdapter fields to the constructor.

Added comments regarding LoggerFactory expectation of providers to
initialize their MDCAdapter and markerFactory field as early as
possible, preferably at construction time.

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Feb 25, 2025
1 parent 9bd0374 commit 40b3e51
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
4 changes: 2 additions & 2 deletions slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ private final static void bind() {
/**
* The value of PROVIDER.getMDCAdapter() can be null while PROVIDER has not yet initialized.
*
* However,
*
* However, SLF4JServiceProvider implementations are expected to initialize their internal
* MDCAdapter field in their constructor or on field declaration.
*/
private static void earlyBindMDCAdapter() {
MDCAdapter mdcAdapter = PROVIDER.getMDCAdapter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@

public class SubstituteServiceProvider implements SLF4JServiceProvider {
private final SubstituteLoggerFactory loggerFactory = new SubstituteLoggerFactory();
private final IMarkerFactory markerFactory = new BasicMarkerFactory();
private final MDCAdapter mdcAdapter = new BasicMDCAdapter();

// LoggerFactory expects providers to initialize markerFactory as early as possible.
private final IMarkerFactory markerFactory;

// LoggerFactory expects providers to initialize their MDCAdapter field
// as early as possible, preferably at construction time.
private final MDCAdapter mdcAdapter;

public SubstituteServiceProvider() {
markerFactory = new BasicMarkerFactory();
mdcAdapter = new BasicMDCAdapter();
}

@Override
public ILoggerFactory getLoggerFactory() {
Expand Down Expand Up @@ -36,6 +46,5 @@ public String getRequestedApiVersion() {

@Override
public void initialize() {

}
}
12 changes: 10 additions & 2 deletions slf4j-jdk14/src/main/java/org/slf4j/jul/JULServiceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ public class JULServiceProvider implements SLF4JServiceProvider {
public static String REQUESTED_API_VERSION = "2.0.99"; // !final

private ILoggerFactory loggerFactory;
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new BasicMDCAdapter();
// LoggerFactory expects providers to initialize markerFactory as early as possible.
private final IMarkerFactory markerFactory;
// LoggerFactory expects providers to initialize their MDCAdapter field
// as early as possible, preferably at construction time.
private final MDCAdapter mdcAdapter;

public JULServiceProvider() {
markerFactory = new BasicMarkerFactory();
mdcAdapter = new BasicMDCAdapter();
}

@Override
public ILoggerFactory getLoggerFactory() {
Expand Down
14 changes: 11 additions & 3 deletions slf4j-nop/src/main/java/org/slf4j/nop/NOPServiceProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ public class NOPServiceProvider implements SLF4JServiceProvider {
public static String REQUESTED_API_VERSION = "2.0.99"; // !final

private final ILoggerFactory loggerFactory = new NOPLoggerFactory();
private final IMarkerFactory markerFactory = new BasicMarkerFactory();
private final MDCAdapter mdcAdapter = new NOPMDCAdapter();

// LoggerFactory expects providers to initialize markerFactory as early as possible.
private final IMarkerFactory markerFactory;

// LoggerFactory expects providers to initialize their MDCAdapter field
// as early as possible, preferably at construction time.
private final MDCAdapter mdcAdapter;

public NOPServiceProvider() {
markerFactory = new BasicMarkerFactory();
mdcAdapter = new NOPMDCAdapter();
}
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
Expand All @@ -44,7 +53,6 @@ public String getRequestedApiVersion() {
}

public void initialize() {

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ public class Reload4jServiceProvider implements SLF4JServiceProvider {
public static String REQUESTED_API_VERSION = "2.0.99"; // !final

private ILoggerFactory loggerFactory;
private IMarkerFactory markerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new Reload4jMDCAdapter();

// LoggerFactory expects providers to initialize markerFactory as early as possible.
private final IMarkerFactory markerFactory;

// LoggerFactory expects providers to have a valid MDCAdapter field
// as early as possible, preferably at construction time.
private final MDCAdapter mdcAdapter;

public Reload4jServiceProvider() {
markerFactory = new BasicMarkerFactory();
mdcAdapter = new Reload4jMDCAdapter();
try {
@SuppressWarnings("unused")
Level level = Level.TRACE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ public class SimpleServiceProvider implements SLF4JServiceProvider {
public static String REQUESTED_API_VERSION = "2.0.99"; // !final

private ILoggerFactory loggerFactory;
private IMarkerFactory = new BasicMarkerFactory();
private MDCAdapter mdcAdapter = new NOPMDCAdapter();
// LoggerFactory expects providers to initialize markerFactory as early as possible.
private final IMarkerFactory markerFactory;
// LoggerFactory expects providers to initialize their MDCAdapter field
// as early as possible, preferably at construction time.
private final MDCAdapter mdcAdapter;

public SimpleServiceProvider() {
markerFactory = new BasicMarkerFactory();
mdcAdapter = new NOPMDCAdapter();
}

public ILoggerFactory getLoggerFactory() {
return loggerFactory;
Expand All @@ -42,6 +50,7 @@ public String getRequestedApiVersion() {
@Override
public void initialize() {
loggerFactory = new SimpleLoggerFactory();

}

}

0 comments on commit 40b3e51

Please sign in to comment.