Author: Zhu Jinchan
Source: http://blog.csdn.net/clever101/
As a powerful C + + log system, LOGCXX Library has been used more and more in the industry. However, the Logcxx library lacks the ability to format log output, but it cannot but say that it is a relatively large flaw. Of course you can say with the help of the format of the string class, and then plug it into the Logcxx library interface functions, such as the standard C library snprintf function, STL Std::ostringstream and MFC CString format interface. The disadvantage of snprintf is that you have to define a large buffer (not to worry about the log information), and it is not type-safe, std::ostringstream use security, but first define the Ostringstream object to construct the formatted string CString one is MFC program-specific, two it is not type-safe, three if you plug it into the Logcxx library interface, implied a CString to char* conversion operation. The approach I'm going to introduce is simpler and more secure than the one above, which is using the Boost library's Boost::format and Boost::lexical_cast.
Logcxx Library use method Here I do not repeat, is basically set up a configuration file, the definition of output mode, grade, etc., you can refer to this article: Log4cxx usage. Jin Qing proposed Boost::format and Logcxx library with the use of (see reference 2), I tried it very easy. But I find that logcxx and Boost::lexical_cast combine equally well. Here is the test code:
#include <string> using std::string; #include "boost/format.hpp" #include "boost/lexical_cast.hpp" #include "log4cxx/logger.h" #include "log4cxx/ Propertyconfigurator.h "//Get Log configuration information, define log class variable Log4cxx::P ropertyconfigurator::configure (_t (" Paint.config ")); Log4cxx::loggerptr Rootlogger = Log4cxx::logger::getlogger (_t ("Paint")); Log4cxx_debug (rootlogger,_t ("System ready to initialize/n")); Output the address of the variable i and j to log int i = 0; int j = 0; Log4cxx_debug (Rootlogger,boost::format ("I, J, respectively,%1% and%2%")% (&i)% (&j)); Log4cxx_debug (rootlogger,_t ("I, J address is") +boost::lexical_cast<string> (&i) +_t ("and") +boost::lexical_cast <string> (&j));
Compilation environment: Win XP + Sp3,vs C + + SP1
Reference documents:
1. Log4cxx Usage Detailed
2. Log4cxx with Boost::format, author Jin Qing.