Log4j2 无法使用 json 配置文件
Log4j2 not working with json config file
似乎 log4j 正在尝试通过 XML 解析器解析 .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 文件夹中的 Jars:
- hamcrest-junit-2.0.0.0.jar
- jackson-annotations-2.6.1.jar
- jackson-core-2.6.1.jar
- jackson-databind-2.6.1.jar
- java-hamcrest-2.0.0.0.jar
- junit-4.12.jar
- kotlin-reflect.jar
- kotlin-runtime-sources.jar
- kotlin-runtime.jar
- log4j-api-2.3.jar
- log4j-core-2.3.jar
- log4j-slf4j-impl-2.3.jar
可以使用系统属性 log4j.configurationFactory
:
-Dlog4j.configurationFactory=org.apache.logging.log4j.core.config.json.JsonConfigurationFactory
即使已经有一个可接受的答案,我也会 post 我的解决方案。也许这对将来的其他人有帮助...
它对我不起作用的原因是我没有可用的 jackson-core
和 jackson-databind
库。在我添加
之后
compile 'com.fasterxml.jackson.core:jackson-core:2.8.7'
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.2'
我的 build.gradle
文件一切正常。
似乎 log4j 正在尝试通过 XML 解析器解析 .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 文件夹中的 Jars:
- hamcrest-junit-2.0.0.0.jar
- jackson-annotations-2.6.1.jar
- jackson-core-2.6.1.jar
- jackson-databind-2.6.1.jar
- java-hamcrest-2.0.0.0.jar
- junit-4.12.jar
- kotlin-reflect.jar
- kotlin-runtime-sources.jar
- kotlin-runtime.jar
- log4j-api-2.3.jar
- log4j-core-2.3.jar
- log4j-slf4j-impl-2.3.jar
可以使用系统属性 log4j.configurationFactory
:
-Dlog4j.configurationFactory=org.apache.logging.log4j.core.config.json.JsonConfigurationFactory
即使已经有一个可接受的答案,我也会 post 我的解决方案。也许这对将来的其他人有帮助...
它对我不起作用的原因是我没有可用的 jackson-core
和 jackson-databind
库。在我添加
compile 'com.fasterxml.jackson.core:jackson-core:2.8.7'
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.2'
我的 build.gradle
文件一切正常。