IAmYourFaja
IAmYourFaja

Reputation: 56912

Stopping Logback System for Clean Shutdown

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.

Upvotes: 10

Views: 4382

Answers (1)

Ceki
Ceki

Reputation: 27450

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.

Upvotes: 7

Related Questions