下面列出了怎么用org.slf4j.helpers.NOPLoggerFactory的API类实例代码及写法,或者点击链接到github查看源代码。
public static ServiceLocator initServiceLocator() {
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
locator.setErrorHandler(
new DefaultServiceLocator.ErrorHandler() {
@Override
public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception) {
throw new RuntimeException(
String.format(
"Failed to initialize service %s, implemented by %s: %s",
type.getName(), impl.getName(), exception.getMessage()),
exception);
}
});
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
locator.addService(TransporterFactory.class, FileTransporterFactory.class);
// Use a no-op logger. Leaving this out would introduce a runtime dependency on log4j
locator.addService(ILoggerFactory.class, NOPLoggerFactory.class);
// Also requires log4j
// locator.addService(ILoggerFactory.class, Log4jLoggerFactory.class);
return locator;
}
@Test
public void login() throws URISyntaxException {
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) {
Assert.fail("No logging implementation found, using fallback NOP logger");
}
Logger logger = LoggerFactory.getLogger("");
logger.info("If you see this info in the logger, everything is alright");
}
@Override
public void onActivated(Options opts) throws BadCommandLineException {
final ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
if (iLoggerFactory instanceof NOPLoggerFactory) {
System.err
.println("You seem to be using the NOP provider of the SLF4j logging facade. "
+ "With this configuration, log messages will be completely suppressed. "
+ "Please consider adding a SLF4j provider (for instance slf4j-simple) to enable logging.");
}
}
@Test
public void shouldNotUseNopLoggerFactory() {
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
// verify that a SLF4J backend is used which is not the NOP logger
assertFalse("Should not use NOPLoggerFactory", loggerFactory instanceof NOPLoggerFactory);
// should either use slf4j-jdk14 or slf4j-jboss-logmanager
String loggerFactoryClassName = loggerFactory.getClass().getCanonicalName();
assertTrue("Should use slf4j-jdk14 or slf4j-jboss-logmanager",
JDK14_LOGGER_FACTORY.equals(loggerFactoryClassName) || JBOSS_SLF4J_LOGGER_FACTORY.equals(loggerFactoryClassName));
}
public Slf4jLogFactory() {
this.useJdkLogger = LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory;
}
private Slf4JLoggerFactory() {
if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) {
throw new NoClassDefFoundError("NOPLoggerFactory not supported");
}
}
/**
* Returns {@code true} if there is a logging binding available for SLF4J.
*
* @return {@code true} if there is a SLF4J logging binding
*/
protected boolean hasLogBinding() {
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
return loggerFactory != null
&& !NOPLoggerFactory.class.equals(loggerFactory.getClass());
}