2015-08-28 192 views
0

看來log4j試圖通過XML解析器解析.json配置文件,但爲什麼?Log4j2不能使用json配置文件

我通過命令行參數設置log4j的配置文件:

-Dlog4j.configurationFile=src/log4j2.json 

文件內容:

{ 
    "configuration": { 
     "appenders": { 
      "Console": { 
       "name": "Console", 
       "target": "SYSTEM_OUT", 
       "PatternLayout": { 
        "pattern": "%date %thread %logger %message" 
       } 
      } 
     }, 
     "loggers": { 
      "root": { 
       "level": "debug", 
       "appender-ref": { 
        "ref": "Console" 
       } 
      } 
     } 
    } 
} 

我能得到什麼:

[Fatal Error] log4j2.json:1:1: Content is not allowed in prolog. 
ERROR StatusLogger Error parsing D:\Dev\HDelphiCrawler\src\log4j2.json org.xml.sax.SAXParseException; systemId: file:///D:/Dev/HDelphiCrawler/src/log4j2.json; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. 
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257) 
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348) 
    at org.apache.logging.log4j.core.config.xml.XmlConfiguration.<init>(XmlConfiguration.java:140) 
    at org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory.getConfiguration(XmlConfigurationFactory.java:44) 
    at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:410) 
    ....and so on... 
ERROR StatusLogger No logging configuration 

罐子在我的lib文件夾:

  • hamcrest-的junit-2.0.0.0.jar
  • 傑克遜 - 註解-2.6.1.jar
  • 傑克遜 - 芯 - 2.6.1.jar
  • 傑克遜 - 數據綁定-2.6.1.jar
  • java的hamcrest-2.0.0.0.jar
  • 的junit-4.12.jar
  • 科特林-reflect.jar
  • 科特林-運行時sources.jar
  • 科特林-runtime.jar
  • log4j的-API-2.3.jar
  • log4j的核心 - 2.3.jar
  • 的log4j-SLF4J-IMPL-2.3.jar
+0

我會建議添加-Dlog4j2.StatusLogger.level = DEBUG。它應該打印一些可能有助於識別問題的調試消息。 – rgoers

回答

1

您可以使用系統屬性log4j.configurationFactory

-Dlog4j.configurationFactory=org.apache.logging.log4j.core.config.json.JsonConfigurationFactory