问题
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