log4cpp::PatternLayout – Available log4cpp log formats

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

log4cpp supports three layouts: Simple Layout, Basic Layout and PatternLayout. Simple and Basic Layout are the simplest ones and you need not configure them in any way. The display the log in a fixed format. log4cpp::PatternLayout has the additional advantage that it supports four different formats and provides a mechanism to create a format of your own.

PatternLayout has the following constructor

log4cpp::PatternLayout::PatternLayout()

which can be used to initialize a layout. There are four log formats

  1. DEFAULT_CONVERSION_PATTERN = “%m%n”
  2. SIMPLE_CONVERSION_PATTERN = “%p – %m%n”
  3. BASIC_CONVERSION_PATTERN = “%R %p %c %x: %m%n”
  4. TTCC_CONVERSION_PATTERN = “%r [%t] %p %c %x – %m%n”

By default, the DEFAULT_CONVERSION_PATTERN is used to display the logs. This is the simplest log format which simply displays the log message followed by a line separator (newline).
To make use of other formats, you can use

void log4cpp::PatternLayout::setConversionPattern(const std::string &conversionPattern)

substitute conversionPattern with any of the above patterns. For more information on what each of the symbols %d, %m mean, check PatternLayout definition.

The following program makes use of PatternLayoout


#include <stdio.h>
#include <stdlib.h>
#include <log4cpp/category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PatternLayout.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::PatternLayout *layout = new log4cpp::PatternLayout();
 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");
 layout->setConversionPattern(log4cpp::PatternLayout::SIMPLE_CONVERSION_PATTERN);
 category.notice("This is to notify certain events");
 layout->setConversionPattern(log4cpp::PatternLayout::BASIC_CONVERSION_PATTERN);
 category.warn("This is to generate certain warnings");
 layout->setConversionPattern(log4cpp::PatternLayout::TTCC_CONVERSION_PATTERN);
 category.error("This is to generate errors");
}

The output of the program as seen on the standard output


This is for tracing the flow
NOTICE - This is to notify certain events
1263459000 WARN Category : This is to generate certain warnings
0 [-1608374496] ERROR Category  - This is to generate errors

See how we make use of all the above patterns in our program

Tags: , , , ,

Comments:

1 Comment

  • Ash says:

    Hey,
    I’m having a problem with log4cpp output. When I run:
    category.infoStream() << "testing125: Accessed class correctly: " << testClassNameString.c_str();

    The literal string is displayed correctly, but the variable access of the char pointer just prints some address. Other variable accesses do not show up as any output at all. Any ideas?

    Thanks,
    As

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.