上一篇讲解了log4j输出xml格式的日志文件,本篇讲述log4j2.x版本。log4j2.x版本相比log4j1变化很大,使用起来也更麻烦,主要是jar包的依赖的问题。先上代码,然后再列举遇到的问题。
1、Log类代码
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
public class Log{
public static void main(String[] args) {
Logger logger = LogManager.getRootLogger();
ThreadContext.push("Message only");
ThreadContext.push("int", 1);
ThreadContext.push("int-long-string", "3");
ThreadContext.put("ipAddress", "192.168.21.9");
ThreadContext.put("key", "value");
logger.debug("hello world--debug");
logger.error("hello world--error");
ThreadContext.clearMap();
ThreadContext.clearAll();
}
}
2、配置文件log4j2.xml
xml version="1.0" encoding="UTF-8"?>
status="error" name="XMLConfig">
level="debug">
ref="MyFile"/>
name="CONSOLE">
complete="true" compact="false"/>
name="MyFile" fileName="logs/testlog.xml">
complete="true" compact="false" properties="true" locationInfo="true"/>
3、运行Log输出:
xmlns="http://logging.apache.org/log4j/2.0/events" timeMillis="1468682588978" thread="main" level="DEBUG" endOfBatch="false" loggerFqcn="org.apache.logging.log4j.spi.AbstractLogger" threadId="1" threadPriority="5">
hello world--debug
- key="ipAddress" value="192.168.21.9"/>
- key="key" value="value"/>
Message only
int
int-long-string
xmlns="http://logging.apache.org/log4j/2.0/events" timeMillis="1468682589064" thread="main" level="ERROR" endOfBatch="false" loggerFqcn="org.apache.logging.log4j.spi.AbstractLogger" threadId="1" threadPriority="5">
hello world--error
- key="ipAddress" value="192.168.21.9"/>
- key="key" value="value"/>
Message only
int
int-long-string
下面主要讲讲遇到的问题
问题1:log4j2的jar包很多,有几十个。如果只是在普通的java类中应用,需要使用两个:log4j-api-2.6.2.jar和log4j-core-2.6.2.jar
问题2:log4j2可以自动读取配置文件,自动查找log4j2.xml文件。Log类并没有指定配置文件。需要做的,就是把log4j2.xml文件放到src目录下。然后编译,就可以运行了。
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
23:35:25.986 [main] ERROR - hello world--error
问题3:“java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonGenerationException”异常,这个问题,定位了很久,由于不熟悉java,不太会看异常代码。现在看来,其实调用栈已经明确告诉你了,遇到JsonGenerationException。初步定位是有jar依赖,中途又去弄mvn,查找依赖,尝试了很久也没成功。采取比较笨的方法,就用google搜。各个页面仔细看,过程痛苦。
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonGenerationException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.createBuilder(PluginBuilder.java:147)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:119)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:479)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:219)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:231)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:496)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:566)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:582)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:217)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
at org.apache.logging.log4j.LogManager.getRootLogger(LogManager.java:652)
at testlog4j2.Log.main(Log.java:10)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonGenerationException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 22 more
最终找到的原因,log4j2有好几个库的依赖,在官网上可以找到。不同Layout,依赖也不同。XMLLayout依赖:
Jackson core, databind and dataformat XML
And com.fasterxml.woodstox:woodstox-core:5.0.2
jar包可以到http://mvnrepository.com/ 搜。下载三个jar包,jackson-annotations-2.8.0.jar,jackson-core-2.8.0.jar和jackson-dtatbind-2.8.0.jar。添加上,再运行。
问题4:添加了jackson库运行,结果出现如下错误。有颜色不一样,刚开始未注意,不断感叹多坎坷。后来发现,还少了一个库woodstox-core。加上问题依旧,想到woodstox-core还有依赖的库。于是又找到了jackson-dataformat-xml.2.8.0.jar,jackson-module-jaxb-annotations-2.8.0.jar和stax2-api-4.0.0.jar,才总算解决了运行的错误。
2016-07-16 23:46:34,996 main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.layout.XmlLayout for element XMLLayout. java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:132)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:479)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:219)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:231)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:496)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:566)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:582)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:217)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
at org.apache.logging.log4j.LogManager.getRootLogger(LogManager.java:652)
at testlog4j2.Log.main(Log.java:10)
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/dataformat/xml/XmlMapper
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.logging.log4j.core.layout.XmlLayout.<init>(XmlLayout.java:198)
at org.apache.logging.log4j.core.layout.XmlLayout.createLayout(XmlLayout.java:287)
... 22 more
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.dataformat.xml.XmlMapper
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 36 more