log4cpp::SimpleLayout – A Simple Layout for log4cpp logs

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

log4cpp supports three different Layouts, one of which is the SimpleLayout. log4cpp::SimpleLayout has the following format for every log message

priority message

where priority is one of the different values like DEBUG, WARN, NOTICE, INFO, CRIT, ERROR and the message is the same as the string which you want to print.

Initializing a SimpleLayout is as simple as calling the constructor SimpleLayout() with no parameter


log4cpp::SimpleLayout::SimpleLayout()

The program below uses the simple layout

#include <stdio.h>
#include <stdlib.h>
#include <log4cpp/category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/SimpleLayout.hh>

using namespace std;

int main()
{
 /*Setting up Appender, <span id="IL_AD4">layout</span> and Category*/
 log4cpp::Appender *appender = new log4cpp::OstreamAppender("OstreamAppender",&cout);

 log4cpp::Layout *layout = new log4cpp::SimpleLayout();
 log4cpp::Category& category = log4cpp::Category::getInstance("Category");

 appender->setLayout(layout);
 category.setAppender(appender);
 category.setPriority(log4cpp::Priority::INFO);

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

The output of the program is something like this displayed on the standard output


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


As you can see, this is the simplest layout with no meaningful information like the time it was generated, the hostname of the machine.

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.