`
v5browser
  • 浏览: 1130485 次
社区版块
存档分类
最新评论

tomcat 日志 java.util.logging.Logger使用 (二)

 
阅读更多
# default file output is in user's home directory.  
java.util.logging.FileHandler.pattern = %h/java%u.log  
java.util.logging.FileHandler.limit = 50000  
java.util.logging.FileHandler.count = 1  
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter  
  
# Limit the message that are printed on the console to INFO and above.  
java.util.logging.ConsoleHandler.level = WARNING   
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter  

1:重写输出的日志的格式

需要配置一下java.util.logging.ConsoleHanlder.formatter 属性。

这个问题在java SE程序中没问题。 java程序可以通过该配置找到自定义的Formatter

但是放到Tomcat等web工程里。问题就出现了

且看ConsoleHandler的源码

public class ConsoleHandler extends StreamHandler {
    // Private method to configure a ConsoleHandler from LogManager
    // properties and/or default values as specified in the class
    // javadoc.
    private void configure() {
        LogManager manager = LogManager.getLogManager();
	String cname = getClass().getName();

	setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
	setFilter(manager.getFilterProperty(cname +".filter", null));
	setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
	try {
	    setEncoding(manager.getStringProperty(cname +".encoding", null));
	} catch (Exception ex) {
	    try {
	        setEncoding(null);
	    } catch (Exception ex2) {
		// doing a setEncoding with null should always work.
		// assert false;
	    }
	}
    }
其中有一行代码:这里用于设置配置文件里置顶的.formatter
setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
跟踪进去发现:ClassLoader.getSystemClassLoader 根本无法反射到指定的class

  Formatter getFormatterProperty(String name, Formatter defaultValue) {
	String val = getProperty(name);
	try {
	    if (val != null) {
		Class clz = ClassLoader.getSystemClassLoader().loadClass(val);
	        return (Formatter) clz.newInstance();
	    }
	} catch (Exception ex) {
	    // We got one of a variety of exceptions in creating the
	    // class or creating an instance.
	    // Drop through.
	}

继续查阅Tomcat的相关文档

http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html

System — This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, the standard Tomcat startup scripts ($CATALINA_HOME/bin/catalina.sh or %CATALINA_HOME%\bin\catalina.bat) totally ignore the contents of the CLASSPATH environment variable itself, and instead build the System class loader from the following repositories:


$CATALINA_HOME/bin/bootstrap.jar — Contains the main() method that is used to initialize the Tomcat server, and the class loader implementation classes it depends on.


$CATALINA_BASE/bin/tomcat-juli.jar or $CATALINA_HOME/bin/tomcat-juli.jar — Logging implementation classes. These include enhancement classes to java.util.logging API, known as Tomcat JULI, and a package-renamed copy of Apache Commons Logging library used internally by Tomcat. See logging documentation for more details.


If tomcat-juli.jar is present in $CATALINA_BASE/bin, it is used instead of the one in $CATALINA_HOME/bin. It is useful in certain logging configurations


$CATALINA_HOME/bin/commons-daemon.jar — The classes from Apache Commons Daemon project. This JAR file is not present in the CLASSPATH built by catalina.bat|.sh scripts, but is referenced from the manifest file of bootstrap.jar.

发现SystemClassLoader只能加载Tomcat-7.0.34\bin 目录下的三个Jar 。

也就是说如果需要继续使用JDK自带的log 则没办法重写Formater等。。。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics