log4cpp::BasicLayout – Log Format with Additional Information
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
Comments: