1. Introduction
Log4cplus is an open-source log system written in C ++. The purpose of this project is to port the excellentlog for Java (log4j) logging library
C ++.
Log4cplus is flexible, powerful, simple to use, and secure with multiple threads. It is truly a boon to the enemy and the guerrillas.
2. Installation and Use (Linux)
Log4cplus installation and use is very simple, from its official website: http://log4cplus.sourceforge.net/download the latest version
Run:
Tar xvzf log4cplus-x.x.x.tar.gz
CD log4cplus-x.x.x
./Configure -- prefix =/where/to/install
Make
Make install
Generate the include and Lib folders under the installation directory, which are header files and library files respectively.
Usage:
G ++-O Server/mnt/HGFS/work_vm/project/work_project/Server/obj/main. o-l .. /.. // third/log4cplus/lib/-l .. /.. // third/Boost/lib/-llog4cplus-lpthread-I/mnt/HGFS/work_vm/project/work_project/Server/include-I .. /.. // third/log4cplus/include/-I .. /.. // third/Boost/include/
Compilation parameters:
-L ../.. // third/log4cplus/lib/
-Llog4cplus
-Lpthread
-I ../.. // third/log4cplus/include/
3. Use
Log4cplus mainly includes layout, appender, and loglevel. For details, refer:
Http://masterdog.bokee.com/153892.html
Nice
4. Packaging
The logcplus package is very convenient to use. The following is my package for your reference.
Log. h
// Log. h: interface for the log class. //////////////////////////////////////// /// // If! Defined (DEFINE _) # define afx_log_h1_b87f71e3_ffae_4cfa_a528_3f4f2ff7d69e00000000ded _ # include "log4cplus/loglevel. H "# include" log4cplus/NDC. H "# include" log4cplus/logger. H "# include" log4cplus/aggregator. H "# include" iomanip "# include" log4cplus/fileappender. H "# include" log4cplus/layout. H "# include" Const. H "# include" Common. H "# include" main_config.h "USI Ng namespace log4cplus; using namespace log4cplus: Helpers; // log encapsulation # define trace (p) log4cplus_trace (Log: _ logger, p) # define debug (P) log4cplus_debug (Log: _ logger, p) # define notice (p) log4cplus_info (Log: _ logger, p) # define warning (p) log4cplus_warn (Log: _ logger, p) # define fatal (p) log4cplus_error (Log: _ logger, p) // The log control class. A global log class log {public: // open the log bool open_log (); // obtain the log instance static log & instance (); s Tatic logger _ logger; private: log (); Virtual ~ Log (); // log file path and name char _ log_path [path_size]; char _ log_name [path_size] ;};# endif //! Defined (afx_log_h1_b87f71e3_ffae_4cfa_a528_3f4f2ff7d69e00000000ded _)
Log. cpp:
// Log.cpp: implementation of the Log class.////////////////////////////////////////////////////////////////////////#include "Log.h"//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////Logger Log::_logger = log4cplus::Logger::getInstance("main_log");Log::Log(){snprintf(_log_path, sizeof(_log_path), "%s", "../log");snprintf(_log_name, sizeof(_log_name), "%s/%s.%s", _log_path, execname, "log");}Log::~Log(){}Log& Log::instance(){ static Log log; return log;}bool Log::open_log(){ int Log_level = Main_config::instance().get_config().Read("LOG_LEVEL", 0); /* step 1: Instantiate an appender object */ SharedAppenderPtr _append(new FileAppender(_log_name)); _append->setName("file log test");/* step 2: Instantiate a layout object */std::string pattern = "[%p] [%d{%m/%d/%y %H:%M:%S}] [%t] - %m %n"; std::auto_ptr<Layout> _layout(new PatternLayout(pattern)); /* step 3: Attach the layout object to the appender */ _append->setLayout(_layout); /* step 4: Instantiate a logger object */ /* step 5: Attach the appender object to the logger */ Log::_logger.addAppender(_append);/* step 6: Set a priority for the logger */ Log::_logger.setLogLevel(Log_level); return true;}
Int log_level = main_config: instance (). get_config (). Read ("log_level", 0 );
Main_config is a configuration class I wrap myself (refer to: http://blog.csdn.net/yfkiss/article/details/6802451) that sets log level by reading the configuration.
Usage:
# Inlucde "log. H"
When the program is initialized:
// Open the log
If (! Log: instance (). open_log ())
{
STD: cout <"log: open_log () failed" <STD: Endl;
Return false;
}
Then, use notice and fatal to print logs to files.
# Include "log. H"... // open the log if (! Log: instance (). open_log () {STD: cout <"log: open_log () failed" <STD: Endl; return false ;}..... notice ("server init succ"); fatal ("Server Run failed ");