On May 12, 2006, at 6:57 PM, [EMAIL PROTECTED] wrote:
Hi,
I was wondering what the consensus is on correct use of the logger,
if there is such a thing.
Right now, in order to be able to use it in my entire project I
have made the code included below. The code works as intended but
I'm not familiar with the use of the "extern" keyword and I was
wondering if my code is "correct" and optimal or whether
improvement is possible.
At any rate, thanks for any advice.
The more typical way of using log4cxx is to have a logger member
variable in each class and name the logger after the class. In that
pattern, you'd have header files like:
class FooBar {
private:
static log4cxx::LoggerPtr logger;
public:
FooBar();
...
}
and implementation files like:
#include "foobar.h"
log4cxx::LoggerPtr FooBar::logger(Logger::getLogger("FooBar"));
FooBar::FooBar() {
LOG4CXX_INFO("Entering constructor");
}
What you have in your example is valid, but atypical. The reason
that you need the extern on all but one declaration is since there
can only be one LoggerPtr named ::logger (logger without a qualifying
class or namespace name) in the link. The one without the extern
allocates and initializes the ::logger symbol, all the ones with
extern access that single instance of LoggerPtr. By only using one
logger in your application, you lose the ability to increase or
decrease the logging at a class or functional level.