2016-07-21 61 views
0

這是Log4J2's site XML配置例如:與此Log4J2配置相當的JSON是什麼?

我已經嘗試了一些不同的JSON的變化,他們都失敗,NullPointException或類似的東西。

例如,下面的配置失敗:

 "PatternLayout": { 
     "MarkerPatternSelector": { 
      "defaultPattern": [%-5level] %c{1.} %msg%n", 
      "PatternMatch": { 
      "key": "FLOW", 
      "pattern": "[%-5level] %c{1.} ====== %C{1.}.%M:%L %msg ======%n" 
      } 
     } 
     } 

什麼是JSON相當於該XML配置是否正確?

回答

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文檔網站的URL(https://logging.apache.org/log4j/2.x)把我下印象,所有2.x版本是兼容的。