Thanks for the help.
Can you kindly please give me an example of filter which changes the log level.
I wrote below but it changes log level for all the message, I want to do sepecifically for some packages.
<filter>
<any>
<change-level new-level = "ERROR" />
<match pattern = "JBAS016330" />
</any>
</filter>
Below are files which I used in Jboss-4.2.2 GA. In Java class I used to do log it is INFO level.
Configuration:
<filter class="com.log4jextensions.filter.hierarchical.HierarchicalExceptionFilter">
<param name="rulesFile" value="jbossHierarchicalExceptionFilter.xml" />
<param name="enabled" value="true" />
<param name="logWhenHandled" value="true" />
<param name="logWhenNotHandled" value="false" />
</filter>
XML: jbossHierarchicalExceptionFilter
<?xml version="1.0" encoding="UTF-8"?>
<HierarchicalExceptions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="HierarchicalExceptionFilter.xsd">
<!-- couldn't restore view -->
<exception depth="any">
<exception match="javax.faces.application.ViewExpiredException" type="equals">
<exception depth="any"/>
</exception>
</exception>
<!-- thrown by Hibernate when row was updated or deleted by another transaction -->
<exception match="org.hibernate.StaleObjectStateException" type="equals"/>
<!-- thrown by FileUpload when it is cancelled/reset when in progress -->
<exception match="org.jboss.seam.web.FileUploadException" type="equals"/>
<!-- thrown by JSF when entity is not found -->
<exception depth="any">
<exception match="javax.faces.el.EvaluationException" type="equals">
<exception match="javax.ejb.EJBTransactionRolledbackException" type="equals">
<exception match="javax.persistence.EntityNotFoundException" type="equals"/>
</exception>
</exception>
</exception>
<!-- thrown during DataTable operations -->
<exception depth="any">
<exception match="com.openet.referenceDataGui.dbManagement.exceptions.DatabaseAccessException" type="equals">
<exception depth="any"/>
</exception>
</exception>
</HierarchicalExceptions>
Java: HierarchicalExceptionFilter
package com.log4jextensions.filter.hierarchical;
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;
public class HierarchicalExceptionFilter extends Filter {
private static final Logger LOGGER = Logger.getLogger(HierarchicalExceptionFilter.class);
private static final String SCHEMA_FILE = "HierarchicalExceptionFilter.xsd";
private static final String RULES_FILE = "hierarchicalExceptionFilter.xml";
private static final Class<?>[] importClasses = new Class[]{HierarchicalExceptions.class, HierarchicalException.class};
private HierarchicalExceptions exceptions = new HierarchicalExceptions();
private boolean initialzied = false;
private boolean initialzieError = false;
private String rulesFile = RULES_FILE;
private boolean enabled = true;
private boolean logWhenHandled = true;
private boolean logWhenNotHandled = false;
public HierarchicalExceptionFilter(){
}
public HierarchicalExceptionFilter(String rulesFile){
this.rulesFile = rulesFile;
}
private void initialize(){
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
try {
schema = sf.newSchema(getClass().getClassLoader().getResource(SCHEMA_FILE));
} catch (Exception e) {
initialzieError = true;
LOGGER.warn("Error parsing schema "+SCHEMA_FILE + " cause : "+e.getLocalizedMessage(), e);
return;
}
try
{
InputStream stream = getClass().getClassLoader().getResourceAsStream(rulesFile);
if (stream == null){
LOGGER.warn("Couldn't find "+rulesFile + " file.");
initialzieError = true;
return;
}
JAXBContext context = JAXBContext.newInstance(importClasses);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema(schema);
exceptions = (HierarchicalExceptions) unmarshaller.unmarshal(stream);
}catch (Exception e) {
initialzieError = true;
LOGGER.warn("Couldn't load "+rulesFile + " cause : "+e.getLocalizedMessage(), e);
}
}
@Override
public int decide(LoggingEvent event) {
return decide(event, -1);
}
/**
* if index is -1 than check all, if >= 0 check at position
*/
public int decide(LoggingEvent event, int index) {
if (!enabled) {
return ACCEPT;
}
if (!initialzieError) {
if (!initialzied ){
initialzied = true;
initialize();
}
if (initialzied) {
try {
if (event.getThrowableInformation() != null){
if (check(event, index)) {
if (logWhenHandled) {
LOGGER.info("Found and handled "+exceptions.getInfo(event.getThrowableInformation()));
}
return DENY;
}
else {
if (logWhenNotHandled) {
LOGGER.info("Not handled "+exceptions.getInfo(event.getThrowableInformation()));
}
return ACCEPT;
}
}
}
catch (Exception e) {
initialzieError = true;
LOGGER.error("Error during processing exceptions.", e);
}
}
}
return ACCEPT;
}
private boolean check(LoggingEvent event, int index) {
if (index == -1) {
return exceptions.matches(event.getThrowableInformation());
}
else {
return exceptions.matches(event.getThrowableInformation(), index);
}
}
public void setRulesFile(String rulesFile) {
this.rulesFile = rulesFile;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setLogWhenHandled(boolean logWhenHandled) {
this.logWhenHandled = logWhenHandled;
}
public void setLogWhenNotHandled(boolean logWhenNotHandled) {
this.logWhenNotHandled = logWhenNotHandled;
}
}