I don't know if it's the method everyone else uses, but we work with it quite well.
We get a Logger: Logger logger = Logger.getLogger(this.getClass());
We use org.jboss.logging.Logger
With this instance we do all of our logging.
But we have the application already using instances of:
org.apache.log4j.Category ( or org.apache.log4j.Logger) all over the place.
And to tell you the truth, this is working, but in a wierd way:
Once any code that uses logging is run, the server's OWN logging is multiplying like crazy. Instead of one log entry, you get the same log entry up to 20 times!!!
I'm having the same problem as you as my log is being multiplied. Were you able to figure this out?
I am by no means an authority on this subject.
This is kind of a known problem with log4j and application containers. Or, rather, it's not really a problem because this is the expected behavior. Or so I've gathered.
From what I've puzzled out the problem is all about Class loading. When you instatiate a new Logger, it, by default uses the Hierarchy of the logger instantiated above it. Or something close to that anway. The end result is that your application's logging routines are forced into the same sandbox that JBoss's logging routines are in, thus it uses JBoss's appenders, etc..
This cycle can be broken, but bugger me if I've figured out how! You have to deploy your own copy of the log4j.jar file (I think?) with your application. You must then instatiate an instace of the Hierarchy from the context of your application, with a seperate configuration file!
I do not yet have a fully working example, but I'm working hard on it. If anyone beats me to it, please post your work.
Here is the related documentation that really gave me the heads up:
Supporting the log4j RepositorySelector in Servlet Containers
I actually did work the problem out. I'm sorry I forgot to post back with what I found....
In my case I did not use log4j. Instead I'm using java.util.logging which I then add a custom JDBC Handler to because I am logging to a DB table rather than a file or the JBoss server log file. Extra handlers where added to my logger because a bean kept adding handlers due to an error in my design and another class that was used kept adding handlers each time it was instantiated.
To fix the problem, I just added the handler the first time the bean object was created. If the bean happened to get created again in anther session, I had placed a check around the addHandler call to make sure the Logger object had no handlers so that the handler would not be added again resulting in multiple calls of the same log statement.
In pseudo code (since I'm not at work):
Logger logger = Logger.getLogger("new");
If (Logger.getHandlers().count == 0)
In other words, my guess is that if you are having this problem, somewhere in your code logic you are adding extra handlers to your Logger object. Therefore each time your log statement is multiplied and extra handler of the same type as before has been added to your Logger.
I i undrestood yr problem correctly, then its an issue with 'additvity' flag inr yr logger configutartion for that looger.
If you set this flag=false then it shouldn't multiply logging in.