2

我正在尝试编写具有特殊配置的自定义附加程序,并且我需要为其 xml 配置制作相应的标签。从 logback 手册中可以清楚地看到应该如何完成:通过向配置器添加新规则,如下所述:http: //logback.qos.ch/manual/onJoran.html

我的问题是当配置器不是直接创建时如何添加这个规则,但是当它由 slf4j 绑定创建时。当您第一次访问 slf4j-api aLoggerFactory时,slf4j-api 将在类路径中查找可用的绑定。如果你正确地找到logback-classic了绑定,那就是 class StaticLoggerBinder。它的初始化显示

  try {
      try {
        new ContextInitializer(defaultLoggerContext).autoConfig();
      } catch (JoranException je) {
        Util.report("Failed to auto configure default logger context", je);
      }
      // logback-292
      if(!StatusUtil.contextHasStatusListener(defaultLoggerContext)) {
        StatusPrinter.printInCaseOfErrorsOrWarnings(defaultLoggerContext);
      }
      contextSelectorBinder.init(defaultLoggerContext, KEY);
      initialized = true;
    } catch (Throwable t) {
      // we should never get here
      Util.report("Failed to instantiate [" + LoggerContext.class.getName()
          + "]", t);
    }
  }

如果我们查看内部,ContextInitializer.autoconfig()我们会看到如果初始化文件可用,它会将工作委托给

public void configureByResource(URL url) throws JoranException {
    if (url == null) {
      throw new IllegalArgumentException("URL argument cannot be null");
    }
    if (url.toString().endsWith("groovy")) {
      if (EnvUtil.isGroovyAvailable()) {
        // avoid directly referring to GafferConfigurator so as to avoid
        // loading  groovy.lang.GroovyObject . See also http://jira.qos.ch/browse/LBCLASSIC-214
        GafferUtil.runGafferConfiguratorOn(loggerContext, this, url);
      } else {
        StatusManager sm = loggerContext.getStatusManager();
        sm.add(new ErrorStatus("Groovy classes are not available on the class path. ABORTING INITIALIZATION.",
                loggerContext));
      }
    }
    if (url.toString().endsWith("xml")) {
      JoranConfigurator configurator = new JoranConfigurator();
      configurator.setContext(loggerContext);
      configurator.doConfigure(url);
    }
  }

如您所见,无法在创建时将规则添加到用于解析 logback.xml 的配置器中。我找到的唯一解决方案如下。首先,我在所有需要自定义标签的 logback 配置中添加了一条新规则,然后在此规则代码中添加其他规则。

  <newRule pattern="*/appender/asynclogger" actionClass="com.logentries.logback.joran.AsyncLoggerAction"/>


    <appender name="LE" class="com.logentries.logback.LogentriesAppender">
        <asynclogger>
            <logentries-httpput ssl="false" account="myAccount" key="myKey"/>
            <ignore-exceptions/>
        </asynclogger>
        <facility>USER</facility>
        <layout>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </appender>

我觉得我以次优的方式做这件事,有更好的解决方案吗?

4

1 回答 1

2

logentries-httpput 是否对应 LogentriesAppender 的某些字段?如果是,只需写:

  <appender name="LE" class="com.logentries.logback.LogentriesAppender">
        <logentries-httpput class="fully-qualified-class-name-for-logentries-httpput">
           <ssl>false</ssl>
           <account>myAccount</account>
           <key>myKey</key>
        </logentries-httpput>
        <ignore-exceptions/>
        <facility>USER</facility>
        <layout>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </appender>

Joran 将自己进行接线。您无需指定任何规则。

于 2013-08-18T20:26:49.423 回答