You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "KARR, DAVID (ATTSI)" <dk...@att.com> on 2011/02/21 20:45:58 UTC

Dynamically turning on request/response logging in jax-rs

I need to investigate how I can dynamically turn on request/response
logging in JAX-RS.  The doc page on logging appears to assume we're
doing JAX-WS.  It appears I can turn on logging statically by adding the
"cxf:logging" element to the "jaxrs:features" element, but that's not
what I want.

Re: Dynamically turning on request/response logging in jax-rs

Posted by Sergey Beryozkin <sb...@gmail.com>.
>
> Is it possible to remove the interceptors dynamically also?  I'd have to be able to turn logging on and off dynamically.

If you add the way Dan suggested then they will only affect the
current invocation/exchange

>
>> Alternatively to using the pair of logging interceptors, you may want
>> to experiment with PersistInInterceptor and PersistOutInterceptor in
>> the rt/management module. The PersistOutInterceptor will provide the
>> Exchange details to the injected handler.
>
> What would be the tradeoffs of these two approaches?
>
PersistOutInterceptor will just delegate to the handler - so perhaps
one will be able to log selectively, persist the data onto the db,
etc. The ExchangeData bean may need to be updated a bit though - it
records the input/output streams as Strings as opposed to Input/Output
streams (and thus its setRequestSize/setResponseSize are not needed,
etc...) and it has some properties that need to be collapsed into say
ExceptionData bean, etc...

cheers, Sergey

RE: Dynamically turning on request/response logging in jax-rs

Posted by "KARR, DAVID (ATTSI)" <dk...@att.com>.
> -----Original Message-----
> From: Sergey Beryozkin [mailto:sberyozkin@gmail.com]
> Sent: Tuesday, February 22, 2011 2:53 AM
> To: users@cxf.apache.org
> Subject: Re: Dynamically turning on request/response logging in jax-rs
> 
> Hi
> 
> On Mon, Feb 21, 2011 at 7:45 PM, KARR, DAVID (ATTSI) <dk...@att.com>
> wrote:
> > I need to investigate how I can dynamically turn on request/response
> > logging in JAX-RS.  The doc page on logging appears to assume we're
> > doing JAX-WS.  It appears I can turn on logging statically by adding
> the
> > "cxf:logging" element to the "jaxrs:features" element, but that's
> not,
> > what I want.
> 
> You may want to extend CXF LoggingInInterceptor and in its
> handleMessage(Message inMessage) method decide whether to delegate to
> the superclass or not.
> The message properties such as Message.HTTP_REQUEST_METHOD,
> Message.PROTOCOL_HEADERS, Message.REQUEST_URI, Message.QUERY_STRING
> can help...
> 
> Then you can register this custom LoggingInInterceptor in the
> jaxrs:inInterceptors.
> 
> I'm not sure though if you can dynamically add an outbound
> LoggingOutInterceptor dynamically from the in custon interceptor, may
> be the following will work:
> 
> if (loggingHasToBeEnabled) {
> 
>     // add LoggingOutInterceptor to the outbound message
> 
>     Message outMessage = new MessageImpl();
>     outMessage.getInterceptorChain().add(new LoggingOutInterceptor());
>     Exchange exchange = inMessage.getExchange();
>     exchange.setOutMessage(outMessage);
>     outMessage.setExchange(exchange);
> 
>     // finally, delegate to the super class for the inbound request be
> logged:
>     super.handleMessage(inMessage);
> }
> 
> This may work - if not then you can create a custom
> LoggingOutInterceptor subclass, register it in the
> jaxrs:outInterceptors, and in its handleMessage(Message outMessage)
> check the property on the outMessage.getExchange() which your custom
> LogginInInterceptor will set if needed on the inMessage.getExchange()
> and if this enabling property has been set then delegate to the
> superclass.

Is it possible to remove the interceptors dynamically also?  I'd have to be able to turn logging on and off dynamically.

> Alternatively to using the pair of logging interceptors, you may want
> to experiment with PersistInInterceptor and PersistOutInterceptor in
> the rt/management module. The PersistOutInterceptor will provide the
> Exchange details to the injected handler.

What would be the tradeoffs of these two approaches?

Re: Dynamically turning on request/response logging in jax-rs

Posted by Daniel Kulp <dk...@apache.org>.
On Tuesday 22 February 2011 5:52:39 AM Sergey Beryozkin wrote:
> Hi
> 
> On Mon, Feb 21, 2011 at 7:45 PM, KARR, DAVID (ATTSI) <dk...@att.com> wrote:
> > I need to investigate how I can dynamically turn on request/response
> > logging in JAX-RS.  The doc page on logging appears to assume we're
> > doing JAX-WS.  It appears I can turn on logging statically by adding the
> > "cxf:logging" element to the "jaxrs:features" element, but that's not,
> > what I want.
> 
> You may want to extend CXF LoggingInInterceptor and in its
> handleMessage(Message inMessage) method decide whether to delegate to
> the superclass or not.
> The message properties such as Message.HTTP_REQUEST_METHOD,
> Message.PROTOCOL_HEADERS, Message.REQUEST_URI, Message.QUERY_STRING
> can help...
> 
> Then you can register this custom LoggingInInterceptor in the
> jaxrs:inInterceptors.
> 
> I'm not sure though if you can dynamically add an outbound
> LoggingOutInterceptor dynamically from the in custon interceptor, may
> be the following will work:

You can.  I just discovered this yesterday while looking at some stuff with 
Christan.  :-)

You can do:

message.put(Message.OUT_INTERCEPTORS,
     Collections.singletonList(new LoggingOutInterceptor()));

and it would add that to the out chain when it's calculated.

Dan


 
> if (loggingHasToBeEnabled) {
> 
>     // add LoggingOutInterceptor to the outbound message
> 
>     Message outMessage = new MessageImpl();
>     outMessage.getInterceptorChain().add(new LoggingOutInterceptor());
>     Exchange exchange = inMessage.getExchange();
>     exchange.setOutMessage(outMessage);
>     outMessage.setExchange(exchange);
> 
>     // finally, delegate to the super class for the inbound request be
> logged: super.handleMessage(inMessage);
> }
> 
> This may work - if not then you can create a custom
> LoggingOutInterceptor subclass, register it in the
> jaxrs:outInterceptors, and in its handleMessage(Message outMessage)
> check the property on the outMessage.getExchange() which your custom
> LogginInInterceptor will set if needed on the inMessage.getExchange()
> and if this enabling property has been set then delegate to the
> superclass.
> 
> Alternatively to using the pair of logging interceptors, you may want
> to experiment with PersistInInterceptor and PersistOutInterceptor in
> the rt/management module. The PersistOutInterceptor will provide the
> Exchange details to the injected handler.
> 
> 
> 
> Cheers, Sergey

-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog
Talend - http://www.talend.com

Re: Dynamically turning on request/response logging in jax-rs

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On Mon, Feb 21, 2011 at 7:45 PM, KARR, DAVID (ATTSI) <dk...@att.com> wrote:
> I need to investigate how I can dynamically turn on request/response
> logging in JAX-RS.  The doc page on logging appears to assume we're
> doing JAX-WS.  It appears I can turn on logging statically by adding the
> "cxf:logging" element to the "jaxrs:features" element, but that's not,
> what I want.

You may want to extend CXF LoggingInInterceptor and in its
handleMessage(Message inMessage) method decide whether to delegate to
the superclass or not.
The message properties such as Message.HTTP_REQUEST_METHOD,
Message.PROTOCOL_HEADERS, Message.REQUEST_URI, Message.QUERY_STRING
can help...

Then you can register this custom LoggingInInterceptor in the
jaxrs:inInterceptors.

I'm not sure though if you can dynamically add an outbound
LoggingOutInterceptor dynamically from the in custon interceptor, may
be the following will work:

if (loggingHasToBeEnabled) {

    // add LoggingOutInterceptor to the outbound message

    Message outMessage = new MessageImpl();
    outMessage.getInterceptorChain().add(new LoggingOutInterceptor());
    Exchange exchange = inMessage.getExchange();
    exchange.setOutMessage(outMessage);
    outMessage.setExchange(exchange);

    // finally, delegate to the super class for the inbound request be logged:
    super.handleMessage(inMessage);
}

This may work - if not then you can create a custom
LoggingOutInterceptor subclass, register it in the
jaxrs:outInterceptors, and in its handleMessage(Message outMessage)
check the property on the outMessage.getExchange() which your custom
LogginInInterceptor will set if needed on the inMessage.getExchange()
and if this enabling property has been set then delegate to the
superclass.

Alternatively to using the pair of logging interceptors, you may want
to experiment with PersistInInterceptor and PersistOutInterceptor in
the rt/management module. The PersistOutInterceptor will provide the
Exchange details to the injected handler.



Cheers, Sergey