下面列出了org.slf4j.bridge.SLF4JBridgeHandler#removeHandlersForRootLogger ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static JerseyTest create(Enclave enclave) {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
return new JerseyTest() {
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
set(TestProperties.CONTAINER_PORT, SocketUtils.findAvailableTcpPort());
EnclaveApplication application = new EnclaveApplication(new EnclaveResource(enclave));
ResourceConfig config = ResourceConfig.forApplication(application);
config.packages("com.quorum.tessera.enclave.rest");
return config;
}
};
}
@Before
public void initImpl() throws LifecycleException, ServletException {
if (!inited.contains(getClass())) {
inited.add(getClass());
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
tomcat = new Tomcat();
tomcat.setPort(PORT);
tomcat.setBaseDir(".");
final Context ctx = tomcat.addWebapp("", "src/test");
ctx.setJarScanner(NO_SCAN);
((Host) ctx.getParent()).setAppBase("");
init(ctx);
tomcat.start();
}
}
public Component() {
this.locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
ServiceLocatorUtilities.addOneConstant(this.locator, this);
// Optionally remove existing handlers attached to j.u.l root logger
SLF4JBridgeHandler.removeHandlersForRootLogger();
// add SLF4JBridgeHandler to java.util.logging's root logger
SLF4JBridgeHandler.install();
}
public static synchronized void initialize() {
if (dummy == -1) {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
dummy = 0;
}
}
/**
* Method starts the database package and it's libraries by loading all their bean factories each of which is
* responsible to load it's own code henceforth.
*
* List of bean factories is maintained under {@code /src/main/resources/launcher-config.xml}.
*
* TODO: 1) I had a dream.. a dream that some day main functions will parse CLI arguments.
*
* @param args who cares.. for now..
*/
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
logger.info("Starting BlobCity DB\"");
// Pre-startup tasks below
logger.debug("Configuring JUL Loggers");
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
System.setProperty("jna.debug_load", "true");
System.setProperty("jna.debug_load.jna", "true");
// System.setProperty("java.library.path", "/lib");
System.setProperty("jna.library.path", "/lib64");
// Startup tasks below
logger.debug("Performing database launch tasks");
try {
loadBeanConfigs(); // This task launches the database
// Any other future tasks can go here as method class
initEngine();
} catch (ClassNotFoundException ex) {
logger.error("Launching BlobCity DB has failed!", ex);
}
logger.info("BlobCity DB successfully started in " + (System.currentTimeMillis() - startTime) / 1000 + " seconds");
logger.info("Setting log level to ERROR. Change by using set-log-level {log_level} CLI command");
LogManager.getRootLogger().setLevel(org.apache.log4j.Level.ERROR);
// it is assumed that startup failures are managed and appropriate logs are generated
}
@Override
protected void initializeLogging() {
super.initializeLogging();
// Move all java util logging logs to SLF4j
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
protected static void setupLogging(LoggingConfig config, String sentryDsn) {
if (config.getNoLogSetup()) {
return;
}
// Hijack JUL
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
final int verbose = config.getVerbosity();
final Level level = get(asList(INFO, DEBUG, ALL), verbose, ALL);
final File logconfig = config.getConfigFile();
if (logconfig != null) {
LoggingConfigurator.configure(logconfig);
} else {
if (config.isSyslog()) {
LoggingConfigurator.configureSyslogDefaults("helios", level);
} else {
LoggingConfigurator.configureDefaults("helios", level);
}
if (!Strings.isNullOrEmpty(sentryDsn)) {
LoggingConfigurator.addSentryAppender(sentryDsn);
}
}
}
private void switchLogging() {
if (this.useSlf4j) {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
Logger.getLogger("PIT").addHandler(new SLF4JBridgeHandler());
SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
}
}
private AutodetectLogManager() {
if (isLogbackInUse()) {
logManager = new LogbackLogManager();
} else {
logManager = new NoOpLogManager();
}
try {
java.util.logging.LogManager.getLogManager().reset();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
} catch (Exception e) {
System.err.println("Unable to install JUL to SLF4J bridge");
}
}
@BeforeAll
static void beforeAll() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
private void hijackJDKLogging() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
/**
* Setup the SLF4J to JUL bridge.
*/
private static void installSLF4JBridge(){
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
@Override
public void configureLogging(@NotNull final String logfile) {
checkArgument(StringUtils.isNotBlank(logfile));
try {
final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
final FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
fileAppender.setContext(loggerContext);
fileAppender.setName("timestamp");
// set the file name
fileAppender.setFile(logfile);
final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%r %thread %level - %msg%n");
encoder.start();
fileAppender.setEncoder(encoder);
fileAppender.start();
// attach the rolling file appender to the logger of your choice
final Logger logbackLogger =
(Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
logbackLogger.addAppender(fileAppender);
/*
We only want to hear about errors from the libraries that are used by Iridium
*/
logbackLogger.setLevel(Level.ERROR);
/*
Our own code should be info level
*/
final Logger iridiumLogger =
(Logger) LoggerFactory.getLogger("au.com.agic");
iridiumLogger.setLevel(Level.INFO);
/*
Performance increase for redirected JUL loggers
*/
final LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
levelChangePropagator.setContext(loggerContext);
levelChangePropagator.setResetJUL(true);
loggerContext.addListener(levelChangePropagator);
/*
Redirect java logging and sys out to slf4j
*/
LogManager.getLogManager().reset();
SLF4JBridgeHandler.removeHandlersForRootLogger();
SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
SLF4JBridgeHandler.install();
} catch (final Exception ex) {
LOGGER.error("WEBAPPTESTER-BUG-0006: Could not configure Logback", ex);
}
}
@Override
public void start ( final BundleContext arg0 ) throws Exception
{
SLF4JBridgeHandler.removeHandlersForRootLogger ();
SLF4JBridgeHandler.install ();
}
/**
* Initialize java.util.logging to SLF4J.
*/
private static void configureJUL() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
@Override
protected void before() throws Throwable {
Logger.getLogger("").setLevel(Level.ALL);
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
@BeforeClass
public static void setUpBeforeClass() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
public void afterPropertiesSet() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
/**
* Reconfigure logging configuration
*
* @param level Log level
*/
public void setLogging(final String level) {
this.setProperty("logging", level);
// Call only once during initialization time of your application
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
// Apply default configuration
final URL configuration;
final String file = this.getDefault("logging.config");
if(null == file) {
configuration = Preferences.class.getClassLoader().getResource("log4j.xml");
}
else {
configuration = Preferences.class.getClassLoader().getResource(file);
}
LogManager.resetConfiguration();
final Logger root = Logger.getRootLogger();
if(null != configuration) {
DOMConfigurator.configure(configuration);
}
// Allow to override default logging level
root.setLevel(Level.toLevel(level, Level.ERROR));
// Map logging level to pass through bridge
final ImmutableMap<Level, java.util.logging.Level> map = new ImmutableMap.Builder<Level, java.util.logging.Level>()
.put(Level.ALL, java.util.logging.Level.ALL)
.put(Level.DEBUG, java.util.logging.Level.FINE)
.put(Level.ERROR, java.util.logging.Level.SEVERE)
.put(Level.FATAL, java.util.logging.Level.SEVERE)
.put(Level.INFO, java.util.logging.Level.INFO)
.put(Level.OFF, java.util.logging.Level.OFF)
.put(Level.TRACE, java.util.logging.Level.FINEST)
.put(Level.WARN, java.util.logging.Level.WARNING)
.build();
java.util.logging.Logger.getLogger("").setLevel(map.get(root.getLevel()));
final Enumeration loggers = LogManager.getCurrentLoggers();
while(loggers.hasMoreElements()) {
final Logger logger = (Logger) loggers.nextElement();
if(logger.getLevel() != null) {
java.util.logging.Logger.getLogger(logger.getName()).setLevel(map.get(logger.getLevel()));
}
}
}
/**
* Redirects all logging statements from java.util.logging to SLF4J
* <p>
* This is needed because we may have many dependencies which rely on JUL.
*/
private static void redirectJULToSLF4J() {
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}