wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Logging Interceptor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Logging Interceptor" Watch "Logging Interceptor" New topic

Logging Interceptor

Jeffrey Coleman

Joined: May 25, 2012
Posts: 23
I'm having an issue getting this class level interceptor to work. I would like to create an interceptor that initilizes log4j for each EJB that is called since I'm logging nearly everything at this point anyway and will soon be putting in something similar for auditing.

Here is the code of the interceptor class, then the EJB that is being intercepted, and the stack trace of the error.

I also tried it via xml in the ejb-jar.xml:

Any help is appreciated.
Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1076

One thing that I see missing is the call to ctx.proceed() in the Interceptor class.

Jaikiran Pai

Joined: Jul 20, 2005
Posts: 9924

There are multiple issues with that interceptor class:

1) It doesn't have an @AroundInvoke method to intercept the business invocations on the bean
2) The @PostConstruct method's parameter is a WebSphere specific class:

import com.ibm.ws.sib.wsn.InvocationContext;

You probably used the IDE's auto complete there and selected the wrong package.

[My Blog] [JavaRanch Journal]
Jeffrey Coleman

Joined: May 25, 2012
Posts: 23
You were right. I actually fixed those issues.

Now I'm wondering how to do something. Every time any EJB is called I want the name of the ejb logged and any time a method is called I want that method logged along with its EJB and parameters. How do I accomplish this using AOP?

I'm assuming that once I'm out of development I can set my log4j properties file to log level Error and then I won't get the debug msgs and if I'm in validation or integration and have an issue I'll be able to change the log level and trace the application.

Can I do this? Is this a bad idea?
Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1076

You need to write method in the interceptor with @AroundInvoke annotation for intercepting the invocation. This method should also take an InvocationContext as a param.

The format of the method will be usually like this:
<<statements before method invocation >>
ctx.proceed(); //this invokes the actual EJB method
<< statements after invocation >>

The InvocationContext variable will have lot of information from which you can get the name of the method being invoked. I am not sure if you can get the parameter list though.
So, you can add logging statements at appropriate statements with the appropriate logging level. And yes, the logging level can be controlled via the configuration files. Sure, you can do that.
wood burning stoves
subject: Logging Interceptor
Similar Threads
log4j setup
org.apache.struts2.dispatcher.Dispatcher serviceAction Could not find action or result
Validator framework vs JBoss issue.
Capturing JSP Content Within My Strut Action Servlet
Log4j ClassNotFoundException