Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 743
IAmYourFaja Avatar asked May 28 '13 14:05

IAmYourFaja


People also ask

How do I stop Logback logging off?

Logback does not allow logging to be disabled from the command line. However, if the configuration file allows it, you can set the level of loggers on the command line via a Java system property.

Is Log4j2 better than Logback?

So which one should you use? I recommend using Log4j2 because it's the fastest and most advanced of the three frameworks. Logback is still a good option, if performance is not your highest priority.

Where does Logback xml save to?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.

What is Appender in Logback?

Appenders place log messages in their final destinations. A Logger can have more than one Appender. We generally think of Appenders as being attached to text files, but Logback is much more potent than that. Layout prepares messages for outputting.


1 Answers

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.

like image 135
Ceki Avatar answered Nov 03 '22 23:11

Ceki