这是来自Log4J2's site的XML配置示例:
我尝试了一些不同的JSON版本,所有这些版本都因NullPointException
或类似内容而失败。
例如,以下配置失败:
"PatternLayout": {
"MarkerPatternSelector": {
"defaultPattern": [%-5level] %c{1.} %msg%n",
"PatternMatch": {
"key": "FLOW",
"pattern": "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n"
}
}
}
此XML配置的正确JSON等效项是什么?
答案 0 :(得分:1)
问题是由于我正在使用的Log4J2版本(2.3
)。 2.6.2
支持此功能。更确切地说,这是2.6.2中的工厂方法:
@PluginFactory
public static PatternLayout createLayout(
@PluginAttribute(value = "pattern", defaultString = DEFAULT_CONVERSION_PATTERN) final String pattern,
@PluginElement("PatternSelector") final PatternSelector patternSelector,
@PluginConfiguration final Configuration config,
@PluginElement("Replace") final RegexReplacement replace,
// LOG4J2-783 use platform default by default, so do not specify defaultString for charset
@PluginAttribute(value = "charset") final Charset charset,
@PluginAttribute(value = "alwaysWriteExceptions", defaultBoolean = true) final boolean alwaysWriteExceptions,
@PluginAttribute(value = "noConsoleNoAnsi", defaultBoolean = false) final boolean noConsoleNoAnsi,
@PluginAttribute("header") final String headerPattern,
@PluginAttribute("footer") final String footerPattern) {
return newBuilder()
.withPattern(pattern)
.withPatternSelector(patternSelector)
.withConfiguration(config)
.withRegexReplacement(replace)
.withCharset(charset)
.withAlwaysWriteExceptions(alwaysWriteExceptions)
.withNoConsoleNoAnsi(noConsoleNoAnsi)
.withHeader(headerPattern)
.withFooter(footerPattern)
.build();
}
以下是它在2.3中的样子:
@PluginFactory
public static PatternLayout createLayout(
@PluginAttribute(value = "pattern", defaultString = DEFAULT_CONVERSION_PATTERN) final String pattern,
@PluginConfiguration final Configuration config,
@PluginElement("Replace") final RegexReplacement replace,
@PluginAttribute(value = "charset", defaultString = "UTF-8") final Charset charset,
@PluginAttribute(value = "alwaysWriteExceptions", defaultBoolean = true) final boolean alwaysWriteExceptions,
@PluginAttribute(value = "noConsoleNoAnsi", defaultBoolean = false) final boolean noConsoleNoAnsi,
@PluginAttribute("header") final String header,
@PluginAttribute("footer") final String footer) {
return newBuilder()
.withPattern(pattern)
.withConfiguration(config)
.withRegexReplacement(replace)
.withCharset(charset)
.withAlwaysWriteExceptions(alwaysWriteExceptions)
.withNoConsoleNoAnsi(noConsoleNoAnsi)
.withHeader(header)
.withFooter(footer)
.build();
}
API文档网站的网址(https://logging.apache.org/log4j/2.x
)让我觉得所有2.x
版本都兼容。