LogMonFilter
The LogMonFilter is a helpfull tool for monitoring your application. Basically it is a servlet filter along with a special log4j appender. The filter will intercept any request and the appender will write any log message to an internal buffer. This buffer is a threadlocal implementation limited to the thread and the request. After the request is done the filter will scan for any exception in the log messages. If there is one the entire bunch of messages will be send via email.
Hwo to use
First you need to add the filter and the mapping to the web.xml in the resources directory:
<filter>
<filter-name>LogMonFilter</filter-name>
<filter-class>org.jzonic.filter.logmonitor.LogMonFilter</filter-class>
<init-param>
<param-name>receiver</param-name>
<param-value>me@myhost.com</param-value>
</init-param>
<init-param>
<param-name>smtphost</param-name>
<param-value>ip address of your smtp server</param-value>
</init-param>
<init-param>
<param-name>sender</param-name>
<param-value>admin@localhost</param-value>
</init-param>
</filter>
There are 3 parameters that you need to define:
- receiver: the mail address where mails will be send to
- smtphost: the ip address of your smtp server
- sender: the sender mail address
Then add the mapping based on your ending:
<filter-mapping> <filter-name>LogMonFilter</filter-name> <url-pattern>*.step</url-pattern> </filter-mapping>
Make sure that this is the first filter mapping in the chain so you can cover the entire request cycle.
The last step is to add the special appender to the etc/log4j.properties. Here is an example:
log4j.appender.lm=org.jzonic.filter.logmonitor.LogMonAppender
log4j.appender.lm.layout=org.apache.log4j.PatternLayout
log4j.appender.lm.layout.ConversionPattern=%d{HH:mm:ss:SSS} %X{session} %5p %c{2}:%L - %m%n
Now you can add the appender to any logger you wish. The best way would be to add this one to all loggers:
log4j.logger.org.jzonic=error,file,lm