我想在文件而不是控制台中记录Apache CXF入站和出站拦截器日志
Maven项目:
1)创建了一个名为' org.apache.cxf.Logger'的文件。在META-INF \ cxf下并放置以下文字
META-INF \ CXF \ org.apache.cxf.Logger
org.apache.cxf.common.logging.Log4jLogger
2)application-context.xml:
<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>
<jaxrs:server id="services" address="/mysvc">
<jaxrs:serviceBeans>
<ref bean="myserviceImpl" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider" />
</jaxrs:providers>
<jaxrs:outInterceptors>
<ref bean="loggingOutInterceptor" />
</jaxrs:outInterceptors>
<jaxrs:inInterceptors>
<ref bean="loggingInInterceptor" />
</jaxrs:inInterceptors>
</jaxrs:server>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" id="loggingInInterceptor" />
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" id="loggingOutInterceptor" />
3)log4j2.xml:在src \ main \ resources文件夹下
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="CompanyServices" packages="">
<Appenders>
<RollingFile name="RollingFile" fileName="D:/temp/companyfile.log"
filePattern="companyfile-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d %p [%t] %C:%L - %m%n</Pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10 MB" />
</Policies>
</RollingFile>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout>
<Pattern>%d %p [%t] %C:%L - %m%n</Pattern>
</PatternLayout>
</Console>
</Appenders>
<Loggers>
<Logger name="com.myorg.mycompany" level="trace"
additivity="false">
<AppenderRef ref="RollingFile" />
</Logger>
<Logger name="org.apache.cxf.interceptor" level="info"
additivity="false">
<AppenderRef ref="RollingFile" />
</Logger>
<Root level="all">
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>
</Configuration>
4。 pom.xml:
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-service-description</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
生成了一个文件,只将我的应用程序级日志打印到我的文件中。但是不能打印CXF拦截器登录到文件。
请说明出了什么问题?
答案 0 :(得分:2)
步骤1)重定向到Slf4jLogger
,因为它看起来像重定向到Log4jLogger
does not seem to work with Log4j2。此外,如果您决定更改日志记录框架,SLF4J之后可以提供更大的灵活性。
第2步)我认为上面的配置可以简化一下。您正在将记录拦截器添加到CXF总线和JAX-RS端点。如果您只想为端点记录消息,请在那里添加消息。如果没有,请将它们添加到总线中,因为您无需为可能的其他端点重复配置。
同样在端点上,您没有将日志记录拦截器添加到故障拦截器链。如果您基本上想要记录所有消息,则可能更容易使用CXF LoggingFeature
。我写了更多details on the difference between interceptors and features, check this blog post。
步骤4)您需要以下依赖项:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
我创建了blog post which explains how to configure CXF for log4j2 in more detail。