最近看了下日志系统,目前有很多种,比如qlog,log4j家族的log4cpp,log4cxx,log4cplus,boost.log,log4qt,Logger等等目前自己比较喜欢的log4cplus和boost.log,后者有时间再研究下。
下载log4cplus 编译./configure -> make ->make check->make install 后默认安装路径在/usr/local/lib /usr/local/include 下面
一般开发我把这两个目录下文件拷贝到工程中
Qt 工程中配置:
LIBS += -L"$PWD/../../Log4cplusTest/libs" -llog4cplus
INCLUDEPATH +="$PWD/../../Log4cplusTest/include/log4cplus"
注意:$PWD 是你运行程序路径
测试代码:
#include <iostream>
#include <unistd.h>
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <log4cplus/layout.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/helpers/stringhelper.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/appender.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/helpers/loglog.h>
using namespace log4cplus;
using namespace log4cplus::helpers;
void TestOutput2Console(){
/* step 1: Instantiate an appender object */
SharedObjectPtr<Appender> _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> _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 FIRST log message...");
sleep(1);
LOG4CPLUS_WARN(_logger, "This is the SECOND log message...");
}
void TestOutput2log(){
/*step1:Instantiateanappenderobject*/
SharedAppenderPtr _append(new FileAppender("Test.log"));
_append->setName("filelogtest");
/*step4:Instantiatealoggerobject*/
Logger _logger = Logger::getInstance("test.subtestof_filelog");
/*step5:Attachtheappenderobjecttothelogger*/
_logger.addAppender(_append);
/*logactivity*/
for (int i = 0; i < 5; ++i) {
LOG4CPLUS_DEBUG(_logger, "Enteringloop#" << i << "Endline#");
}
}
void printMsgs(void)
{
std::cout << "Entering printMsgs()..." << std::endl;
LogLog::getLogLog()->debug("This is a Debug statement...");
LogLog::getLogLog()->warn("This is a Warning...");
LogLog::getLogLog()->error("This is a Error...");
std::cout << "Exiting printMsgs()..." << std::endl << std::endl;
}
void TestLogLog(){
printMsgs();
std::cout << "Turning on debug..." << std::endl;
LogLog::getLogLog()->setInternalDebugging(true);
printMsgs();
std::cout << "Turning on quiet mode..." << std::endl;
LogLog::getLogLog()->setQuietMode(true);
printMsgs();
}
int main()
{
//TestOutput2Console();
//TestOutput2log();
TestLogLog();
return 0;
}