Originally posted by Bear Bibeault:
It's more common, and would solve your problem, to have each class have its own logger. The idiom I follow in logged classes:
42
Originally posted by Jeroen Wenting:
to explain what's going on and why you get the error, I'm going to have to get somewhat theoretical
The keyword 'this' means the current instance of an object. It's therefore meaningless when talking about code that doesn't apply to any specific instance.
The keyword 'static' on the other hand means you're talking about global code that doesn't belong to any specific instance.
Therefore the two are mutually exclusive and can never be used together.
You can do several things. One is to create a single static logger which you can call from everywhere using MyLogger.log() (for example), the other is to give each class it's own static logger instance.
Originally posted by Bear Bibeault:
One of the benefits of log4j is the ability to have a hierarchical organization of loggers. By letting each class have its own named after its fully qualified class name, you have a logging hierarchy that mirrors the class hierarchy.
While thinking in object-orientation is a fine idea, you would also be tying your logging to only classes that inherit from a log-enabled base class.
If future refactoring is a concern, you can always abstract your own logging API that uses whatever your logging-du-jour package might be.
And, as you have painfully discovered, static classes and inheritance do not mix.
There are, of course, numerous other idioms that can be used. I'm surprised no one else has chimed in. (Actually, I'm not. Your topic subject isn't going to attract log4j users -- you might want to consider changing it; or perhaps posting a "How do YOU use log4j" topic).