Share

Monday, August 22, 2016

Servlet Filtering with Apache Log4j

What is Servlet Filtering ?



Servlet filtering, a feature of servlets introduced in the Servlet 2.3 specification, provides a declarative technique for intercepting HTTP requests and performing any desired preprocessing or conditional logic before the request is passed on to the final target JSP page or servlet. 

Filters are very useful for implementing common behaviors such as caching page output, logging page requests, providing debugging information during testing, and checking security information and forwarding to login pages.

Placing a filter in the path of a particular servlet or JSP request is a simple two-step process: Build a class that implements the javax.servlet.Filter interface, and register that class as a filter for the desired pages and servlets using either entries in the web.xml descriptor file or annotations in the filter class. 


What is Apache Log4j

log4j is a java based logging utility.  which makes the application level logging ease and more reliable. 

It's an API , can be directly imported and used in your application as a jar file.

log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog, etc.
log4j has three main components:
  • loggers: Responsible for capturing logging information.
  • appenders: Responsible for publishing logging information to various preferred destinations.
  • layouts: Responsible for formatting logging information in different styles.
Audit_Filter Application (Filtering with Log4j)

We have designed an application named LoggerFilter to demonstrate/test this filtering and logging.

This is how our project look like in eclipse.




















Here I have used weblogic 12c as the application server where the application has been deployed.

Its necessary to import the log4j API jar file in the project using  

LoggerFilter(RightClick) -> Properties -> Java Build Path -> AddExternal JARs

Also we should copy the same jar file into the domain/lib directory of weblogic to avoid getting class not Found exceptions during  run time.






Preceding illustration shows my domain's lib directory (MWIDomain is my domain name)

This is the content of  log4j.properties file we have bundled into the war file.


log4j.logger.com.logger.app.SnoopFilter=TRACE, C
log4j.appender.C=org.apache.log4j.RollingFileAppender
log4j.appender.C.File=D:/Tomcat_logs/loggerapp1_debug.log
log4j.appender.C.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.C.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
#log4j.appender.R.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.C.MaxFileSize=200KB
log4j.appender.C.MaxBackupIndex=1


All the code used in this program can be found on my github you can download the war file as well from git hub and try it.

How it has been setup ?

in web.xml  SnoopFilter.java has been registered as Filter class for all incoming requests as we have used /* as our url matching pattern

<filter>
 <filter-name>AuditFilter</filter-name>
<filter-class>com.logger.app.SnoopFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuditFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

When you are accessing any of the resources in that project (Index.jsp/Home). Filter will monitor your request and print the request info into the log file configured in the log4j.properties file  (D:/Tomcat_logs/loggerapp1_debug.log)








Query String can be anything. What ever you give as a "query string" will be captured and printed.







With little modifications you can use this sample code for your project and filter your incoming requests.

Here we just do a Auditing by capturing the request information. we can do more using filter and log4j.

Hope it helps.

Thanks,
SaravAK