问题描述
我查找了所有消息,但没有找到这个问题的明确答案.
如何将记录配置为记录CXF入站和出站静止消息?
我有以下设置.
-
org.apache.cxf.common.logging.Log4jLogger
-
applicationContext.xml具有以下内容(听起来很愚蠢,但这是我可以收到消息输出的拦截器的唯一位置)
)<bean id="abstractLoggingInterceptor" abstract="true"> <property name="prettyLogging" value="true"/> </bean> <bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" parent="abstractLoggingInterceptor"/> <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" parent="abstractLoggingInterceptor"/> <cxf:bus> <cxf:inInterceptors> <ref bean="loggingInInterceptor"/> </cxf:inInterceptors> <cxf:outInterceptors> <ref bean="loggingOutInterceptor"/> </cxf:outInterceptors> <cxf:outFaultInterceptors> <ref bean="loggingOutInterceptor"/> </cxf:outFaultInterceptors> <cxf:inFaultInterceptors> <ref bean="loggingInInterceptor"/> </cxf:inFaultInterceptors> </cxf:bus>
我尝试使用SLF4J和Log4J遵循以下说明,但是我对文件的唯一输出是应用程序日志消息.我可以在控制台上看到入站和出站消息.
我可以得到类似于logback.xml的东西,因此我将应用程序日志和消息日志分开.示例: http://www.wolfe.id.id.id.id.iu/2011/05/20/apache-cxf-logging/
谢谢.
编辑1: 我从我的类路径中删除了org.apache.cxf.common.logging.log4jlogger,并将以下内容放在我的log4j.xml中.当记录级别等于信息时,它将登录到文件并进行控制.
<appender name="RSLOGFILE" class="org.apache.log4j.RollingFileAppender"> <param name="file" value="${project.basedir}/logs/cxf_inout_messages.log"/> <param name="MaxFileSize" value="100KB"/> <!-- Keep one backup file --> <param name="MaxBackupIndex" value="1"/> <layout class="org.apache.log4j.PatternLayout"> <!-- Print the date in ISO 8601 format --> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/> </layout> </appender> <logger name="org.apache.cxf"> <level value="ERROR"/> <appender-ref ref="RSLOGFILE"/> </logger>
推荐答案
假设您使用的是CXF 2.2.8或更高,则需要执行以下操作:
步骤1)在包含以下几个的类路径上创建文件META-INF/cxf/org.apache.cxf.Logger:
org.apache.cxf.common.logging.Slf4jLogger
如果您使用的是Maven,则此新文件必须在src/main/resources/META-INF/cxf中,而不是src/main/webapp!
步骤2)如果要记录所有消息,请创建一个CXF LoggingFeature,将prettyLogging属性设置为true并将其添加到CXF BUS.
步骤3)为log4j和slf4j-log4j12添加所需的JAR文件.如果您使用的是Maven,请包括以下依赖项:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency>
步骤4)在您中log4j.xml将org.apache.cxf.services的日志级别设置为信息,将additivity设置为FALSE,并附加一个专用的appender:
<!-- level INFO needed to log SOAP messages --> <logger name="org.apache.cxf.services" additivity="false"> <level value="INFO" /> <!-- specify a dedicated appender for the SOAP messages --> <appender-ref ref="WS_LOG_FILE" /> </logger>
我已经创建了博客文章说明如何更详细地为log4j配置cxf
问题描述
I looked up all messages but did not find a clear answer for that question.
How can I configure logging to log CXF inbound and outbound restful messages ?
I have the following setup.
File org.apache.cxf.Logger with
org.apache.cxf.common.logging.Log4jLogger
applicationContext.xml has the following (it sounds silly, but it is the only place for interceptors I could get messages output)
<bean id="abstractLoggingInterceptor" abstract="true"> <property name="prettyLogging" value="true"/> </bean> <bean id="loggingInInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor" parent="abstractLoggingInterceptor"/> <bean id="loggingOutInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor" parent="abstractLoggingInterceptor"/> <cxf:bus> <cxf:inInterceptors> <ref bean="loggingInInterceptor"/> </cxf:inInterceptors> <cxf:outInterceptors> <ref bean="loggingOutInterceptor"/> </cxf:outInterceptors> <cxf:outFaultInterceptors> <ref bean="loggingOutInterceptor"/> </cxf:outFaultInterceptors> <cxf:inFaultInterceptors> <ref bean="loggingInInterceptor"/> </cxf:inFaultInterceptors> </cxf:bus>
I tried to follow these instructions with slf4j and with log4j, but the the only output I get to the file is application log messages. I can see inbound and outbound messages on my console.
Can I get something similar to logback.xml work for me, so I separate app logs and message logs. Example: http://www.wolfe.id.au/2011/05/20/apache-cxf-logging/
Thanks.
EDIT 1: I removed org.apache.cxf.common.logging.Log4jLogger from my classpath, and placed the following to my log4j.xml. It logging to the file and to console when the level of logging is equal to INFO.
<appender name="RSLOGFILE" class="org.apache.log4j.RollingFileAppender"> <param name="file" value="${project.basedir}/logs/cxf_inout_messages.log"/> <param name="MaxFileSize" value="100KB"/> <!-- Keep one backup file --> <param name="MaxBackupIndex" value="1"/> <layout class="org.apache.log4j.PatternLayout"> <!-- Print the date in ISO 8601 format --> <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/> </layout> </appender> <logger name="org.apache.cxf"> <level value="ERROR"/> <appender-ref ref="RSLOGFILE"/> </logger>
推荐答案
Assuming you are using CXF 2.2.8 or higher, you would need to do the following:
step 1) Create a file META-INF/cxf/org.apache.cxf.Logger on the classpath containing the following:
org.apache.cxf.common.logging.Slf4jLogger
If you're using Maven, this new file must be in src/main/resources/META-INF/cxf, not below src/main/webapp!
step 2) If you want to log all messages, create a CXF LoggingFeature, set the prettyLogging property to true and add it to the CXF bus.
step 3) Add the needed jar files for log4j and slf4j-log4j12. If you are using Maven, include following dependencies:
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency>
step 4) In you log4j.xml set the log level of org.apache.cxf.services to INFO, set additivity to FALSE and attach a dedicated appender:
<!-- level INFO needed to log SOAP messages --> <logger name="org.apache.cxf.services" additivity="false"> <level value="INFO" /> <!-- specify a dedicated appender for the SOAP messages --> <appender-ref ref="WS_LOG_FILE" /> </logger>
I've created a blog post which explains how to configure CXF for log4j in more detail.