Slide 2Agenda Current Log Implementation Problems!!! Log4j Overview Log4j And Web Applications Log4j And Class Loaders In Application Servers Log4j And The “Logging Separation” Problem Log4j Implementation In Web Applications Log4j And The “Log Rotation” Problem Dated Logger Implementation In Web Applications For You….
Slide 8Log4j normally needs to be configured only once. Some new users try toconfigure log4j in each and every class. This is very inefficient and just plainwrong.A logger is said to be an ancestor of another logger if its name followed by adot is a prefix of the descendant logger name.The root logger resides at the top of the logger hierarchy.e.g. x.y is parent of x.y.z
Slide 10In practice placing log4j-VERSION.jar in the WEB-INF/lib directory of yourweb-application will cause log4j classes to be loaded/unloaded whenever yourweb application is loaded/unloaded. Moreover, each copy of the log4j classeswill be treated as a separate unrelated copy by the JVM.Under Tomcat 3.x and 4.x, you should place the log4j.xml orlog4j.properties under the WEB-INF/classes directory of your web-applications. Log4j will find the properties file and initialize itself. WEB-INF Lib • log4j-version.jar Classes • log4j.xml/log4j.properties
Slide 11Log4j And Class Loaders InApplication Servers
Slide 12The Java class loader delegation model gives precedence to parent classloaders. This means that if log4j.jar is available on the CLASSPATH, orunder JAVA_HOME/jar/lib/ext or to any class loader which is a parent of thewebapplications class loader, then that copy of log4j will be loaded intomemory and shared by all web-applications.
Slide 13Log4j And The “Logging Separation”Problem
Slide 14In a separated logging environment, each web-application can configure log4jin different ways such that the configuration of one web-application cannotinterfere with the logging configuration of another web application.Since time immemorial users have struggled to control the loggingconfiguration of multiple web-applications deployed on the same ServletContainer (e.g. Tomcat).
Slide 15Log4j Implementation In WebApplications
Slide 16Assuming each web-application is loaded by a distinct class loader, thenplacing a copy of log4j.jar under WEB-INF/lib/ directory of each web-application will automatically result in distinct log4j-logging universes. Simplyput, each webapplication will load its own distinct copy of log4j classes intomemory. All such copies are invisible and inaccessible to each other.
Slide 17Log4j allows different applications live in their own parallel universe by usinga different LoggerRepository for each application. Given that each hierarchy(i.e. logger repository) manages its own separate logger tree, loggingseparation is a direct consequence of this approach.The Java Servlet API mandates a unique ServletContext for each webapplication. Thus, a web-application can set an attribute for a Servlet contextwhich can be shared by all Servlet and jsp pages of a web-application butremain invisible to other web-applications.In particular, an initialization Servlet can create, set and configure anindependent logger hierarchy in the Servlet context. Subsequently, otherServlet can obtain the hierarchy stored in the Servlet context in order to retrievelogger instances.These logger instances will be attached to the particular hierarchy specific tothe web-application.
Slide 18This solution relies on the Servlet Container to keep track of theexecution context and provide a different logging environment for eachcontext.Each web application has its own class loader and Tomcat sets the ThreadContext Classloader, or TCL, to be the class loader of the currentlyexecuting web application.Servlet Container provides a separate hierarchy instance for eachweb-application. Each logger object that log4j creates is attached to ahierarchy. The Hierarchy class implements the LoggerRepository interface byarranging logger objects in a tree according to their name.
Slide 20The log file rotation on Application servers fails with following errorlog4j: ERROR Failed to rename [<filename>.log] to [<file name>.log.2010-03-23].
Slide 21Log4j And “Multiple Line Printing”Problem
Slide 22Logger Added Additivity Output Targets CommentName Appenders Flag The root logger doesroot A1 not applicable A1 not have a parent. Appenders in rootx A-x1, A-x2 true A1, A-x1, A-x2 are added to appenders in "x". Appenders of "x" andx.y none true A1, A-x1, A-x2 root. A1, A-x1, A-x2, A- Appenders in "x.y.z",x.y.z A-xyz1 true xyz1 "x" and root. No appender accumulation as thesecurity A-sec false A-sec additivity flag is set to false. Only appenders of "security" as thesecurity.ACL none true A-sec additivity flag in "security" is set to false.
Slide 24DatedFileAppender is an "appender" object designed for use with theApache Log4j logging system.It provides an Apache Tomcat style FileLogger implementation that differsfrom the file loggers provided with Log4j.While the DatedFileAppender was written with Apache Tomcat in mind, it doesnot depend on Tomcat and may be used in any other environment.How to use it?•Copy the datedFileAppender-1.0.2.jar to the same directory as your otherapplication jar files and add the jar file name to your CLASSPATH.•Modify your log4j.properties file to use the DatedFileAppender class (seebelow for configuration options).