log4cpp::BasicLayout – Log Format with Additional Information

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

The Log4cpp SimpleLayout is the simplest log layout with the format “priority message”. But for regular usage, you need more information especially information like the timestamp. log4cpp::BasicLayout has the following Layout

timeStamp priority category ndc: message

where timestamp corresponds to the timestamp when the message was generated, priority corresponds to the type of the logging  event like ERROR, WARN (warning), category corresponds to the category used for the logging purpose and message is the string specified by the program.

The Syntax of the BasicLayout constructor is


log4cpp::BasicLayout::BasicLayout()

As you can see, similar to SimpleLayout, this constructor doesn’t require any parameters.

Check the following program


#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, layout and Category*/
 log4cpp::Appender *appender = new log4cpp::OstreamAppender("OstreamAppender",&cout);

 log4cpp::Layout *layout = new log4cpp::BasicLayout();
 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");
}

Check the output of the program


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

If this format also doesn’t suffice your requirements, you can create your own layout

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.