Hi,
I see some problems in your code:
1. Why first request isn't called:
logger_file.info(payload) in interceptor is called BEFORE onClose() method
will be invoked.
Therefore first request is not logged. When first request fills the payload
field, it will be logged by the next call.
2. Message Id is null. I think CXF doesn't cares about message id
automatically, you need to set up it yourself in code like:
message.put("My.Key.Identifier", key);
See
http://mail-archives.apache.org/mod_mbox/cxf-users/200906.mbox/%[email protected]%3E
3. I would suggest to consider standard CXF
LoggingOutInterceptor/LoggingInInterceptor. You still can customize id used by
by setting LoggingMessage.ID_KEY property in exchange:
message.getExchange().put(LoggingMessage.ID_KEY, myId);
Regards,
Andrei.
> -----Original Message-----
> From: Stefan Taucher [mailto:[email protected]]
> Sent: Montag, 22. Dezember 2014 16:23
> To: [email protected]
> Subject: LoggingOutInterceptor problem getting message id
>
> We want to log the Request/Response to an ELK-Stack an therefor building
> custom IN/OUT LoggingInterceptors.
>
> The InInterceptor works flawlessly but the Out is making some problems.
>
> Here are my 2 problems:
>
> First of all i never get a massage id (which is essentual for our logging
> system)
> and the secound thing is that the first Response is never logged!
>
> Here is the code
>
> public class GelfLoggingOutInterceptor extends LoggingOutInterceptor
> implements CachedOutputStreamCallback {
>
> private String payload;
>
> public GelfLoggingOutInterceptor() {
> super(Phase.WRITE);
> }
>
> @Override
> public void handleMessage(Message message) throws Fault {
> super.handleMessage(message);
> Logger logger_file =
> LoggerFactory.getLogger(GelfLoggingOutInterceptor.class);
> try
> {
> Logger logger_gelf = LoggerFactory.getLogger("GELFAPPENDER");
>
> OutputStream os = message.getContent(OutputStream.class);
> CacheAndWriteOutputStream cwos = new
> CacheAndWriteOutputStream(os);
> message.setContent(OutputStream.class, cwos);
>
> cwos.registerCallback(this);
>
> String id = (String) message.get(Message);
> Integer responseCode = (Integer) message.get(Message.RESPONSE_CODE);
> String encoding = (String) message.get(Message.ENCODING);
> String contenttype = (String) message.get(Message.CONTENT_TYPE);
> Object headers = message.get(Message.PROTOCOL_HEADERS);
>
> MDC.put("ID", id);
> if (responseCode != null)
> MDC.put("ResponseCode", responseCode);
> MDC.put("Encoding", encoding);
> MDC.put("Content-Type", contenttype);
> if (headers != null)
> MDC.put("Header", headers);
> MDC.put("Message-Type", "Outbound");
>
> logger_gelf.info(payload);
> }
>
> catch (Exception ex)
> {
> ex.printStackTrace();
> StringBuilder sb = new StringBuilder();
> sb.append("Error at writing the gelf log ");
> sb.append(ex.getMessage());
> logger_file.warn(sb.toString());
> }
> }
>
> @Override
> protected void writePayload(StringBuilder builder, CachedOutputStream cos,
> String encoding, String contentType) throws Exception {
> super.writePayload(builder, cos, encoding, contentType);
>
> }
>
> @Override
> public void onClose(CachedOutputStream cos) {
> {
> try
> {
> if (cos != null)
> {
> payload = IOUtils.toString(cos.getInputStream());
> }
>
> } catch (Exception e)
> {
> e.printStackTrace();
> }
> }
>
> }
>
> @Override
> public void onFlush(CachedOutputStream arg0) {
> }
>
> }
>
>
> Can anybody please help me!
>
> Thanks for reading! :D