Do I need to flush events when shutting down using logback?

后端 未结 4 1925
傲寒
傲寒 2021-01-08 01:02

We are migrating to logback from log4j for several web apps. In the shutdown of our application we currently call:

org.apache.log4j.LogManager.shutdown();
<         


        
相关标签:
4条回答
  • 2021-01-08 01:05

    I'm not aware of an overall manager shutdown like log4j's but I close all my individual context loggers when their context is destroyed using a ServletContextListener like so:

    ContextSelector selector = StaticLoggerBinder.getSingleton().getContextSelector();
    LoggerContext context = selector.detachLoggerContext(contextName);
    if (context != null) {
        Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
        context.reset();
    } else {
        System.err.printf("No context named %s was found", contextName);
    }
    

    Also, LoggerContext.stop() is svailable and does some of the same functions internally but I don't use it, so I can't comment on whether its better than reset or not.

    0 讨论(0)
  • 2021-01-08 01:09

    Here's a simple approach:

    import org.slf4j.ILoggerFactory;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import ch.qos.logback.classic.LoggerContext;
    
    ...
    
    ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
    // Check for logback implementation of slf4j
    if (loggerFactory instanceof LoggerContext) {
        LoggerContext context = (LoggerContext) loggerFactory;
        context.stop();
    }
    
    0 讨论(0)
  • 2021-01-08 01:13

    It seems that just adding <shutdownHook/> into configuration should stop the context.

    From logback docs:

    <configuration>
       <!-- in the absence of the class attribute, assume 
       ch.qos.logback.core.hook.DelayingShutdownHook -->
       <shutdownHook/>
      .... 
    </configuration>
    

    And from DelayingShutdownHook summary:

    ShutdownHook implementation that stops the Logback context after a specified delay. The default delay is 0 ms (zero).

    0 讨论(0)
  • 2021-01-08 01:23

    Version 1.1.10 onwards, logback takes care of stopping the current logback-classic context when the web-app is stopped or reloaded.

    Here's the updated doc: https://logback.qos.ch/manual/configuration.html#webShutdownHook

    0 讨论(0)
提交回复
热议问题