Notes
Slide Show
Outline
1
Log4J
  • Sandeep Jassal
2
Topics of Discussion
  • Configuring Log4J
  • Architecture of Log4J
  • Example code
  • Viewing log files(logfactor5 and Chainsaw)
  • Custom Levels and Log4JServlet
  • Using the resource bundle


3
Log4J background
  • Originally developed by IBM at their Zurich research lab.(www.zurich.ibm.com)
  • Currently maintained by Source Forge(www.sourceforge.net).
  • Open source
4
Configuring Tomcat for Log4j
  • Put  the log4j.properties file in the classes directory
  • Use a Servlet to configure Log4j.
  •          PropertyConfigurator.configure(fullConfigFilePath);


5
Log4J  Levels
  • ALL
  • DEBUG
  • WARNING
  • INFO
  • ERROR
  • FATAL
  • OFF


  • It is possible to have custom levels in between these levels
6
Architecture
  • Log4J consists of
  •      LogFactory
  •      Loggers
  •      Appenders
  •      Layout
7
Loggers
  • Loggers are used to write the logging events.
  • Do not Use Category its just for backward compatibility.
  • Loggers have a named hierarchy
  • edu.sc.asg.action is the parent of edu.sc.asg.action.MainAction
  • Child loggers inherit properties from the parents And can override them.
  • Root logger resides at the top of the hierarchy.
  •          Its always there. And you cannot get it by name.
  •          Logger getRootLogger();
  •          Logger getLogger(String name);
  •          Logger getLogger(String name, Loggerfactory);


8
Loggers API
  • log.debug(Object), log.debug(object,throwable)
  • log. error(Object), log.error(object,throwable)
  •   Similarly for other levels
  • log.log(Priority, Object) , log.log(Priority, Object, throwable)


  • To check weather the particular log level is enabled or not.
  • log.isDebugEnabled(), log.isInfoEnabled(), log.isEnabledFor(Priority)


  • Level class extends Priority, so you can you Level.DEBUG in place of Priority.
9
Appenders
  • Appenders is a object that sends log messages to final destinations. File, email, database, NTEventlog, Console…
  • Common Appenders
  • FileAppender, RollingFileAppender, DailyRollingFileAppender
  • JDBCAppender, SMTPAppender, ConsoleAppender


  • Example1
10
Layouts
  • Layouts format the log message.
  • Common Layouts
  • PatternLayout, HTMLLayout
  • Example1


11
PatternLayout – Customize your message
  • Used to customize the layout of a log entry. The format is closely related to conversion pattern of the printf function in ‘c’ The following options are available:
  • c - Used to output the category of the logging event.
  • C - Used to output the fully qualified class name of the caller issuing the logging request.
  • d - Used to output the date of the logging event. The date conversion specifier may be followed by a date format specifier enclosed between braces. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. If no date format specifier is given then ISO8601 format is assumed
  • F - Used to output the file name where the logging request was issued.
  • l - Used to output location information of the caller which generated the logging event. (C+M+L)
  • L - Used to output the line number from where the logging request was issued.
12
PatternLayout – Customize your message
  •  n - Outputs the platform dependent line separator character or characters.
  • M - Used to output the method name where the logging request was issued.
  • p - Used to output the priority of the logging event.
  • t - Used to output the name of the thread that generated the logging event.
  • x - Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.


13
NDC
  • Nested Diagnostic Context.
  • It’s a stack based Object that will keep track of the context in which the logging is being done.
  • Log4J starts a new NDC for each thread.
  • NDC can inherit the context of the parent.
  •     NDC.push(function name with parameters)
  •     NDC.pop()
  •  MainAction.java
14
MDC
  • MDC
  • Mapped Diagnostic Context.
  • It’s a map based object that will keep track of the context in which the logging is being done.
  • MDC automatically inherits the context of the parent thread.


15
Hidden costs of logging
  • if(log.isDebugEnabled() {
  •    log.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
  • }
  •  log.isDebugEnabled()
  •  log.isInfoEnabled()
  •  log.isEnabledFor(Priority)
16
Priorities
  • Five recognized message priorities:
    • DEBUG,INFO,WARN,ERROR ,FATAL
  • Priority specific log methods following the the form:
    • debug(Object message);
    • debug(Object message, Throwable throwable);
  • General log methods for wrappers and cutom priorites:
    • log(Priority level, Object message);
    • log(Priority level, Object message,Throwable throwable);
  • Localized log methods supporting ResourceBundles:
    • L7dlog(Priority level, String message, Throwable throwable)
    • L7dlog(Priority level, String message, Object[] params, Throwable throwable)
    • setResourceBundle(ResourceBundle);


17
Logfactor5
  • Logfactor5
  • chainsaw


18
Using Resource Bundle
  • log.l7dlog(Level.DEBUG,"error.test1",new Object[]{"a","b"},null);


  • MyLogger.java, MyLoggerFactory.java, Xlevel.java


  • Log4JServlet.java  , web.xml , log4jResources.properties


19
Questions