下面列出了org.apache.log4j.helpers.OptionConverter#toBoolean ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
Read configuration options from <code>properties</code>.
See {@link #doConfigure(String, LoggerRepository)} for the expected format.
*/
public
void doConfigure(Properties properties, LoggerRepository hierarchy) {
String value = properties.getProperty(LogLog.DEBUG_KEY);
if(value == null) {
value = properties.getProperty("log4j.configDebug");
if(value != null)
LogLog.warn("[log4j.configDebug] is deprecated. Use [log4j.debug] instead.");
}
if(value != null) {
LogLog.setInternalDebugging(OptionConverter.toBoolean(value, true));
}
//
// if log4j.reset=true then
// reset hierarchy
String reset = properties.getProperty(RESET_KEY);
if (reset != null && OptionConverter.toBoolean(reset, false)) {
hierarchy.resetConfiguration();
}
String thresholdStr = OptionConverter.findAndSubst(THRESHOLD_PREFIX,
properties);
if(thresholdStr != null) {
hierarchy.setThreshold(OptionConverter.toLevel(thresholdStr,
(Level) Level.ALL));
LogLog.debug("Hierarchy threshold set to ["+hierarchy.getThreshold()+"].");
}
configureRootCategory(properties, hierarchy);
configureLoggerFactory(properties);
parseCatsAndRenderers(properties, hierarchy);
LogLog.debug("Finished configuring.");
// We don't want to hold references to appenders preventing their
// garbage collection.
registry.clear();
}
/**
Parse the additivity option for a non-root category.
*/
void parseAdditivityForLogger(Properties props, Logger cat,
String loggerName) {
String value = OptionConverter.findAndSubst(ADDITIVITY_PREFIX + loggerName,
props);
LogLog.debug("Handling "+ADDITIVITY_PREFIX + loggerName+"=["+value+"]");
// touch additivity only if necessary
if((value != null) && (!value.equals(""))) {
boolean additivity = OptionConverter.toBoolean(value, true);
LogLog.debug("Setting additivity for \""+loggerName+"\" to "+
additivity);
cat.setAdditivity(additivity);
}
}
/**
@deprecated Use the setter method for the option directly instead
of the generic <code>setOption</code> method.
*/
public
void setOption(String key, String value) {
if(key.equalsIgnoreCase(STRING_TO_MATCH_OPTION)) {
stringToMatch = value;
} else if (key.equalsIgnoreCase(ACCEPT_ON_MATCH_OPTION)) {
acceptOnMatch = OptionConverter.toBoolean(value, acceptOnMatch);
}
}
/**
* Parse the additivity option for a non-root category.
*/
private boolean getAdditivityForLogger(Properties props, String loggerName) {
boolean additivity = true;
String key = ADDITIVITY_PREFIX + loggerName;
String value = OptionConverter.findAndSubst(key, props);
LOGGER.debug("Handling {}=[{}]", key, value);
// touch additivity only if necessary
if ((value != null) && (!value.equals(""))) {
additivity = OptionConverter.toBoolean(value, true);
}
return additivity;
}
/**
* Used internally to configure the log4j framework by parsing a DOM
* tree of XML elements based on <a
* href="doc-files/log4j.dtd">log4j.dtd</a>.
*/
private void parse(Element element) {
String rootElementName = element.getTagName();
if (!rootElementName.equals(CONFIGURATION_TAG)) {
if (rootElementName.equals(OLD_CONFIGURATION_TAG)) {
LOGGER.warn("The <" + OLD_CONFIGURATION_TAG +
"> element has been deprecated.");
LOGGER.warn("Use the <" + CONFIGURATION_TAG + "> element instead.");
} else {
LOGGER.error("DOM element is - not a <" + CONFIGURATION_TAG + "> element.");
return;
}
}
String debugAttrib = subst(element.getAttribute(INTERNAL_DEBUG_ATTR));
LOGGER.debug("debug attribute= \"" + debugAttrib + "\".");
// if the log4j.dtd is not specified in the XML file, then the
// "debug" attribute is returned as the empty string.
String status = "error";
if (!debugAttrib.equals("") && !debugAttrib.equals("null")) {
status = OptionConverter.toBoolean(debugAttrib, true) ? "debug" : "error";
} else {
LOGGER.debug("Ignoring " + INTERNAL_DEBUG_ATTR + " attribute.");
}
String confDebug = subst(element.getAttribute(CONFIG_DEBUG_ATTR));
if (!confDebug.equals("") && !confDebug.equals("null")) {
LOGGER.warn("The \"" + CONFIG_DEBUG_ATTR + "\" attribute is deprecated.");
LOGGER.warn("Use the \"" + INTERNAL_DEBUG_ATTR + "\" attribute instead.");
status = OptionConverter.toBoolean(confDebug, true) ? "debug" : "error";
}
final StatusConfiguration statusConfig = new StatusConfiguration().setStatus(status);
statusConfig.initialize();
forEachElement(element.getChildNodes(), (currentElement) -> {
switch (currentElement.getTagName()) {
case CATEGORY: case LOGGER_ELEMENT:
parseCategory(currentElement);
break;
case ROOT_TAG:
parseRoot(currentElement);
break;
case RENDERER_TAG:
LOGGER.warn("Renderers are not supported by Log4j 2 and will be ignored.");
break;
case THROWABLE_RENDERER_TAG:
LOGGER.warn("Throwable Renderers are not supported by Log4j 2 and will be ignored.");
break;
case CATEGORY_FACTORY_TAG: case LOGGER_FACTORY_TAG:
LOGGER.warn("Log4j 1 Logger factories are not supported by Log4j 2 and will be ignored.");
break;
case APPENDER_TAG:
Appender appender = parseAppender(currentElement);
appenderMap.put(appender.getName(), appender);
if (appender instanceof AppenderWrapper) {
addAppender(((AppenderWrapper) appender).getAppender());
} else {
addAppender(new AppenderAdapter(appender).getAdapter());
}
break;
default:
quietParseUnrecognizedElement(null, currentElement, props);
}
});
}
/**
* Used internally to configure the log4j framework by parsing a DOM
* tree of XML elements based on <a
* href="doc-files/log4j.dtd">log4j.dtd</a>.
*/
private void parse(Element element) {
String rootElementName = element.getTagName();
if (!rootElementName.equals(CONFIGURATION_TAG)) {
if (rootElementName.equals(OLD_CONFIGURATION_TAG)) {
LOGGER.warn("The <" + OLD_CONFIGURATION_TAG +
"> element has been deprecated.");
LOGGER.warn("Use the <" + CONFIGURATION_TAG + "> element instead.");
} else {
LOGGER.error("DOM element is - not a <" + CONFIGURATION_TAG + "> element.");
return;
}
}
String debugAttrib = subst(element.getAttribute(INTERNAL_DEBUG_ATTR));
LOGGER.debug("debug attribute= \"" + debugAttrib + "\".");
// if the log4j.dtd is not specified in the XML file, then the
// "debug" attribute is returned as the empty string.
String status = "error";
if (!debugAttrib.equals("") && !debugAttrib.equals("null")) {
status = OptionConverter.toBoolean(debugAttrib, true) ? "debug" : "error";
} else {
LOGGER.debug("Ignoring " + INTERNAL_DEBUG_ATTR + " attribute.");
}
String confDebug = subst(element.getAttribute(CONFIG_DEBUG_ATTR));
if (!confDebug.equals("") && !confDebug.equals("null")) {
LOGGER.warn("The \"" + CONFIG_DEBUG_ATTR + "\" attribute is deprecated.");
LOGGER.warn("Use the \"" + INTERNAL_DEBUG_ATTR + "\" attribute instead.");
status = OptionConverter.toBoolean(confDebug, true) ? "debug" : "error";
}
final StatusConfiguration statusConfig = new StatusConfiguration().withStatus(status);
statusConfig.initialize();
forEachElement(element.getChildNodes(), (currentElement) -> {
switch (currentElement.getTagName()) {
case CATEGORY: case LOGGER_ELEMENT:
parseCategory(currentElement);
break;
case ROOT_TAG:
parseRoot(currentElement);
break;
case RENDERER_TAG:
LOGGER.warn("Renderers are not supported by Log4j 2 and will be ignored.");
break;
case THROWABLE_RENDERER_TAG:
LOGGER.warn("Throwable Renderers are not supported by Log4j 2 and will be ignored.");
break;
case CATEGORY_FACTORY_TAG: case LOGGER_FACTORY_TAG:
LOGGER.warn("Log4j 1 Logger factories are not supported by Log4j 2 and will be ignored.");
break;
case APPENDER_TAG:
Appender appender = parseAppender(currentElement);
appenderBag.put(appender.getName(), appender);
if (appender instanceof AppenderWrapper) {
configuration.addAppender(((AppenderWrapper) appender).getAppender());
} else {
configuration.addAppender(new AppenderAdapter(appender).getAdapter());
}
break;
default:
quietParseUnrecognizedElement(null, currentElement, props);
}
});
}