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"); }
public SLF4JLogger(String name) { super(name); if (org.slf4j.LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) { throw new AbacusException("Failed to initilze SLF4J Logger Factory"); } loggerImpl = org.slf4j.LoggerFactory.getLogger(name); }
@Override public void uncaughtException(Thread t, Throwable e) { Object context = LoggerFactory.getILoggerFactory(); if (context instanceof NOPLoggerFactory) { System.err.println(String.format("Exception in thread %s", t.getName())); e.printStackTrace(); } else { LOG.error(String.format("Exception in thread %s", t.getName()), e); } }
/** * 构造 * * @param failIfNOP 如果未找到桥接包是否报错 */ public Slf4jLogFactory(boolean failIfNOP) { super("Slf4j"); if(false == failIfNOP){ return; } // SFL4J writes it error messages to System.err. Capture them so that the user does not see such a message on // the console during automatic detection. final StringBuilder buf = new StringBuilder(); final PrintStream err = System.err; try { System.setErr(new PrintStream(new OutputStream(){ @Override public void write(int b) { buf.append((char) b); } }, true, "US-ASCII")); } catch (UnsupportedEncodingException e) { throw new Error(e); } try { if (LoggerFactory.getILoggerFactory() instanceof NOPLoggerFactory) { throw new NoClassDefFoundError(buf.toString()); } else { err.print(buf); err.flush(); } } finally { System.setErr(err); } }
@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)); }
/** * Initializes Logback framework using the specified configuration file. * If the configuration file does not exist, or cannot be read, then this method logs * the problem and returns. * * @param servletCtx a servlet context. */ private void maybeInitializeLogback( ServletContext servletCtx ) { // non-expanded logback config file location String configLocation = servletCtx.getInitParameter( INIT_PARAM_CONFIG_LOCATION ); String expandedConfigLocation = expandProperties( configLocation, servletCtx ); File configLocationFile = new File( expandedConfigLocation ); if ( configLocationFile.exists() || configLocationFile.isFile() ) { if ( configLocationFile.canRead() ) { ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory(); if ( loggerFactory instanceof LoggerContext ) { log.info( "About to initialize Logback using config file: {}", configLocationFile ); LoggerContext context = (LoggerContext) loggerFactory; JoranConfigurator jc = new JoranConfigurator(); jc.setContext( context ); context.reset(); context.putProperty( "logback.config.dir", configLocationFile.getParentFile().getAbsolutePath() ); try { jc.doConfigure( configLocationFile ); log.info( "Successfully initialized Logback using config file: {}", configLocationFile ); } catch ( JoranException e ) { log.error( "Error initializing Logback from: " + configLocationFile, e ); } } else { if ( loggerFactory instanceof NOPLoggerFactory ) { // need to use System.out, otherwise the message would have been silently swallowed by the NOPLogger System.out.println( "Logback cannot be initialized as SLF4J is not statically bound to any logging framework implementation. See system error output for possible SLF4J errors." ); // print SLF4J LoggerFactory class info System.out.println( "SLF4J LoggerFactory class info: " + Debug.getClassInfo( LoggerFactory.class ) ); } else { log.warn( "Logback cannot be initialized as SLF4J is not statically bound to Logback implementation. Bound SLF4J logger factory: {}", loggerFactory.getClass().getName() ); // log SLF4J LoggerFactory class info log.warn( "SLF4J LoggerFactory class info: {}", Debug.getClassInfo( LoggerFactory.class ) ); } } } else { log.info( "Logback configuration file: {} exists, but cannot be read. Logback configuration not changed.", configLocationFile ); } } else { log.info( "Logback configuration file: {} not found. Logback configuration not changed.", configLocationFile ); } }
private StaticLoggerBinder() { loggerFactory = new NOPLoggerFactory(); }
@Override public ILoggerFactory getLoggerFactory() { return new NOPLoggerFactory(); }
@Override public String getLoggerFactoryClassStr() { return NOPLoggerFactory.class.getName(); }