1

我正在使用 Netbeans7.3 在 GlassFish Server Open Source Edition 3.1.2.2(内部版本 5)上进行编码。我使用 java.util.Logging 并更改了控制台的日志格式 - glassfish 日志没有更改。当我将根 Logger 级别设置为 Level.ALL,然后在包或类的基础上微调级别时,我遇到了来自 com.sun.enterprise.container 的各种 getJNDIName 方法发送的大量消息。 common.impl.ComponentEnvManagerImpl。

问题是它们似乎被发送到一个空记录器,显然我无法访问它以将其设置为更安静的级别。

我尝试使用非空字符串“null”访问记录器,但这也不行:)

这是我的 customFormatter :

public class CustomFormatter extends Formatter {

@Override
public String format(LogRecord record) {
    StringBuilder sb = new StringBuilder();
    sb.append(record.getLevel());
    sb.append(":");
    sb.append(record.getLoggerName());
    sb.append("\t[");
    sb.append(record.getSourceMethodName());
    sb.append(" @ ");
    sb.append(record.getSourceClassName());
    sb.append("] ");        
    sb.append(new Date(record.getMillis()).toString());        
    sb.append("\n\t\t ");
    sb.append(formatMessage(record));
    sb.append("\n");
    return sb.toString();
}

我以这种方式配置它,在 @Startup @Singleton EJB 内:

protected static void configureLogger() {
    try {
        Logger.getLogger(StartUpSingleton.class.getName()).log(Level.INFO, "Preparing to configure Logger");
        Handler hh[] = Logger.getLogger("").getHandlers();

        Logger.getLogger(StartUpSingleton.class.getCanonicalName()).log(Level.INFO, "There are {0} available handlers", hh.length);
        for (Handler hf : hh) {
            Logger.getLogger(StartUpSingleton.class.getName()).log(Level.INFO, "Handler found : {0}", hf.toString());
            if (java.util.logging.ConsoleHandler.class.isInstance(hf)) {
                hf.setFormatter(new CustomFormatter());
                CustomLogLevel.configure(hf);
            }

        }


        Logger.getLogger(StartUpSingleton.class.getName()).log(Level.INFO, "Done with  configure Logger");

    } catch (SecurityException ex) {
        Logger.getLogger(StartUpSingleton.class.getName()).log(Level.SEVERE, null, ex);
    }


}
}

使用以下配置对象设置我想要的所有详细信息级别:

class CustomLogLevel {

static void configure(Handler hf) {

    hf.setLevel(Level.ALL);
    Logger.getLogger("").setLevel(Level.ALL);
    // Logger.getLogger(null).setLevel(Level.INFO) is impossible !
    Logger.getLogger("null").setLevel(Level.INFO); // I tried ... ;-)
    Logger.getLogger("com.sun").setLevel(Level.INFO);
    Logger.getLogger("org").setLevel(Level.INFO);
    Logger.getLogger("grizzly").setLevel(Level.INFO);
    Logger.getLogger("global").setLevel(Level.INFO);
    Logger.getLogger("sun").setLevel(Level.INFO);
    Logger.getLogger("LogStrings").setLevel(Level.INFO);

    Logger.getLogger("com.myprivate.package").setLevel(Level.ALL);
    // ...



}

}

这是我在控制台上得到的日志,被与我无关的 FINE 或 FINER/FINEST 事件溢出(但我希望能够看到我自己的任何级别的事件,因此,将全局级别设置为 info 不会为我工作...)

FINEST:null [getJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getCurrentJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getCurrentJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getCurrentJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getCurrentJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getCurrentJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getCurrentJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getCurrentJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
     ComponentEnvManagerImpl: getCurrentJndiNameEnvironment 123ProductV1_/123ProductV1 is class com.sun.enterprise.deployment.WebBundleDescriptor
FINEST:null [getJndiNameEnvironment @ com.sun.enterprise.container.common.impl.ComponentEnvManagerImpl] Mon Apr 29 17:11:36 CEST 2013
... / ... 

关于如何中和日志泛滥(没有 xml 配置)的任何建议?

泽维尔

4

1 回答 1

0

已解决 - 这显然不是一个错误(这些是 AnonymousLogger 吗?),因为各种其他包正在创建具有空名称的日志消息。我决定通过使用格式化程序过滤掉这些问题来解决这个问题:

@Override
public String format(LogRecord record) {
    if(record.getLoggerName() == null) {
          return ;
          }
    StringBuilder sb = new StringBuilder();
    sb.append(record.getLevel());
    sb.append(":");
    .../...

它运行良好,允许拥有我自定义的控制台视图,而无需修改任何服务器或文件配置......

于 2013-05-03T16:18:23.327 回答