Merge branch 'adorokhine-remarket'

This commit is contained in:
Ceki Gulcu 2014-12-13 23:55:14 +01:00
commit 175c8e41b7
6 changed files with 50 additions and 89 deletions

View File

@ -56,7 +56,7 @@ import org.slf4j.impl.StaticLoggerBinder;
* Please note that all methods in <code>LoggerFactory</code> are static.
*
*
* @author Alexandre Dorokhine
* @author Alexander Dorokhine
* @author Robert Elliot
* @author Ceki G&uuml;lc&uuml;
*
@ -85,9 +85,9 @@ public final class LoggerFactory {
static SubstituteLoggerFactory TEMP_FACTORY = new SubstituteLoggerFactory();
static NOPLoggerFactory NOP_FALLBACK_FACTORY = new NOPLoggerFactory();
// Support for the automatically-named logger field trial.
static final String AUTO_NAMED_LOGGER_FIELD_TRIAL_PROPERTY = "org.slf4j.LoggerFactory.autoNamedLoggerFieldTrial";
static boolean AUTO_NAMED_LOGGER_FIELD_TRIAL = Boolean.getBoolean(AUTO_NAMED_LOGGER_FIELD_TRIAL_PROPERTY);
// Support for detecting mismatched logger names.
static final String DETECT_LOGGER_NAME_MISMATCH_PROPERTY = "org.slf4j.LoggerFactory.detectLoggerNameMismatch";
static boolean DETECT_LOGGER_NAME_MISMATCH = Boolean.getBoolean(DETECT_LOGGER_NAME_MISMATCH_PROPERTY);
/**
* It is LoggerFactory's responsibility to track version changes and manage
@ -297,14 +297,12 @@ public final class LoggerFactory {
@Nonnull
public static Logger getLogger(@Nonnull Class<?> clazz) {
Logger logger = getLogger(clazz.getName());
if (AUTO_NAMED_LOGGER_FIELD_TRIAL) {
if (DETECT_LOGGER_NAME_MISMATCH) {
Class<?> autoComputedCallingClass = Util.getCallingClass();
if (nonMatchingClasses(clazz, autoComputedCallingClass)) {
Util.report(String.format("Auto-named logger field trial: mismatch detected between "
+ "given logger name and automatic logger name. Given name: \"%s\"; "
+ "automatic name: \"%s\". If this is unexpected, please file a bug "
+ "against slf4j. Set property %s to \"false\" to disable this check.", logger.getName(),
autoComputedCallingClass.getName(), AUTO_NAMED_LOGGER_FIELD_TRIAL_PROPERTY));
Util.report(String.format("Detected logger name mismatch. Given name: \"%s\"; computed name: \"%s\". ",
logger.getName(), autoComputedCallingClass.getName()));
Util.report("See http://www.slf4j.org/codes.html#loggerNameMismatch for an explanation");
}
}
return logger;
@ -329,16 +327,16 @@ public final class LoggerFactory {
performInitialization();
}
switch (INITIALIZATION_STATE) {
case SUCCESSFUL_INITIALIZATION:
return StaticLoggerBinder.getSingleton().getLoggerFactory();
case NOP_FALLBACK_INITIALIZATION:
return NOP_FALLBACK_FACTORY;
case FAILED_INITIALIZATION:
throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG);
case ONGOING_INITIALIZATION:
// support re-entrant behavior.
// See also http://bugzilla.slf4j.org/show_bug.cgi?id=106
return TEMP_FACTORY;
case SUCCESSFUL_INITIALIZATION:
return StaticLoggerBinder.getSingleton().getLoggerFactory();
case NOP_FALLBACK_INITIALIZATION:
return NOP_FALLBACK_FACTORY;
case FAILED_INITIALIZATION:
throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG);
case ONGOING_INITIALIZATION:
// support re-entrant behavior.
// See also http://bugzilla.slf4j.org/show_bug.cgi?id=106
return TEMP_FACTORY;
}
throw new IllegalStateException("Unreachable code");
}

View File

@ -28,7 +28,7 @@ package org.slf4j.helpers;
/**
* An internal utility class.
*
* @author Alexandre Dorokhine
* @author Alexander Dorokhine
* @author Ceki G&uuml;lc&uuml;
*/
public final class Util {

View File

@ -33,20 +33,20 @@ import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.shape.Square;
/**
* Tests that the automatically named logger field trial works and
* doesn't cause problems or trigger if disabled.
* Tests that detecting logger name mismatches works and doesn't cause problems
* or trigger if disabled.
* <p>
* This test can't live inside slf4j-api because the NOP Logger doesn't
* remember its name.
*
* @author Alexandre Dorokhine
* @author Alexander Dorokhine
* @author Ceki G&uuml;lc&uuml;
*/
public class AutoNamedLoggerFieldTrialTest {
public class DetectLoggerNameMismatchTest {
private static final String MISMATCH_STRING = "Auto-named logger field trial: mismatch detected";
private static final String MISMATCH_STRING = "Detected logger name mismatch";
private final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
private final PrintStream oldErr = System.err;
@ -63,7 +63,7 @@ public class AutoNamedLoggerFieldTrialTest {
}
/*
* Pass in the wrong class to the Logger with the field trial disabled, and
* Pass in the wrong class to the Logger with the check disabled, and
* make sure there are no errors.
*/
@Test
@ -75,7 +75,7 @@ public class AutoNamedLoggerFieldTrialTest {
}
/*
* Pass in the wrong class to the Logger with the field trial enabled, and
* Pass in the wrong class to the Logger with the check enabled, and
* make sure there ARE errors.
*/
@Test
@ -92,24 +92,22 @@ public class AutoNamedLoggerFieldTrialTest {
public void testTriggerWholeMessage() {
setTrialEnabled(true);
LoggerFactory.getLogger(String.class);
assertTrue(String.valueOf(byteArrayOutputStream).contains(
"Auto-named logger field trial: mismatch detected between given logger " +
"name and automatic logger name. Given name: \"java.lang.String\"; " +
"automatic name: \"org.slf4j.AutoNamedLoggerFieldTrialTest\". If this " +
"is unexpected, please file a bug against slf4j. Set property " +
"org.slf4j.LoggerFactory.autoNamedLoggerFieldTrial to \"false\" to " +
"disable this check."));
assertTrue(
"Actual value of byteArrayOutputStream: " + String.valueOf(byteArrayOutputStream),
String.valueOf(byteArrayOutputStream).contains(
"Detected logger name mismatch. Given name: \"java.lang.String\"; " +
"computed name: \"org.slf4j.DetectLoggerNameMismatchTest\". "));
}
/*
* Checks that there are no errors with the trial enabled if the
* Checks that there are no errors with the check enabled if the
* class matches.
*/
@Test
public void testPassIfMatch() {
setTrialEnabled(true);
Logger logger = LoggerFactory.getLogger(AutoNamedLoggerFieldTrialTest.class);
assertEquals("org.slf4j.AutoNamedLoggerFieldTrialTest", logger.getName());
Logger logger = LoggerFactory.getLogger(DetectLoggerNameMismatchTest.class);
assertEquals("org.slf4j.DetectLoggerNameMismatchTest", logger.getName());
assertMismatchDetected(false);
}
@ -118,18 +116,25 @@ public class AutoNamedLoggerFieldTrialTest {
String.valueOf(byteArrayOutputStream).contains(MISMATCH_STRING));
}
@Test
public void verifyLoggerDefinedInBaseWithOverridenGetClassMethod() {
setTrialEnabled(true);
Square square = new Square();
assertEquals("org.slf4j.shape.Square", square.logger.getName());
assertMismatchDetected(false);
@Test
public void verifyLoggerDefinedInBaseWithOverridenGetClassMethod() {
setTrialEnabled(true);
Square square = new Square();
assertEquals("org.slf4j.Square", square.logger.getName());
assertMismatchDetected(false);
}
}
private static void setTrialEnabled(boolean enabled) {
// 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.AUTO_NAMED_LOGGER_FIELD_TRIAL = enabled;
LoggerFactory.DETECT_LOGGER_NAME_MISMATCH = enabled;
}
}
// Used for testing that inheritance is ignored by the checker.
class ShapeBase {
public Logger logger = LoggerFactory.getLogger(getClass());
}
class Square extends ShapeBase {}

View File

@ -1,23 +0,0 @@
package org.slf4j;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import org.slf4j.shape.Square;
public class ShapeTest {
@Test
public void verifyLoggerDefinedInBaseWithOverridenGetClassMethod() {
Square square = new Square();
System.out.println(square.logger.getName());
}
private static void setTrialEnabled(boolean enabled) {
// 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.AUTO_NAMED_LOGGER_FIELD_TRIAL = enabled;
}
}

View File

@ -1,12 +0,0 @@
package org.slf4j.shape;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by ceki on 12/8/2014.
*/
public class ShapeBase {
public Logger logger = LoggerFactory.getLogger(getClass());
}

View File

@ -1,7 +0,0 @@
package org.slf4j.shape;
/**
* Created by ceki on 12/8/2014.
*/
public class Square extends ShapeBase {
}