Example of log4cpp properties configuration

Source: Internet
Author: User
For more information about log priority, see the source code log4cpp-0.3.5rc3/include/log4cpp/priority. hh from high to low.

EMERG
FATAL
ALERT
CRIT
ERROR
WARN
NOTICE
INFO
DEBUG
NOTSET

Corresponding to the category function, see the source code log4cpp-0.3.5rc3/include/log4cpp/category. HH

 

Category::emerg()
Category::fatal()
Category::alert()
Category::crit()
Category::error()
Category::warn()
Category::notice()
Category::info()
Category::debug()

 

The preceding functions have two overload functions, which can be formatted strings or STD: String, such as debug ().

 

void debug(const char* stringFormat, ...) throw();
void debug(const std::string& message) throw();

 

Suggestions on priority usage
During Development and running, it is set to debug level, while during official operation, it is set to notice;
You can use notice or above to display the information;
The info level is used to trace the function running traces;
Debug level is used for debugging information during running;
Example

 

void initialize(int argc, char* argv[])
{
log.info("initialize() : argc=%d", argc);

for (int i=0; i < argc; ++i)
{
    log.debug("initialize() : argv[%d]=%s", i, argv[i]);
}

log.notice("initialize() : done");
}

 

Log4cpp category is divided into rootcategory and other custom category.

Each category can be output to multiple appender. And category is also inclusive.

For example, rootcategory is the root of all category. The custom category can also be defined in the configuration file.

First look at the configuration of a rootcategory

 

log4cpp.rootCategory=DEBUG, console, sample

 

In this definition, the log priority of rootcategory is set to debug, And the appender has two options: console and sample.

That is to say, the content on the right of the equal sign is separated by commas. The first item is the priority level, followed by the appender name, which can have one or more.

Now let's take a look at the custom category.

 

log4cpp.category.demo=DEBUG, sample

 

Here, a category named demo is defined. Its priority is debug and appender is sample.

Note that the category and appender names can be identical.

Let's take a look at the definition of a category with an inclusive relationship.

 

log4cpp.category.demo.son=DEBUG, son
log4cpp.category.demo.daughter=DEBUG, daughter

 

The preceding two categories are defined: son and daughter, and its parent category is demo.

Logs generated by son are written to the appender of son and demo. Similarly, daughter logs are written to daughter and demo appender.

Now let's take a look at the definition of appender. There are many appender types. Here I will only introduce several types, which are

 

Consoleappender: Console output, that is, STD: cout
Win32debugappender: VC ide output, namely: outputdebugstring
Fileappender: file output
Rollingfileappender: to be studied

 

Now let's look at an example of leleappender.

 

log4cpp.appender.console=ConsoleAppender
log4cpp.appender.console.layout=PatternLayout
log4cpp.appender.console.layout.ConversionPattern=%d [%p] - %m%n

 

The above information is interpreted as: An appender named console, whose type is leleappender. That is, the layout of the log output by the console is the specified style.
The output format is "% d [% P]-% m % N" (explained later)

Let's look at another example of fileappender.

 

log4cpp.appender.sample=FileAppender
log4cpp.appender.sample.fileName=sample.log
log4cpp.appender.sample.layout=PatternLayout
log4cpp.appender.sample.layout.ConversionPattern=%d [%p] - %m%n

 

The above information is interpreted as: An appender named sample, its type is fileappender, that is, the log file name specified by the file output is sample. log, the output layout is the specified style, and the output format is "% d [% P]-% m % N"

Corresponding to the configuration method of category and appender, you can find

Category is "log4cpp. category." + "category name"

CATEGORY names can be separated by "." To identify the inclusion relationship.

Appender is "log4cpp. appender." + "appender name"

Appender names cannot be separated by ".", that is, appender has no inclusion relationship.

Now let's look at a complete configuration file example.

 

# Define the attributes of root category
Log4cpp. rootcategory = debug, console

 

# Define console attributes
Log4cpp. appender. Console = consoleappender
Log4cpp. appender. Console. layout = patternlayout
Log4cpp. appender. Console. layout. conversionpattern = % d [% P]-% m % N

 

# Define the attributes of sample category
Log4cpp. Category. Sample = debug, sample

 

# Define the attributes of the sample appender
Log4cpp. appender. Sample = fileappender
Log4cpp. appender. sample. filename = sample. Log
Log4cpp. appender. sample. layout = patternlayout
Log4cpp. appender. sample. layout. conversionpattern = % d [% P]-% m % N

 

# Define the attributes of sample. Son category
Log4cpp. Category. sample. Son = debug, son

 

# Define son appender attributes
Log4cpp. appender. Son = fileappender
Log4cpp. appender. Son. filename = Son. Log
Log4cpp. appender. Son. layout = patternlayout
Log4cpp. appender. Son. layout. conversionpattern = % d [% P]-% m % N

 

# Define the attributes of sample. Daughter category
Log4cpp. Category. sample. Daughter = debug, daughter

 

# Define the attributes of daughter appender
Log4cpp. appender. Daughter = fileappender
Log4cpp. appender. Daughter. filename = daughter. Log
Log4cpp. appender. Daughter. layout = patternlayout
Log4cpp. appender. Daughter. layout. conversionpattern = % d [% P]-% m % N

 

Conversionpattern Parameter Interpretation, see the source code log4cpp-0.3.5rc3/src/patternlayout. cpp

 

% M log message content, that is, the specific information of the log written by the user
% N carriage return line feed
% C category name
% D Timestamp
% P priority
% R: interval between the last log write, in milliseconds
% R: interval between the last log write, in seconds
% T thread name
% U processor time
% X NDC?

 

The following format is enough to output "time [thread name] Priority-log Content carriage return line feed"

 

%d [%t] %p - %m%n

 

The configuration knowledge is almost the same. Now let's look at the actual code application.

First, initialize the log4cpp configuration. For example, my configuration file name is log4cpp. properties.

 

try
{
log4cpp::PropertyConfigurator::configure("log4cpp.properties");
}
catch (log4cpp::ConfigureFailure & f)
{
std::cerr << "configure problem " << f.what() << std::endl;
}

 

After Initialization is complete, you can use it like this (you can find out the specific application skills)

 

log4cpp::Category & log = log4cpp::Category::getInstance(std::string("sample"));

log.debug("test debug log");
log.info("test info log");

 

// Use sample. Son

log4cpp::Category & log = log4cpp::Category::getInstance(std::string("sample.son"));

log.debug("test debug log of son");
log.info("test info log of son");


// Use sample. Daughter

log4cpp::Category & log = log4cpp::Category::getInstance(std::string("sample.daughter"));

log.debug("test debug log of daughter");
log.info("test info log of daughter");

 

Another example is the usage in the Custom class.

 

#include <log4cpp/Category.hh>

namespace demo
{
class Sample
{
public:
    Sample();
    ~Sample();

public:
    Testing(int i);

private:
    static log4cpp::Category & log;
};

}

 

 

#include "Sample.h"

namespace demo
{
log4cpp::Category & Sample::log = log4cpp::Category::getInstance(std::string("sample"));

Sample::Sample()
{
    log.debug("Sample::Sample()");
}

Sample::~Sample()
{
    log.debug("Sample::~Sample()");
}

Sample::Testing(int i)
{
    log.debug("Sample::Testing() : i=%d", i);
}
}

Example of log4cpp properties configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.