Trouble understanding how to store an instance of a log4cpp logger in a class

494 views Asked by At

This question might get downvoted because its overly simple - but I am not sure of the pattern I am supposed to be following for these kinds of objects.

I would like to store an instance of a log4cpp logger in a class so I only need to instantiate it once. This, in my mind, is going to help me keep my code clean.

Imagine I have a class like so:

#include <log4cpp/Category.hh>
#include <log4cpp/Appender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/Layout.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/Priority.hh>

class ExampleClass {
private:
    log4cpp::Category& logger;

public:
    ExampleClass();
   ~ExampleClass();
}

With implementation:

ExampleClass::ExampleClass() {
    log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
    appender1->setLayout(new log4cpp::BasicLayout());

    log4cpp::Appender *appender2 = new log4cpp::FileAppender("default", "output.log");
    appender2->setLayout(new log4cpp::BasicLayout());

    logger = log4cpp::Category::getRoot();
    logger.setPriority(log4cpp::Priority::WARN);
    logger.addAppender(appender1);
    logger.addAppender(appender2);

}

According to the the documentation (http://log4cpp.sourceforge.net/#simpleexample) the function log4cpp::Category::getRoot() returns a log4cpp::Category&. So naturally I'd like to store this (as shown above).

Unfortunately, this does not work. I get the following error from my IDE: operator = is a private member of log4cpp::Category. Now i've realized I've gotten in over my head. I'm confused, especially considering the class documentation (http://log4cpp.sourceforge.net/api/classlog4cpp_1_1Category.html) seems to indicate what I've done here is correct.

I'd imagine that I've screwed up my understanding of pointers here (why am I passing around addresses via & rather than a pointer via indirection). Could someone explain to me how I can fix this up/where I have gone wrong?

Thank you!

0

There are 0 answers