log4cpp::PropertyConfigurator – Configure with configuration files

Posted by Joys of Programming on in C/C++

log4cpp can also be configured with the configuration files. log4cpp::PropertyConfigurator can be used for this purpose. It has a single function which takes as input the configuration file.

void 	configure (const std::string &initFileName)

Simply specify the configuration in a file. You must specify the layout, priority and the appender in the configuration file as shown here


log4j.rootCategory=DEBUG, rootAppender
log4j.appender.rootAppender=org.apache.log4j.ConsoleAppender
log4j.appender.rootAppender.layout=org.apache.log4j.BasicLayout

You can read here for more on configuration.

Check the following program and how the configuration file is used


#include <stdio.h>
#include <stdlib.h>
#include <log4cpp/Category.hh>
#include <log4cpp/PropertyConfigurator.hh>
#include <log4cpp/Configurator.hh>

using namespace std;

int main()
{
 /*Setting up Appender, layout and Category*/
 try {
 log4cpp::PropertyConfigurator::configure("/home/user/config");
 }
 catch (log4cpp::ConfigureFailure e) {
 cout<<"Log4cpp Error: "<<e.what()<<endl;
 }
 log4cpp::Category& category = log4cpp::Category::getInstance("Category");

 category.info("This is for tracing the flow");
 category.notice("This is to notify certain events");
 category.warn("This is to generate certain warnings");
}

As you have seen, the values in the configuration file tells the PropertyConfigurator to use the console as appender.


1263575757 INFO Category : This is for tracing the flow
1263575757 NOTICE Category : This is to notify certain events
1263575757 WARN Category : This is to generate certain warnings


Tags: , , , , , ,

Comments:

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.