CXF客户端普通java工程使用log4j

AndyXuq 2012-12-27

做cli命令行去调用webservice,该CXF客户端是一个普通java工程,需要使用log4j打日志。。

CXF默认使用java.util.logging作为默认的日志实现。

参考CXF官方文档:

http://cxf.apache.org/docs/debugging-and-logging.html

Using Log4j Instead of java.util.logging

As noted above, CXF uses the java.util.logging package by default. But it is possible to switch CXF to instead use Log4J. This is achieved through the use of configuration files. There are two options to bootstrapping CXF logging and each is listed below:

  • Add the following system property to the classpath from which CXF is initialized:
-Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Log4jLogger
  • Add the file META-INF/cxf/org.apache.cxf.Logger to the classpath and make sure it contains the following content:
org.apache.cxf.common.logging.Log4jLogger

Using SLF4J Instead of java.util.logging (since 2.2.8)

As noted above, CXF uses the java.util.logging package by default. But it is possible to switch CXF to instead use SLF4J. This is achieved through the use of configuration files. There are two options to bootstrapping CXF logging and each is listed below:

  • Add the following system property to the classpath from which CXF is initialized:
-Dorg.apache.cxf.Logger=org.apache.cxf.common.logging.Slf4jLogger
  • Add the file META-INF/cxf/org.apache.cxf.Logger to the classpath and make sure it contains the following content:
org.apache.cxf.common.logging.Slf4jLogger

简单描述一下就是:有两种方式实现在cxf中使用日志功能(本文采用的第二种方式)

1、在classpath中新建文件  META-INF/cxf/org.apache.cxf.Logger(此处本人走了不少弯路,刚开始 META-INF/cxf/org.apache.cxf.Logger和log4j.propertis放在src目录下,源码编译的class文件放在bin中。后来发现编译后目录中居然没有org.apache.cxf.Logger文件,干脆直接把bin文件放入classpath中,直接把log4j.properties和META-INF/cxf/org.apache.cxf.Logger建在这个位置)

2、在新建的文件中加上一行

org.apache.cxf.common.logging.Slf4jLogger

3、添加log4j需要的包: log4j.jar, common-logging.jar

4、直接把log4j.properties放在bin目录下

归根结底还是一个classpath的问题,但是刚开始的时候我把log4j.properties和 META-INF/cxf/org.apache.cxf.Logger放在src目录下应该是在classpath中的呀,不明白,请高手指教一下。。

相关推荐