getviswa
getviswa

Reputation: 41

LOG4CPP with multiple files

I have configuration file, it has root category with two file appender. In the programming side I specified to two category. but both category has two appenders. I try to remove one appender from each category but it is not working. In the following code. I want to remove the A2 appender from categoryInfo, and A1 Appender from categoryData.

Configration file

    log4j.rootCategory=INFO, A1
    log4j.Categoty=INFO, A2
    log4j.appender.A1=org.apache.log4j.FileAppender
    log4j.appender.A1.fileName=A1.log
    log4j.appender.A1.layout=org.apache.log4j.BasicLayout

    log4j.appender.A2=org.apache.log4j.FileAppender
    log4j.appender.A2.fileName=A2.log
    log4j.appender.A2.layout=org.apache.log4j.BasicLayout

Code

    try 
    {
       log4cpp::PropertyConfigurator::configure("conf");
    }
    catch (log4cpp::ConfigureFailure e) 
    {
       cout<<"Log4cpp Error: "<<e.what()<<endl;
    }
    log4cpp::Category& categoryInfo = log4cpp::Category::getInstance("A1");
    log4cpp::Category& categoryData = log4cpp::Category::getInstance("A2");

    categoryInfo.setAdditivity(true);
    categoryData.setAdditivity(true);
    categoryData.setPriority(log4cpp::Priority::WARN);

    categoryInfo.removeAppender(log4cpp::Appender::getAppender(std::string("A2")));
    categoryData.removeAppender(log4cpp::Appender::getAppender(std::string("A1")));

Upvotes: 0

Views: 2963

Answers (1)

Adrian Shum
Adrian Shum

Reputation: 40056

I am not sure if I understand what you mean.

However is that u have mixed up Category(Logger) and Appender?

From my experience in Log4J, your config should be something like this:

log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.fileName=A1.log
log4j.appender.A1.layout=org.apache.log4j.BasicLayout

log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.fileName=A2.log
log4j.appender.A2.layout=org.apache.log4j.BasicLayout

log4j.rootCategory=ERROR, A1
log4j.category.data=INFO, A1    # only A1 in data logger
log4j.category.info=INFO, A2    # only A2 in info logger

And in your code, simply get the category "data" and "info" will give you the logger with your desired behavior

Upvotes: 3

Related Questions