SLF4J and LOGBack  Next Generation Logging! 31-08-2010
LOGBack : History <ul><li>It’s a successor of log4j </li></ul><ul><li>LOGBack together with SLF4J designed to be the next ...
What is SLF4J? <ul><li>Remember commons-logging? </li></ul><ul><li>SLF4J is a façade for different logging tools </li></ul...
Hello SLF4J <ul><li>import  org.slf4j.Logger ; </li></ul><ul><li>import  org.slf4j.LoggerFactory ; </li></ul><ul><li>publi...
SLF4J Enforcing Best Practices! <ul><li>if(logger.isDebugEnabled()) { logger.debug(&quot;Entry number: &quot; + i + &quot;...
Exception Logging <ul><li>logger.error(&quot;some accompanying message&quot;, e);  </li></ul>try {    Integer i = Integer....
What is LOGBack? <ul><ul><li>“ LOGBack is faster, reliable and feature upgraded logging tool compared to its predecessors“...
SLF4J and Logback : Dream Dates! <ul><li>Logback implements SLF4J natively </li></ul><ul><li>No computational and memory o...
Pre-Requirements <ul><li>logback.xml/logback.groovy </li></ul><ul><li>slf4j-api.jar </li></ul><ul><li>logback-classic.jar ...
Why Logback <ul><li>Automatic Reloading </li></ul><ul><ul><li>LOGLEVELs can be changed on the fly.  </li></ul></ul><ul><ul...
Why Logback <ul><li>Conditional Processing </li></ul><ul><ul><li>Good For Dev/Prod switch </li></ul></ul>Is It Worth A Use?
Why Logback <ul><li>Stack Traces Pointing jar files </li></ul>
logback.xml ConsoleAppender RollingFileAppender rollover daily or whenever the  file size reaches 100MB
logback.groovy <ul><li>import ch.qos.logback.classic.encoder.PatternLayoutEncoder </li></ul><ul><li>import ch.qos.logback....
Best Practice <ul><li>Though the samples here used are xml for our understanding, groovy is simpler </li></ul><ul><li>Mave...
MDC <ul><li>MDC : Map Diagnostic Context </li></ul><ul><ul><li>Helps to uniquely stamp requests </li></ul></ul><ul><ul><li...
MDC Use Case: User Based Logging <ul><li>public class UserServletFilter implements Filter { </li></ul><ul><li>private fina...
MDC Use Case: User Based Logging <ul><li>   <appender name=&quot;CONSOLE“ class=&quot;ch.qos.logback.core.ConsoleAppender&...
MDC Use Case: InsertingServletFilter  <ul><li>%X{req.remoteHost} </li></ul><ul><li>%X{req.requestURI}%n%d - %m%n  </li></ul>
MDC Use Case: SiftingAppender <ul><li>Separate logging based runtime attributes </li></ul><ul><ul><li>logger.debug(&quot;A...
Filters: LevelFilter <ul><ul><li><appender name=&quot;CONSOLE&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;>...
Filters: ThresholdFilter <ul><li><configuration>   <appender name=&quot;CONSOLE&quot;     class=&quot;ch.qos.logback.core....
Filters:EvaluatorFilter  <ul><li><configuration>   <appender name=&quot;STDOUT&quot; class=&quot;ch.qos.logback.core.Conso...
Good To Know! <ul><li>Logback-access </li></ul><ul><ul><li>powerful HTTP-access log with tomcat and jetty </li></ul></ul><...
DANKE!!!
Upcoming SlideShare
Loading in …5
×

LOGBack and SLF4J

2,082 views
1,954 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,082
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
26
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

LOGBack and SLF4J

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

×