logback and slf4j
TRANSCRIPT
![Page 1: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/1.jpg)
SLF4J and LOGBack
Next Generation Logging!
31-08-2010
![Page 2: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/2.jpg)
LOGBack : History
It’s a successor of log4j
LOGBack together with SLF4J designed to be the next generation logging framework
There is no active developer community for log4j
![Page 3: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/3.jpg)
What is SLF4J?
Remember commons-logging?SLF4J is a façade for different logging toolsDifferent logging tools can be plugged without any code changeNo classloader and memory leak issueBetter support of MDC (Later!)No difference using with LOGBack but advantageous on the way!
![Page 4: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/4.jpg)
Hello SLF4J
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
public class HelloSLF4J { private static final Logger logger = LoggerFactory.getLogger(HelloSLF4J.class); public static void main(String[] args) {
logger.info("Hello World"); }}
![Page 5: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/5.jpg)
logger.debug("The entry is {}.", entry);
SLF4J Enforcing Best Practices!
if(logger.isDebugEnabled()) {logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
•Parameterised Logging•String manipulation after checking loglevel
![Page 6: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/6.jpg)
Exception Logging
logger.error("some accompanying message", e);
try { Integer i = Integer.valueOf(s);} catch (NumberFormatException e){ logger.error("Failed to format {}", s, e);}
![Page 7: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/7.jpg)
What is LOGBack?
“LOGBack is faster, reliable and feature upgraded logging tool compared to its predecessors“
![Page 8: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/8.jpg)
SLF4J and Logback : Dream Dates!
Logback implements SLF4J natively
No computational and memory overhead
Fully compliant to SLF4J
Faster they say! 10 times!! Smaller Blue Print
Core, classic and access modules
![Page 9: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/9.jpg)
Pre-Requirements
logback.xml/logback.groovy
slf4j-api.jar
logback-classic.jar
logback-core.jar
![Page 10: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/10.jpg)
Why Logback
Automatic Reloading• LOGLEVELs can be changed on the fly. • <configuration scan="true" scanPeriod="30 seconds" >
Prudent Model Recovery (3 times slower)• Multiple JVM can share same log file.• Graceful recovery from IO failures(100% disk usage) without
process restart. File Server recovery will let log files working automatically
Filters And MDCRolling Policies• Compress and Roll• Housekeeping with maxHistory property
![Page 11: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/11.jpg)
Why LogbackConditional Processing• Good For Dev/Prod switch
Is It Worth A Use?
![Page 12: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/12.jpg)
Why Logback
Stack Traces Pointing jar files
![Page 13: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/13.jpg)
logback.xmlConsoleAppenderConsoleAppender
RollingFileAppenderRollingFileAppender
rollover daily or whenever the file size reaches 100MB rollover daily or whenever the file size reaches 100MB
![Page 14: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/14.jpg)
logback.groovy
import ch.qos.logback.classic.encoder.PatternLayoutEncoderimport ch.qos.logback.core.ConsoleAppender
import static ch.qos.logback.classic.Level.DEBUG
appender("STDOUT", ConsoleAppender) { encoder(PatternLayoutEncoder) { pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
%msg%n" }}root(DEBUG, ["STDOUT"])
![Page 15: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/15.jpg)
Best Practice
Though the samples here used are xml for our understanding, groovy is simpler
Maven will take care most!• src/test/resources/logback-test.xml
• src/main/resources/logback.xml
• No setting required, scanner checks groovy, test and production in an order!
![Page 16: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/16.jpg)
MDC
MDC : Map Diagnostic Context• Helps to uniquely stamp requests
• Similar to NDC : Nested Diagnostic Context
• Correlating the logs effectively
The MDC manages contextual information on a per thread basis(and its children)
![Page 17: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/17.jpg)
MDC Use Case: User Based Loggingpublic class UserServletFilter implements Filter { private final String USER_KEY = "username"; public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; Principal principal = req.getUserPrincipal(); if (principal != null) { String username = principal.getName(); MDC.put(USER_KEY, username); } try { chain.doFilter(request, response); } finally { MDC.remove(USER_KEY); } }}
![Page 18: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/18.jpg)
MDC Use Case: User Based Logging
<appender name="CONSOLE“ class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern>%-4r [%thread] %-5level C:%X{username} - %msg%n</Pattern> </layout> </appender>
![Page 19: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/19.jpg)
MDC Use Case: InsertingServletFilter
%X{req.remoteHost}
%X{req.requestURI}%n%d - %m%n
![Page 20: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/20.jpg)
MDC Use Case: SiftingAppender
Separate logging based runtime attributeslogger.debug("Application started");
MDC.put("userid", "Alice");
logger.debug("Alice says hello");
![Page 21: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/21.jpg)
Filters: LevelFilter
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFO</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <encoder> <pattern> %-4relative [%thread] %-5level %logger{30} - %msg%n </pattern> </encoder> </appender>
![Page 22: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/22.jpg)
Filters: ThresholdFilter
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <!-- deny all events with a level below INFO, that is TRACE and DEBUG --> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <encoder> <pattern> %-4relative [%thread] %-5level %logger{30} - %msg%n </pattern> </encoder> </appender> <root level="DEBUG"> <appender-ref ref="CONSOLE" /> </root></configuration>
![Page 23: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/23.jpg)
Filters:EvaluatorFilter
<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.core.filter.EvaluatorFilter"> <evaluator>
<expression>message.contains("billing")</expression> </evaluator> <OnMismatch>NEUTRAL</OnMismatch> <OnMatch>DENY</OnMatch> </filter> <encoder> <pattern> %-4relative [%thread] %-5level %logger - %msg%n </pattern> </encoder> </appender>
<root level="INFO"> <appender-ref ref="STDOUT" /> </root></configuration>
![Page 24: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/24.jpg)
Good To Know!
Logback-access• powerful HTTP-access log with tomcat and jetty
• UI based
• JMX support
• Statistical Data of UI accesses
• TeeFilter, CountingFilter, ViewStatusMessagesServlet
Tools : SLF4J Migrator tool
![Page 25: LOGBack and SLF4J](https://reader036.vdocuments.mx/reader036/viewer/2022062513/555e22ded8b42a6a4c8b4c96/html5/thumbnails/25.jpg)
DANKE!!!