Open Source Log System Log4cplus (i)

Source: Internet
Author: User

Log4cplus is a C + + written open source log system, the function is very comprehensive, using their own development of the project will be more professional,:, this article describes the basic concept of log4cplus, and how to install, configure.

				
### Profile ###
Log4cplus is a C + + written open source of the log system, the predecessor of the Java-written log4j system. By Apache Software License
Protection. The author is Tad E. Smith. Log4cplus is characterized by thread-safe, flexible, and granular control by dividing information into
Priority allows it to be oriented to program debugging, running, testing, and maintenance of the entire lifecycle; You can choose to output the information to the screen, file,
NT Event Log, or even a remote server, which periodically backs up the log by specifying a policy, and so on.
### Download ###
The latest log4cplus can be downloaded from the following Web site Http://log4cplus.sourceforge.net


The version used in this article is: 1.0.2
### Installation ###
1. Install under Linux
Tar xvzf log4cplus-x.x.x.tar.gz
CD log4cplus-x.x.x.
/Configure--prefix =/where/to/install
Make
Make install

Here I use the default installation path:/usr/local, if not specifically described below, whichever is the path.
2. Install under Windows
You do not need to install, there is a MSVC6 to store the source code and use cases, including the development project (for VC6 only), before using compile
The "Log4cplus_dll class" project generates a DLL, or compiles the "Log4cplus_static class" project to generate LIB.
### Configuration before use ###
1. Configuration under Linux
Make sure your makefile contains/USR/LOCAL/LIB/LIBLOG4CPLUS.A (static library) or-llog4cplus (Dynamic library),
The header file is in the/usr/local/include/log4cplus directory. For dynamic libraries, if you want to use them properly, you must also add the library installation path to the
In Ld_library_path, I usually do this: Log in as an administrator, add the installation path to the/etc/ld.so.conf, and here
Is/usr/local/lib, and then executes ldconfig to make the settings effective.
2. Configuration under Windows
Insert the DSP file for the "Log4cplus_dll class" project or the "Log4cplus_static class" project into your project, or directly
Put both the library and the header file directory into the search path for your project, if you use a static library, in your project
Add log4cplus_static to the preprocessor definitions of "project/setting/c++".
Introduction of ### Elements ###
Although the function is formidable, should say Log4cplus uses up or is more complicated, in order to use it better, first introduces its basic element.
Layouts: The layout controller, which controls the format of the output message.
Appenders: Hook, closely with the layout, to output a specific format of the message to the attached device terminal
(such as screens, files, etc.).
Logger: Logger, save and track object log information change entity, when you need to make an object
When you record, you need to generate a logger.
Categories: Classifier, hierarchical (hierarchy) structure, used for classification of recorded information, hierarchy
Each node maintains a logger of all information.
Priorities: priority, including Trace, DEBUG, INFO, WARNING, ERROR, FATAL.
				
This article describes the basic concepts of log4cplus, and how to install, configure, and the next article will illustrate how to use Log4cplus.

This article describes the use of Log4cplus with six steps and provides some examples to guide you through the basic use of log4cplus.

				
### Basic Use ###
There are six basic steps to using Log4cplus:
1. Instantiate a Appender object
2. Instantiate a Layout object
3. Bind the Layout object (attach) to the Appender object
4. Instantiate a Logger object and invoke a static function: Log4cplus::logger::getinstance ("Logger_name")
5. Bind the Appender object (attach) to the Logger object, such as omitting this step, and the standard output (screen) Appender object is bound to the logger
6. Set the priority of the logger, such as omitting this step, a variety of finite-level messages will be recorded
Here are some examples to understand the basic use of log4cplus.
Case 1〗

/**//* Strict implementation steps 1-6,appender output to the screen, where the layout format and loglevel are explained in detail later. */
#include < Log4cplus/logger.h >
#include < Log4cplus/consoleappender.h >
#include < log4cplus/layout.h >
using namespace Log4cplus;
using namespace Log4cplus::helpers;
int main ()
{
/**//* Step 1:instantiate a Appender Object * *
Sharedobjectptr _append (New Consoleappender ());
_append-> setname ("Append for Test");
/**//* Step 2:instantiate a Layout object * *
std:: string pattern = "%d{%m/%d/%y%h:%m:%s}-%m [%l]%n";
Std::auto_ptr _layout (new Patternlayout (pattern));
/**//* Step 3:attach the Layout object to the Appender * *
_append-> setlayout (_layout);
/**//* Step 4:instantiate a Logger object * *
Logger _logger = logger::getinstance ("test");
/**//* Step 5:attach the Appender object to the logger * *
_logger.addappender (_append);
/**//* Step 6:set a priority for the logger * *
_logger.setloglevel (All_log_level);
/**//* Log Activity * *
Log4cplus_debug (_logger, "This is the The" "")
Sleep (1);
Log4cplus_warn (_logger, "This are the SECOND log message")
return 0;
}

Output results:
10/14/04 09:06:24-this is the The "" "... [Main.cpp:31]
10/14/04 09:06:25-this is the SECOND log message ... [Main.cpp:33]
				
Case 2〗

/**//* Concise usage mode, appender output to screen. */
#include < Log4cplus/logger.h >
#include < Log4cplus/consoleappender.h >
using namespace Log4cplus;
using namespace Log4cplus::helpers;
int main ()
{
/**//* Step 1:instantiate a Appender Object * *
Sharedappenderptr _append (New Consoleappender ());
_append-> setname ("Append test");
/**//* Step 4:instantiate a Logger object * *
Logger _logger = Logger::getinsta

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.