Java Code Examples for ch.qos.logback.classic.LoggerContext#stop()

The following examples show how to use ch.qos.logback.classic.LoggerContext#stop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingularityAbort.java    From Singularity with Apache License 2.0 6 votes vote down vote up
private void flushLogs() {
  final long millisToWait = 100;

  LOG.info(
    "Attempting to flush logs and wait {} ...",
    JavaUtils.durationFromMillis(millisToWait)
  );

  ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
  if (loggerFactory instanceof LoggerContext) {
    LoggerContext context = (LoggerContext) loggerFactory;
    context.stop();
  }

  try {
    Thread.sleep(millisToWait);
  } catch (Exception e) {
    LOG.info("While sleeping for log flush", e);
  }
}
 
Example 2
Source File: LOGBackConfigurer.java    From styx with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize LOGBack from the given URL.
 *
 * @param url              the url pointing to the location of the config file.
 * @param installJULBridge set to true to install SLF4J JUL bridge
 * @throws IllegalArgumentException if the url points to a non existing location or an error occurs during the parsing operation.
 */
public static void initLogging(URL url, boolean installJULBridge) {
    StaticLoggerBinder.getSingleton();
    ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector();
    LoggerContext loggerContext = selector.getLoggerContext();
    loggerContext.stop();
    ContextInitializer ctxi = new ContextInitializer(loggerContext);
    try {
        ctxi.configureByResource(url);
        loggerContext.start();
        if (installJULBridge) {
            //uninstall already present handlers we want to
            //continue logging through SLF4J after this point
            Logger l = LogManager.getLogManager().getLogger("");
            for (Handler h : l.getHandlers()) {
                l.removeHandler(h);
            }
            SLF4JBridgeHandler.install();

        }
    } catch (JoranException e) {
        throw new IllegalArgumentException("exception while initializing LOGBack", e);
    }
}
 
Example 3
Source File: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 6 votes vote down vote up
public void testTlsSender() throws JoranException, InterruptedException {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    context.reset();
    configurator.doConfigure(this.getClass().getClassLoader().getResourceAsStream("logback-syslog4j-tls.xml"));

    Logger logger = context.getLogger("test-tls");
    logger.info("test message over tls");

    context.stop();
    Thread.sleep(100);

    final String serverData = serverStream.toString();
    assertTrue("Server received: " + serverData, serverData.contains("test message over tls"));
}
 
Example 4
Source File: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 6 votes vote down vote up
public void testTcpSender() throws JoranException, InterruptedException {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    context.reset();
    configurator.doConfigure(this.getClass().getClassLoader().getResourceAsStream("logback-syslog4j-tcp.xml"));

    Logger logger = context.getLogger("test-tcp");
    logger.info("test message over tcp");

    context.stop();
    Thread.sleep(100);

    final String serverData = serverStream.toString();
    assertTrue("Server received: " + serverData, serverData.contains("test message over tcp"));
}
 
Example 5
Source File: Syslog4jAppenderTest.java    From logback-syslog4j with MIT License 6 votes vote down vote up
public void testUdpSender() throws JoranException, InterruptedException {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    JoranConfigurator configurator = new JoranConfigurator();
    configurator.setContext(context);
    context.reset();
    configurator.doConfigure(this.getClass().getClassLoader().getResourceAsStream("logback-syslog4j-udp.xml"));

    Logger logger = context.getLogger("test-udp");
    logger.info("test message over udp");

    context.stop();
    Thread.sleep(100);

    final String serverData = serverStream.toString();
    assertTrue("Server received: " + serverData, serverData.contains("test message over udp"));
}
 
Example 6
Source File: LogbackJMXContextListener.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
@Override
public void contextDestroyed(ServletContextEvent sce) {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    try {
        if (lc != null) {
            lc.stop();
        }
    } catch (Exception exp) {
        // Do nothing, no more logs
    }
}
 
Example 7
Source File: LifecycleHelper.java    From Baragon with Apache License 2.0 5 votes vote down vote up
private void flushLogs() {
  final long millisToWait = 100;

  ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
  if (loggerFactory instanceof LoggerContext) {
    LoggerContext context = (LoggerContext) loggerFactory;
    context.stop();
  }

  try {
    Thread.sleep(millisToWait);
  } catch (Exception e) {
    LOG.info("While sleeping for log flush", e);
  }
}
 
Example 8
Source File: SingularityExecutorRunner.java    From Singularity with Apache License 2.0 5 votes vote down vote up
private static void stopLog() {
  ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
  if (loggerFactory instanceof LoggerContext) {
    LoggerContext context = (LoggerContext) loggerFactory;
    context.stop();
  }
}
 
Example 9
Source File: ContextManager.java    From StatsAgg with Apache License 2.0 5 votes vote down vote up
private void shutdownLogger() {
    try {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        loggerContext.stop();
    }
    catch (Exception e) {
        logger.error(e.toString() + File.separator + StackTrace.getStringFromStackTrace(e));
        logger.error("Failed to shutdown logger");
    }
}
 
Example 10
Source File: LOGBackConfigurer.java    From styx with Apache License 2.0 5 votes vote down vote up
/**
 * Shut down LOGBack.
 * This isn't strictly necessary, but recommended for shutting down
 * logback in a scenario where the host VM stays alive (for example, when
 * shutting down an application in a J2EE environment).
 *
 * @param uninstallJULBridge should an attempt be made to uninstall the JUL bridge
 */
public static void shutdownLogging(boolean uninstallJULBridge) {
    ContextSelector selector = ContextSelectorStaticBinder.getSingleton().getContextSelector();
    LoggerContext loggerContext = selector.getLoggerContext();
    String loggerContextName = loggerContext.getName();
    LoggerContext context = selector.detachLoggerContext(loggerContextName);
    if (uninstallJULBridge) {
        SLF4JBridgeHandler.uninstall();
    }
    context.stop();
}
 
Example 11
Source File: LogbackLoggingSystem.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
private void stopAndReset(LoggerContext loggerContext) {
	loggerContext.stop();
	loggerContext.reset();
	if (isBridgeHandlerAvailable()) {
		addLevelChangePropagator(loggerContext);
	}
}
 
Example 12
Source File: DefaultLogbackReInitializer.java    From sofa-common-tools with Apache License 2.0 5 votes vote down vote up
private void stopAndReset(LoggerContext loggerContext) {
    loggerContext.stop();
    loggerContext.reset();
    if (isBridgeHandlerAvailable()) {
        addLevelChangePropagator(loggerContext);
    }
}
 
Example 13
Source File: NsLog.java    From ns4_frame with Apache License 2.0 4 votes vote down vote up
private static void renameLoggingFile() {
     LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
     context.stop();
     context.reset();
     JoranConfigurator configurator = new JoranConfigurator();
     configurator.setContext(context);
     try {
         URL url = getResource(LogConstants.AUTOCONFIG_FILE, Thread.currentThread().getContextClassLoader());
         if (url == null) {
             throw new RuntimeException("init Log error,can't find logback.xml");
         }
         
         
         System.out.println("初始化File:"+url.getFile());
         
         
         configurator.doConfigure(url.getFile());
     } catch (JoranException e) {
     	
     	//读取文件读不到 读取流
     	try {
	configurator.doConfigure(getResourceAsStream(LogConstants.AUTOCONFIG_FILE, Thread.currentThread().getContextClassLoader()));
} catch (JoranException e1) {
	throw new RuntimeException(e.getMessage(), e1);
}
     	
     }

     String configFile = System.getProperty("configfile");
     if(configFile == null || "".equals(configFile)) {
     	configFile = "";
     	//System.out.println("ns_log.configfile is null");
     }else {
     	configFile = configFile.indexOf(".") != -1 ? configFile.substring(0, configFile.lastIndexOf(".")) : configFile;
     	configFile += "_";
     	//System.out.println("ns_log.configfile is configFile");
     }
     
     for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
         Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders();
         while (index.hasNext()) {
             Appender<ILoggingEvent> appender = index.next();
             if (appender instanceof NSLogRollingFileAppender) {
                 NSLogRollingFileAppender fileAppender = ((NSLogRollingFileAppender) appender);
                 fileAppender.stop();
                 String fileName = fileAppender.getFile().replace("configfile_", configFile);
                 fileAppender.setFile(fileName);
                 //System.out.println("ns_log.filenNme:" + fileName);

                 TriggeringPolicy triggeringPolicy = fileAppender.getTriggeringPolicy();
                 if (triggeringPolicy instanceof TimeBasedRollingPolicy) {
                     TimeBasedRollingPolicy timeBasedRollingPolicy = ((TimeBasedRollingPolicy) triggeringPolicy);
                     String fileNamePattern = timeBasedRollingPolicy.getFileNamePattern().replace("configfile_", configFile);
                     timeBasedRollingPolicy.setFileNamePattern(fileNamePattern);
                     //System.out.println("ns_log.fileNamePattern:" + fileNamePattern);
                     timeBasedRollingPolicy.stop();
                     timeBasedRollingPolicy.start();
                 }
                 fileAppender.realStart();
             }
         }
     }
     context.start();
 }
 
Example 14
Source File: LogbackStopListener.java    From telekom-workflow-engine with MIT License 4 votes vote down vote up
@Override
public void contextDestroyed( ServletContextEvent sce ){
    // assume SLF4J is bound to logback-classic in the current environment
    LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
    loggerContext.stop();
}
 
Example 15
Source File: LogBackExample.java    From gflogger with Apache License 2.0 4 votes vote down vote up
@Override
protected void stop() {
	LoggerContext ctx = ((LoggerContext)LoggerFactory.getILoggerFactory());
	ctx.stop();
}
 
Example 16
Source File: LogbackConfigurer.java    From icure-backend with GNU General Public License v2.0 4 votes vote down vote up
public static void stopLogging() {
	// Stop the logback context
	LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
	loggerContext.stop();
}
 
Example 17
Source File: LogbackUtils.java    From logback-redis with Apache License 2.0 3 votes vote down vote up
/**
 * Shuts down logback. This ensures that the {@link RedisBatchAppender#stop()} method is called
 * which cleans up background threads and pools and ensures that remaining messages are
 * sent to Redis before shutting down the app.
 *
 * This functionality can also be performed with a shutdown hook in logback.xml
 * (&lt;shutdownHook class="ch.qos.logback.core.hook.DelayingShutdownHook"/&gt;)
 * but this doesn't work in Spring Boot apps when shut down with the actuator shutdown URL.
 *
 * Therefore, this method should be called in Spring Boot apps as follows:
 * 
 * <pre>
 *{@literal @}Component
 * public class LogbackStopListener implements ApplicationListener&lt;ContextClosedEvent&gt; {
 *  {@literal @}Override
 *   public void onApplicationEvent(ContextClosedEvent event) {
 *     LogbackUtils.stopLogback();
 *   }
 * }
 * </pre>
 */
public static void stopLogback() {
    LoggerFactory.getLogger(LogbackUtils.class).debug("shutting down logback");

    ILoggerFactory factory = LoggerFactory.getILoggerFactory();
    if (factory instanceof LoggerContext) {
        LoggerContext ctx = (LoggerContext) factory;
        ctx.stop();
    }
}