27
Slide 1 Logger Implementation Abhishek Chikane 1 st November 2010

Logger implementation

Embed Size (px)

DESCRIPTION

Common problems and solutions for log4j implementation in applications running on application servers like Weblogic, Websphere

Citation preview

Page 1: Logger implementation

Slide 1

Logger ImplementationAbhishek Chikane1st November 2010

Page 2: Logger implementation

Slide 2

Agenda

Current Log Implementation Problems!!!Log4j OverviewLog4j And Web ApplicationsLog4j And Class Loaders In Application ServersLog4j And The “Logging Separation” ProblemLog4j Implementation In Web ApplicationsLog4j And The “Log Rotation” ProblemDated Logger Implementation In Web ApplicationsFor You….

Page 3: Logger implementation

Slide 3

Current Log Implementation Problems!!!

Page 4: Logger implementation

Slide 4

Log statements mixing from different applications

Page 5: Logger implementation

Slide 5

No daily rotation of file which increases size of log file

Page 6: Logger implementation

Slide 6

Multiple Log Lines Printing

Page 7: Logger implementation

Slide 7

Log4j Overview

Page 8: Logger implementation

Slide 8

Log4j And Naming Hierarchy A logger is said to be an ancestor of another logger if its name followed by a dot 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

Log4j And Your Code Log4j normally needs to be configured only once. Some new users try to configure log4j in each and every class. This is very inefficient and just plain wrong.

Page 9: Logger implementation

Slide 9

Log4j And Web Applications

Page 10: Logger implementation

Slide 10

In practice placing log4j-VERSION.jar in the WEB-INF/lib directory of your web-application will cause log4j classes to be loaded/unloaded whenever your web application is loaded/unloaded. Moreover, each copy of the log4j classes will be treated as a separate unrelated copy by the JVM.

Under Tomcat 3.x and 4.x, you should place the log4j.xml or log4j.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

Page 11: Logger implementation

Slide 11

Log4j And Class Loaders In Application Servers

Page 12: Logger implementation

Slide 12

The Java class loader delegation model gives precedence to parent class loaders. This means that if log4j.jar is available on the CLASSPATH, or under JAVA_HOME/jar/lib/ext or to any class loader which is a parent of the webapplication's class loader, then that copy of log4j will be loaded into memory and shared by all web-applications.

Page 13: Logger implementation

Slide 13

Log4j And The “Logging Separation” Problem

Page 14: Logger implementation

Slide 14

What does separation of logging mean? In a separated logging environment, each web-application can configure log4j in different ways such that the configuration of one web-application cannot interfere with the logging configuration of another web application.

Since time immemorial users have struggled to control the logging configuration of multiple web-applications deployed on the same Servlet Container (e.g. Tomcat).

Page 15: Logger implementation

Slide 15

Log4j Implementation In Web Applications

Page 16: Logger implementation

Slide 16

First Solution To “Logging Separation” Problem

Assuming each web-application is loaded by a distinct class loader, then placing a copy of log4j.jar under WEB-INF/lib/ directory of each web-application will automatically result in distinct log4j-logging universes. Simply put, each webapplication will load its own distinct copy of log4j classes into memory. All such copies are invisible and inaccessible to each other.

Page 17: Logger implementation

Slide 17

Second Solution To “Logging Separation” Problem

Log4j allows different applications live in their own parallel universe by using a different LoggerRepository for each application. Given that each hierarchy (i.e. logger repository) manages its own separate logger tree, logging separation is a direct consequence of this approach.

The Java Servlet API mandates a unique ServletContext for each web application. Thus, a web-application can set an attribute for a Servlet context which can be shared by all Servlet and jsp pages of a web-application but remain invisible to other web-applications. In particular, an initialization Servlet can create, set and configure an independent logger hierarchy in the Servlet context. Subsequently, other Servlet can obtain the hierarchy stored in the Servlet context in order to retrieve logger instances. These logger instances will be attached to the particular hierarchy specific to the web-application.

Page 18: Logger implementation

Slide 18

Third Solution To “Logging Separation” Problem

This solution relies on the Servlet Container to keep track of theexecution context and provide a different logging environment for each context.

Each web application has its own class loader and Tomcat sets the Thread Context Classloader, or TCL, to be the class loader of the currently executing web application.

Servlet Container provides a separate hierarchy instance for eachweb-application. Each logger object that log4j creates is attached to a hierarchy. The Hierarchy class implements the LoggerRepository interface by arranging logger objects in a tree according to their name.

Page 19: Logger implementation

Slide 19

Log4j And The “Log Rotation” Problem

Page 20: Logger implementation

Slide 20

The log file rotation on Application servers fails with following error

log4j: ERROR Failed to rename [<file name>.log] to [<file name>.log.2010-03-23].

Page 21: Logger implementation

Slide 21

Log4j And “Multiple Line Printing” Problem

Page 22: Logger implementation

Slide 22

LoggerName

AddedAppenders

AdditivityFlag

Output Targets Comment

root A1 not applicable A1The root logger does not have a parent.

x A-x1, A-x2 true A1, A-x1, A-x2Appenders in root are added to appenders in "x".

x.y none true A1, A-x1, A-x2Appenders of "x" and root.

x.y.z A-xyz1 trueA1, A-x1, A-x2, A-xyz1

Appenders in "x.y.z", "x" and root.

security A-sec false A-sec

No appender accumulation as the additivity flag is set to false.

security.ACL none true A-sec

Only appenders of "security" as the additivity flag in "security" is set to false.

Example of Appender Additivity

Page 23: Logger implementation

Slide 23

Dated Logger Implementation In Web Applications

Page 24: Logger implementation

Slide 24

DatedFileAppender is an "appender" object designed for use with the Apache Log4j logging system.

It provides an Apache Tomcat style FileLogger implementation that differs from the file loggers provided with Log4j.

While the DatedFileAppender was written with Apache Tomcat in mind, it does not 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 other application jar files and add the jar file name to your CLASSPATH.

• Modify your log4j.properties file to use the DatedFileAppender class (see below for configuration options).

Page 25: Logger implementation

Slide 25

For You….

We have created the logger implementation guide.

Page 26: Logger implementation

Slide 26

Still Have Any Question?

Page 27: Logger implementation

Slide 27

Thank You !!!