添加标签到logback配置

时间:2013-08-14 11:56:32

标签: xml logback

我正在尝试编写具有特殊配置的自定义appender,我需要为其xml配置创建相应的标记。从logback手册中可以清楚地知道如何做到这一点:通过向配置器中添加新规则,如下所述:http://logback.qos.ch/manual/onJoran.html

我的问题是如何在不直接创建配置程序时添加此规则,而是在slf4j绑定创建配置器时添加此规则。当您第一次访问slf4j-api LoggerFactory时,slf4j-api将在类路径中查找可用的绑定。如果您正确拥有logback-classic,则会找到绑定,它就是类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.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>

我觉得我是以次优的方式做到这一点,有更好的解决方案吗?

1 个答案:

答案 0 :(得分: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将独自完成布线。您无需指定任何规则。