Tomcat6 doesn't seem to load my LogFormatter class

I want to customize my log messages in Tomcat6 and created a class "MyFormatter" that looks like this:

public class LogFormatter extends Formatter { @Override public String format(LogRecord record) { StringBuilder sb = new StringBuilder(); sb.append("LOLCAT--") .append(new Date(record.getMillis())) .append(" \t") .append(record.getThreadID()) .append(" \t") .append(record.getSourceMethodName()) .append(" \t") .append(record.getSourceClassName()) .append(" \t") .append(record.getLevel().getLocalizedName()) .append(": ") .append(formatMessage(record)) .append(System.getProperty("line.separator")); return sb.toString(); } } 

I packed this in .jar and put it in $ {catalina.home} / lib.

In my logging.properties file, I added the following:

 1catalina.org.apache.juli.FileHandler.level = FINE 1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs 1catalina.org.apache.juli.FileHandler.prefix = lolcat. 1catalina.org.apache.juli.FileHandler.formatter = my.package.LogFormatter 

After trying sevral to try different packaging, different configs, I decided to try the built-in "org.apache.juli.OneLineFormatter" - and it works fine. Therefore, the configuration should be good.

The question remains, why is Tomcat6 not loading my class?

+4
source share
1 answer

I have found a solution.

After reading Tomcat and Class Loading, I found that there is an order that Tomcat follows. This happens as follows:

Bootstrap (/ jre / lib / ext) -> System (/ catalina-home / bin /) -> Common (/ catalina-home / lib) -> Webapps.

tomcat-juli.jar, which contains logging material, is loaded using the "System" -step, so when you post other logging materials, it ignores it because it is already loaded.

Then the solution should put .jar before tomcat-juli.jar is loaded in / jre / lib / ext. '

Edit: It is not always a good idea to save it in the jre folder, so I found that the best solution is to put it in an approved directory.

 -Djava.endorsed.dirs=${catalina_home}/endorsed 

This approved directory will be started prior to booting System .

+5
source

All Articles