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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With