First, you need a PrintWriter as a bridge from JTidy to Log4j:
public class Log4jPrintWriter extends PrintWriter {
Priority level;
Category cat;
StringBuffer text = new StringBuffer("");
public Log4jPrintWriter(org.apache.log4j.Category cat, org.apache.log4j.Priority level) {
super(System.err); // PrintWriter doesn't have default constructor.
this.level =level;
this.cat = cat;
}
// overrides all the print and println methods for 'print' it to the constructor's Category
public void close(){
flush();
}
public void flush(){
if (!text.toString().equals("")){
cat.log(level,text.toString());
text.setLength(0);
}
}
public void print(boolean b){
text.append(b);
}
public void print(char c){
text.append(c);
}
public void print(char[] s){
text.append(s);
}
public void print(double d){
text.append(d);
}
public void print(float f){
text.append(f);
}
public void print(int i){
text.append(i);
}
public void print(long l){
text.append(l);
}
public void print(Object obj){
text.append(obj);
}
public void print(String s){
text.append(s);
}
public void println(){
if (!text.toString().equals("")){
cat.log(level,text.toString());
text.setLength(0);
}
}
public void println(boolean x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(char x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(char[] x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(double x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(float x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(int x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(long x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(Object x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
public void println(String x){
text.append(x);
cat.log(level,text.toString());
text.setLength(0);
}
}
Thanks to JD Evora for this.
Then, declare your Logger and your PrintWriter
private static Logger log = Logger.getLogger(HtmlProcessor.class);
private static Log4jPrintWriter log4j = new Log4jPrintWriter(log, Level.DEBUG);
And finally, make JTidy work with it:
InputStream pageStream = new ByteArrayInputStream(html.getBytes("UTF-8"));
Tidy tidy = new Tidy();
tidy.setOnlyErrors(true); //<------------------
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setQuiet(true);
tidy.setShowWarnings(false);
tidy.setErrout(log4j);
dom = tidy.parseDOM(pageStream, null);
dom.normalize();
...
Enjoy your clean catalina.out!
Nessun commento:
Posta un commento