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.
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.
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.