java学习:log4j输出xml格式的日志文件(log4j2篇)

上一篇讲解了log4j输出xml格式的日志文件,本篇讲述log4j2.x版本。log4j2.x版本相比log4j1变化很大,使用起来也更麻烦,主要是jar包的依赖的问题。先上代码,然后再列举遇到的问题。

1、Log类代码

 
  
  1. import org.apache.logging.log4j.LogManager;  
  2. import org.apache.logging.log4j.Logger;
  3. import org.apache.logging.log4j.ThreadContext;  
  4. public class Log{
  5. public static void main(String[] args) {
  6. Logger logger = LogManager.getRootLogger();
  7. ThreadContext.push("Message only");
  8.    ThreadContext.push("int", 1);
  9.    ThreadContext.push("int-long-string", "3");
  10. ThreadContext.put("ipAddress", "192.168.21.9");
  11.    ThreadContext.put("key", "value");
  12.    
  13. logger.debug("hello world--debug");
  14. logger.error("hello world--error");
  15. ThreadContext.clearMap();
  16. ThreadContext.clearAll();
  17.    }  
  18. }

2、配置文件log4j2.xml

 
  
  1. xml version="1.0" encoding="UTF-8"?>
  2. status="error" name="XMLConfig">
  3.    
  4.         level="debug">
  5.             ref="MyFile"/>
  6.        
  7.    
  8.    
  9.         name="CONSOLE">
  10.             complete="true" compact="false"/>
  11.        
  12.         name="MyFile" fileName="logs/testlog.xml">
  13.             complete="true" compact="false" properties="true" locationInfo="true"/>
  14.        
  15.    

3、运行Log输出:

 
  
  1. 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">
  2.    hello world--debug
  3.    
  4.       key="ipAddress" value="192.168.21.9"/>
  5.       key="key" value="value"/>
  6.    
  7.    
  8.      Message only
  9.      int
  10.      int-long-string
  11.    
  12.     class="com.flyerfish.common.Log" method="main" file="Log.java" line="18"/>
  13.  
  14.   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">
  15.    hello world--error
  16.    
  17.       key="ipAddress" value="192.168.21.9"/>
  18.       key="key" value="value"/>
  19.    
  20.    
  21.      Message only
  22.      int
  23.      int-long-string
  24.    
  25.     class="com.flyerfish.common.Log" method="main" file="Log.java" line="19"/>
  26.  


下面主要讲讲遇到的问题

问题1:log4j2的jar包很多,有几十个。如果只是在普通的java类中应用,需要使用两个:log4j-api-2.6.2.jar和log4j-core-2.6.2.jar

问题2:log4j2可以自动读取配置文件,自动查找log4j2.xml文件。Log类并没有指定配置文件。需要做的,就是把log4j2.xml文件放到src目录下。然后编译,就可以运行了。

 
  
  1. ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
  2. 23:35:25.986 [main] ERROR  - hello world--error

问题3:“java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonGenerationException”异常,这个问题,定位了很久,由于不熟悉java,不太会看异常代码。现在看来,其实调用栈已经明确告诉你了,遇到JsonGenerationException。初步定位是有jar依赖,中途又去弄mvn,查找依赖,尝试了很久也没成功。采取比较笨的方法,就用google搜。各个页面仔细看,过程痛苦。

 
  
  1. Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonGenerationException
  2. at java.lang.Class.getDeclaredMethods0(Native Method)
  3. at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
  4. at java.lang.Class.getDeclaredMethods(Class.java:1975)
  5. at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.createBuilder(PluginBuilder.java:147)
  6. at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:119)
  7. at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
  8. at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
  9. at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
  10. at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
  11. at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:479)
  12. at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:219)
  13. at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:231)
  14. at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:496)
  15. at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:566)
  16. at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:582)
  17. at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:217)
  18. at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
  19. at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
  20. at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
  21. at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
  22. at org.apache.logging.log4j.LogManager.getRootLogger(LogManager.java:652)
  23. at testlog4j2.Log.main(Log.java:10)
  24. Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonGenerationException
  25. at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  26. at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  27. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
  28. at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  29. ... 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,才总算解决了运行的错误。

 
   
  1. 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
  2. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  3. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  4. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  5. at java.lang.reflect.Method.invoke(Method.java:498)
  6. at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:132)
  7. at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
  8. at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
  9. at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
  10. at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
  11. at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:479)
  12. at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:219)
  13. at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:231)
  14. at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:496)
  15. at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:566)
  16. at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:582)
  17. at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:217)
  18. at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
  19. at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
  20. at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
  21. at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
  22. at org.apache.logging.log4j.LogManager.getRootLogger(LogManager.java:652)
  23. at testlog4j2.Log.main(Log.java:10)
  24. Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/dataformat/xml/XmlMapper
  25. at java.lang.ClassLoader.defineClass1(Native Method)
  26. at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
  27. at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
  28. at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
  29. at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
  30. at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
  31. at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
  32. at java.security.AccessController.doPrivileged(Native Method)
  33. at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
  34. at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  35. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
  36. at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  37. at org.apache.logging.log4j.core.layout.XmlLayout.<init>(XmlLayout.java:198)
  38. at org.apache.logging.log4j.core.layout.XmlLayout.createLayout(XmlLayout.java:287)
  39. ... 22 more
  40. Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.dataformat.xml.XmlMapper
  41. at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  42. at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  43. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
  44. at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  45. ... 36 more
结论:java异常打印,基本已经告诉你了哪里错误,比如上面的红色部分,是 com.fasterxml.jackson.dataformat.xml.XmlMapper类找不到定义,不要被这么长的出错信息吓到,而不仔细去看。学会看了,处理起来就容易了。


你可能感兴趣的:(java)