停止Logback系统以实现干净关闭

停止Logback系统以实现干净关闭

本文介绍了停止Logback系统以实现干净关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在尝试使用,并且直接从Eclipse内部运行示例。当我这样做时,我注意到 - 即使在我的静态 main(String [] args)方法结束后(从我的Java驱动程序类中),应用程序仍在运行。 / p>

我最终确定Logback正在管理自己的线程,即使在我的主应用程序退出之后它仍然存活。我搜索了一些解决方案并发现这是一种从Java内部显式关闭Logback的方法:

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

这真的是干净地关闭回归的唯一方法吗? / strong>我从来没有遇到过一个日志系统(JUL,JCL,log4j等),它会让你明确地关闭它以便优雅地退出你的应用程序......



提前致谢!



更新:此处为 logback.xml

 < configuration debug =truescan =truescanPeriod =5 minutes> 
< appender name =logManager-consoleAppenderclass =ch.qos.logback.core.ConsoleAppender>
< filter class =ch.qos.logback.classic.filter.LevelFilter>
< level> TRACE< / level>
< onMatch> ACCEPT< / onMatch>
< onMismatch> NEUTRAL< / onMismatch>
< / filter>
< filter class =ch.qos.logback.classic.filter.LevelFilter>
< level> DEBUG< / level>
< onMatch> ACCEPT< / onMatch>
< onMismatch> DENY< / onMismatch>
< / filter>
< encoder>
< pattern>%d {HH:mm:ss.SSS} [%thread]%-5level%logger {36} - %msg%n< / pattern>
< / encoder>
< / appender>

< appender name =logManager-smtpAppenderclass =ch.qos.logback.classic.net.SMTPAppender>
< filter class =ch.qos.logback.classic.filter.LevelFilter>
< level> WARN< / level>
< onMatch> ACCEPT< / onMatch>
< onMismatch> NEUTRAL< / onMismatch>
< / filter>
< filter class =ch.qos.logback.classic.filter.LevelFilter>
< level> ERROR< / level>
< onMatch> ACCEPT< / onMatch>
< onMismatch> DENY< / onMismatch>
< / filter>
< smtpHost> my.smtp.host< / smtpHost>
< to> [email protected]< / to>
< from> [email protected]< / from>
< username> my_user< / username>
< password> my_password< / password>
< subject>%logger {20} - %m< / subject>
< layout class =ch.qos.logback.classic.html.HTMLLayout/>
< cyclicBufferTracker class =ch.qos.logback.core.spi.CyclicBufferTracker>
< bufferSize> 5< / bufferSize>
< / cyclicBufferTracker>
< / appender>

< root level =ALL>
< appender-ref ref =logManager-consoleAppender/>
< appender-ref ref =logManager-smtpAppender/>
< / root>
< / configuration>

使用logback-1.0.13和JDK 1.6u34。

解决方案

问题可能是由于logback中的错误。在SMTPAppender中将设置为true可以避免错误(无需实际修复)。我已经在我们的jira中添加了来描述这个问题。


I've been experimenting with logback recently and have been running examples directly from inside Eclipse. When I do this, I notice that - even after my static main(String[] args) method ends (from inside my Java driver class), the application keeps running.

I eventually ascertained that Logback is managing its own threads that are staying alive even after my main application exits. I googled around for some solutions and found this as a way of explicitly shutting down Logback from inside Java:

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

Is this really the only way of shutting down logback cleanly? I've never encountered a logging system (JUL, JCL, log4j, etc.) that makes you explicitly shut it down in order to gracefully exit your application...

Thanks in advance!

Update: here is logback.xml:

<configuration debug="true" scan="true" scanPeriod="5 minutes">
    <appender name="logManager-consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>TRACE</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>NEUTRAL</onMismatch>
        </filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="logManager-smtpAppender" class="ch.qos.logback.classic.net.SMTPAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARN</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>NEUTRAL</onMismatch>
        </filter>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
        <smtpHost>my.smtp.host</smtpHost>
        <to>[email protected]</to>
        <from>[email protected]</from>
        <username>my_user</username>
        <password>my_password</password>
        <subject>%logger{20} - %m</subject>
        <layout class="ch.qos.logback.classic.html.HTMLLayout"/>
        <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
            <bufferSize>5</bufferSize>
        </cyclicBufferTracker>
    </appender>

    <root level="ALL">
        <appender-ref ref="logManager-consoleAppender" />
        <appender-ref ref="logManager-smtpAppender" />
    </root>
</configuration>

Using logback-1.0.13 and JDK 1.6u34.

解决方案

The issue is probably due to a bug in logback. Setting asynchronousSending to true in SMTPAppender circumvents the bug (without actually fixing it). I have added a new issue in our jira describing the problem.

这篇关于停止Logback系统以实现干净关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:40