tomcat 如何设置`logging.properties`客户处理程序的属性

vxf3dgd4  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(43)

在尝试为Tomcat创建自定义处理程序(使用java.util.logging)时,我无法找到添加自定义属性的方法。
例如,如何设置处理程序的abc属性的值?

# logging.properties

handlers = java.util.logging.ConsoleHandler, com.company.tomcat.JdbcHandler

.handlers = java.util.logging.ConsoleHandler, com.company.tomcat.JdbcHandler

com.company.tomcat.JdbcHandler.level = FINE
com.company.tomcat.JdbcHandler.abc=hello

个字符

gj3fmq9x

gj3fmq9x1#

对于JDK和Tomcat,自定义处理程序必须在创建时与LogManager交互以读取解析和加载的值。您必须设置由java.util.logging. logging类定义的所有属性沿着新属性。
这里有一个例子来帮助你开始:

import java.util.logging.LogManager; //Use the JUL LogManager

public class JdbcHandler extends Handler {
  private String abc;

  public JdbcHandler() {
     final LogManager m = LogManager.getLogManager();
     final String p = getClass().getName(); //property prefix
 
     String lvl = m.getProperty(p + ".level"); //parse and set the level
     try {
        super.setLevel(lvl == null ? Level.ALL : Level.parse(lvl));
     } catch(RuntimeException failed) {
        this.reportError("Unable to set level", failed, ErrorManager.OPEN_FAILURE);
        super.setLevel(Level.ALL);
     }

    //TODO set other handler properties for formatter, filter, encoding, and errorManager.

     //set your property for JDK, Tomcat, etc.
     initAbc(m.getProperty(p + ".abc"));
  }

  //WildFly will support this way of assigning the property.
  public void setAbc(String abc) {
     initAbc(abc);
  }

  private void initAbc(String abc) {
    this.abc = abc;
  }

  @Override
  public void publish(LogRecord record) {
    if (!isLoggable(record) {
       return;
    }

    System.out.println("record " + record);     // this is called
  }
  public void close() {
     super.setLevel(Level.OFF);
  }
   
  public void flush() {
  }
}

字符串
保留公共setAbc方法是值得的,因为WildFly将使用该方法来分配属性。

相关问题