I. Introduction
Log4cplus is an open-source logstore written in C ++ with comprehensive functions. In a word, developers can record logs to custom locations in a custom format.
Log4cplus, formerly known as log4j written in java, belongs to the Apache project and is prepared by Tad E. Smith. Log4cplus has the features of thread security, flexibility, and multi-granularity control. By dividing information into priorities, log4cplus can face the entire life cycle of program debugging, running, testing, and maintenance; developers can choose to output information to screens and files.
Compared with the previously used glog, I personally think that the biggest benefit of log4cplus is comprehensive and flexible. developers can customize the file to write logs to avoid glog generating massive small log files.
Ii. Installation
1. Download: http://log4cplus.sourceforge.net/index.html
I downloaded version 1.1.1.
2. Unzip and install:
Tar xvzf log4cplus-x.x.x.tar.gz
Cd log4cplus-x.x.x
./Configure -- prefix =/where/to/install
Make
Make install
Note:-llog4cplus must be added during compilation.
Iii. Composition
1. Appenders: the device that decides to receive logs, such as the console, file, and network.
2. Layouts: Layouts to control the format of the Output Message.
3. Logger: Logger and log object.
4. Filter: filters are used to solve information output problems, such as output control of DEBUG, WARR, and INFO.
5. Priorities: priority, including TRACE, DEBUG, INFO, WARNING, ERROR, and FATAL.
.
Iv. Sample Code
#include <iostream>#include <log4cplus/logger.h>#include <log4cplus/loggingmacros.h>#include <log4cplus/configurator.h>#include <log4cplus/fileappender.h>#include <log4cplus/consoleappender.h>#include <log4cplus/layout.h>// using namespace log4cplus;// using namespace log4cplus::helpers;using std::cout;using std::endl;using std::auto_ptr;using std::string;using log4cplus::Logger;using log4cplus::ConsoleAppender;using log4cplus::FileAppender;using log4cplus::Appender;using log4cplus::Layout;using log4cplus::PatternLayout;using log4cplus::helpers::SharedObjectPtr;int main () { /* step 1: Instantiate an appender object */ SharedObjectPtr<Appender> append(new FileAppender("master.log")); append->setName(LOG4CPLUS_TEXT("append for master")); /* step 2: Instantiate a layout object */ auto_ptr<Layout> layout(new PatternLayout(LOG4CPLUS_TEXT("%d{%m/%d/%y %H:%M:%S} %p [%l]: %m %n"))); /* step 3: Attach the layout object to the appender */ append->setLayout(layout); /* step 4: Instantiate a logger object */ Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("master")); /* step 5: Attach the appender object to the logger */ logger.addAppender(append); /* step 6: Set a priority for the logger */ logger.setLogLevel(log4cplus::ALL_LOG_LEVEL); LOG4CPLUS_DEBUG(logger, "This is the FIRST log message"); return 0;}
This article is from the "talking Cabbage" blog, please be sure to keep this source http://speakingbaicai.blog.51cto.com/5667326/1274823