10

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>john.smith@example.com</to>
        <from>no-reply@example.com</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.

IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 1
    What does your logback.xml file look like? Which version of logback are you using? Which JDK? – Ceki May 28 '13 at 15:05
  • Thanks @Ceki (+1) - please see my updates for answers to all your questions. – IAmYourFaja May 28 '13 at 15:16
  • 1
    Set smtpAsynchronousSending in SMTPAppender to false and see if it makes a difference: http://logback.qos.ch/manual/appenders.html#smtpAsynchronousSending – Ceki May 28 '13 at 15:18
  • That was it @Ceki - thank you. If you add a quick answer explaining `asynchronousSending` I will happily give you the definitive answer for this - thanks again! – IAmYourFaja May 28 '13 at 15:21
  • Do you by any chance have a test case for the issue? If you do, please attach it to http://jira.qos.ch/browse/LOGBACK-872 or send it to me by email. – Ceki May 28 '13 at 15:26
  • Please also check https://gist.github.com/JonasGroeger/34bb004b30d18d8bc12642ba03141ac6 for file compression on logging context shutdown. – Jonas Gröger Apr 17 '18 at 14:23

1 Answers1

7

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.

Ceki
  • 26,753
  • 7
  • 62
  • 71