1

在我的应用程序中,有几个借助 cxf 框架实现的 Web 服务。我想将每个 Web 服务的传入请求记录在单独的文件中。例如

requests -> ws1 interface -> incomingWs1.log
requests -> ws2 interface -> incomingWs2.log

目前我知道如何使用日志拦截器记录传入和传出消息。这很好用,但是使用拦截器我无法定义日志的输出位置。这是使用日志拦截器的示例:

@WebService(endpointInterface = "at.pdts.cxf.HelloWorld")
@InInterceptors(interceptors = "org.apache.cxf.interceptor.LoggingInInterceptor") // this works!
@Logging(limit=16000, inLocation="<stdout>", outLocation="<logger>", pretty=true) // does not work!
public class HelloWorldImpl implements HelloWorld { ...

在研究了一段时间后,发现提供了我搜索的位置设置的日志注释,如您在上面的代码示例中所见。国庆没有效果!我尝试了几个位置选项,比如

file:///c:/log.log
file:c:\log.log
<stdout>

我希望有人能指出我正确的方向。我在版本 2.2.1 的 grails 应用程序中使用 cxf 框架 2.7.3。

4

2 回答 2

1

可以使用 annotation的outLocation属性来实现这一点,但是,提供的文件名必须是绝对文件名。@Logging下面提供了一个示例:

@Logging(limit=16000, inLocation="<stdout>", outLocation="/absolute/path/to/file", pretty=true)
public class HelloWorldImpl implements HelloWorld { ...

请注意,只有绝对路径才能工作,因为 aURI用于在类内部创建输出文件AbstractLoggingInterceptor,如下所示:

public abstract class AbstractLoggingInterceptor extends AbstractPhaseInterceptor<Message> {
    public void setOutputLocation(String s) {
        ...
        } else {
            try {
                URI uri = new URI(s);
                File file = new File(uri);
                writer = new PrintWriter(new FileWriter(file, true), true);
            } catch (Exception ex) {
                getLogger().log(Level.WARNING, "Error configuring log location " + s, ex);
            }
        }
    }
}

如果您想使用名称将在运行时确定的动态文件,您可以通过扩展LoggingOutInterceptor来实现这一点。

于 2017-06-23T10:20:22.480 回答
0

解决方案非常简单。如果要使用日志注释,则必须将其放置在服务端点接口中。在我上面发布的示例中,注释属于 HelloWorld 接口:

@Logging(limit=16000, inLocation="<stdout>", outLocation="<logger>", pretty=true)
public interface HelloWorld {
...

使用文件uris 将soap 消息记录在特定文件中,例如file:///c:/out.txt。

于 2013-04-22T09:17:33.163 回答