Content Pipeline Guide > Error Handling > Using log4j Methods
  
Using log4j Methods
If you do not need to know the error location, you can call log4j class methods to log errors.
The following example illustrates a filter that uses log4j to report messages. This filter counts the number of elements in the document instance and reports the number to the log. The log4j Category class is imported and an instance of Category logger is created using the getInstance static method.
package com.arbortext.epic.saxfilter.custom;
import java.util.Map;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
// log4j
import org.apache.log4j.Category;
import com.arbortext.epic.saxfilter.DefaultSAXFilter;
/**
* CountTags.java
*
* <p> This filter counts how many elements are in the
* current document.
*
* <pre>
* <FilterDef id="tagCounter"
* adapterClass=
"com.arbortext.epic.saxfilter.DefaultFilterAdapter"
* filterClass=
"com.arbortext.epic.saxfilter.custom.CountTags"
* type="transformer">
* <Label>Count Tags Filter</Label>
* <Documentation>
* This filter counts how many elements are there in
* the current document.
* </Documentation>
* </FilterDef>
* </pre>
*
*
* Created: Wed Sep 11 09:53:38 2002
*
*/
public class CountTags extends DefaultSAXFilter {
protected int count = 0;
private Category logger = Category.getInstance
(CountTags.class);
public void initFilter (Map params) throws
Exception {
super.initFilter(params);
//Reset counter for each run.
count = 0;
}
public void startElement (String NameSpaceURI,
String lName, String qName,
Attributes atts) throws SAXException {
count++; // increment the counter
super.startElement(NameSpaceURI, lName, qName, atts);
}
public void endDocument () throws SAXException {
//Report the total number of elements using log4j
//"info" method.
logger.info("There are total of " + count +
" elements in this document.");
super.endDocument();
}

}//CountTags
The debug, info, warn, error and fatal methods are the most frequently used logging methods in the Category class.
* 
Refer to the log4j web site at http://jakarta.apache.org/log4j/docs/ for more information on the Category class.