Use Variable Substitution for Logback Appender Class Attribute

自作多情 提交于 2021-01-28 09:13:12

问题


Is it possible to use variable substitution when specifying the implementing class for a Logback appender? e.g.,

<appender name="LOGFILE" class="${LOGFILE_APPENDER_CLASS}">

When specified as above, Logback appears to attempt to load a class with a name of "${LOGFILE_APPENDER_CLASS}". i.e., no variable substitution is performed!

21:17:11,331 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [${LOGFILE_APPENDER_CLASS}] 21:17:11,333 |-ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type [${LOGFILE_APPENDER_CLASS}]. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ${LOGFILE_APPENDER_CLASS} at ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ${LOGFILE_APPENDER_CLASS}

I want to send output to a file in production and console for eclipse developers. I don't want to duplicate the appender configuration if possible as it contains a long list of filters that will be the same in most cases.


回答1:


As per the question, all evidence indicates that LogBack will not perform variable substitution in this scenario.

Adopted solution was to switch from XML configuration file to Groovy configuration file. Then dynamically control the list of appenders based on a "bDeveloper" variable derived from the environment.

def bDeveloper = ["","true"].contains("${System.getProperty("developer")}".toLowerCase())
if (bDeveloper) {
    scan("15 seconds")
    lstRootAppenders.add("BEAGLE")
    oLogfileAppender = ch.qos.logback.core.ConsoleAppender
}


来源:https://stackoverflow.com/questions/13857785/use-variable-substitution-for-logback-appender-class-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!