File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logging Interceptor

 
Jeffrey Coleman
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that I see missing is the call to ctx.proceed() in the Interceptor class.
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeffrey Coleman
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic