一.简介
log4cplus是一个日志记录的库,目的很简单,就是把合适的信息送到正确的位置上去。在服务器程序上使用非常方便。
开发库下载地址可以去baidu搜一下,是开源的哦!
二.组成
Log4cplus 由4部分组成:
(1) Logger 日志模块,程序中唯一一个必须得使用的模块,解决了在哪里使用日志的问题
(2) Appenders 接收日志的各个设备,如控制台、文件、网络等。解决了输出到哪里去的问题
(3)Layout 格式化输出信息,解决了如何输出的问题。
(4)Filter 过滤器,解决哪些信息需要输出的问题,比如DEBUG,WARR,INFO等的输出控制.
Log4cplus的主要部件关系图如下:
三.一些简单示例
(1) 各对象可以看下上图理解一下,以下为 严格实现步骤1-6
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <iostream>
#include <log4cplus/consoleappender.h>
#include <log4cplus/layout.h>
#include <conio.h>
#include <log4cplus/helpers/sleep.h>
using namespace log4cplus;
using namespace log4cplus::helpers;
int _tmain(int argc, _TCHAR* argv[]){
/**//* step 1: Instantiate an appender object */
SharedObjectPtr<Appender> _append (new ConsoleAppender());
_append->setName(LOG4CPLUS_TEXT("append for test"));
/**//* step 2: Instantiate a layout object */
//std::string pattern = ;
std::auto_ptr<Layout> _layout(new PatternLayout(LOG4CPLUS_TEXT("%d{%m/%d/%y %H:%M:%S} - %m [%l]%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("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");
_getch();
return 0;
}
(2) 简洁使用模式,appender输出到屏幕
#include "stdafx.h"
#pragma comment(lib,"../bin/log4cplusD.lib")
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <iostream>
#include <log4cplus/consoleappender.h>
#include <log4cplus/layout.h>
#include <conio.h>
#include <log4cplus/helpers/sleep.h>
using namespace log4cplus;
using namespace log4cplus::helpers;
int _tmain(int argc, _TCHAR* argv[]){
/**//* step 1: Instantiate an appender object */
SharedAppenderPtr _append(new ConsoleAppender());
_append->setName(LOG4CPLUS_TEXT("append test"));
/**//* step 4: Instantiate a logger object */
Logger _logger = Logger::getInstance(LOG4CPLUS_TEXT("test"));
/**//* step 5: Attach the appender object to the logger */
_logger.addAppender(_append);
/**//* log activity */
LOG4CPLUS_DEBUG(_logger, "This is the FIRST log message");
sleep(1);
LOG4CPLUS_WARN(_logger, "This is the SECOND log message");
_getch();
return 0;
}
(3)简洁使用模式,appender输出到屏幕
#include "stdafx.h"
#pragma comment(lib,"../bin/log4cplusD.lib")
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <iostream>
#include <log4cplus/consoleappender.h>
#include <log4cplus/layout.h>
#include <conio.h>
#include <log4cplus/helpers/sleep.h>
using namespace log4cplus;
using namespace log4cplus::helpers;
using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
/**//* step 1: Instantiate an appender object */
SharedAppenderPtr _append(new ConsoleAppender());
_append->setName(LOG4CPLUS_TEXT("append test"));
/**//* step 4: Instantiate a logger object */
Logger _logger = Logger::getInstance(LOG4CPLUS_TEXT("test"));
/**//* step 5: Attach the appender object to the logger */
_logger.addAppender(_append);
/**//* log activity */
LOG4CPLUS_TRACE(_logger, "This is" << " just a t" << "est." << std::endl) ; //不会被打印
LOG4CPLUS_DEBUG(_logger, "This is a bool: " << true);
LOG4CPLUS_INFO(_logger, "This is a char: " << 'x') ;
LOG4CPLUS_WARN(_logger, "This is a int: " << 1000) ;
LOG4CPLUS_ERROR(_logger, "This is a long(hex): " << std::hex << 100000000) ;
LOG4CPLUS_FATAL(_logger, "This is a double: " << 1.2345234234);
_getch();
return 0;
}
(4)通过loglog来控制输出调试、警告或错误信息,appender输出到屏幕。
#include "stdafx.h"
#pragma comment(lib,"../bin/log4cplusD.lib")
#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>
#include <iostream>
#include <log4cplus/consoleappender.h>
#include <log4cplus/layout.h>
#include <conio.h>
#include <log4cplus/helpers/sleep.h>
#include <log4cplus/helpers/loglog.h>
using namespace log4cplus;
using namespace log4cplus::helpers;
using namespace std;
void printMsgs(void){
std::cout << "Entering printMsgs()" << std::endl;
LogLog::getLogLog()->debug(LOG4CPLUS_TEXT("This is a Debug statement"));
LogLog::getLogLog()->warn(LOG4CPLUS_TEXT("This is a Warning"));
LogLog::getLogLog()->error(LOG4CPLUS_TEXT("This is a Error"));
std::cout << "Exiting printMsgs()" << std::endl << std::endl;
}
int _tmain(int argc, _TCHAR* argv[]){
/**//*
LogLog类实现了debug, warn, error 函数用于输出调试、警告或错误信息,
同时提供了两个方法来进一步控制所输出的信息,其中:
setInternalDebugging方法用来控制是否屏蔽输出信息中的调试信息,当输
入参数为false则屏蔽,缺省设置为 false。
setQuietMode方法用来控制是否屏蔽所有输出信息,当输入参数为true则屏蔽,
缺省设置为false。
LogLog::getLogLog()->setInternalDebugging(false);
*/
//默认级别是不打印debug信息
printMsgs();
std::cout << "Turning on debug" << std::endl;
LogLog::getLogLog()->setInternalDebugging(true);
//打开debug方式
//全部打印
printMsgs();
std::cout << "Turning on quiet mode" << std::endl;
LogLog::getLogLog()->setQuietMode(true);
//屏蔽所有输出信息,所以不全打印各种级别信息
printMsgs();
_getch();
return 0;
}
/**//*输入结果如下:
Entering printMsgs()
log4cplus:WARN This is a Warning
log4cplus:ERROR This is a Error
Exiting printMsgs()
Turning on debug
Entering printMsgs()
log4cplus: This is a Debug statement
log4cplus:WARN This is a Warning
log4cplus:ERROR This is a Error
Exiting printMsgs()
Turning on quiet mode
Entering printMsgs()
Exiting printMsgs()
都有个log4cplus:打头,可以自定义修改以下部分代码
LogLog::LogLog() : mutex(LOG4CPLUS_MUTEX_CREATE), debugEnabled(false),
quietMode(false), PREFIX( LOG4CPLUS_TEXT("log4cplus: ") ),
WARN_PREFIX( LOG4CPLUS_TEXT("log4cplus:WARN ") ),
ERR_PREFIX( LOG4CPLUS_TEXT("log4cplus:ERROR ") ){}
*/
(5)文件模式,appender输出到文件
#include "stdafx.h"
#pragma comment(lib,"../bin/log4cplusD.lib")
#include <log4cplus/logger.h>
#include <log4cplus/fileappender.h>
#include <conio.h>
using namespace log4cplus;
int _tmain(int argc, _TCHAR* argv[]){
/**//* step 1: Instantiate an appender object */
SharedAppenderPtr _append(new FileAppender(LOG4CPLUS_TEXT("Test.log")));
_append->setName(LOG4CPLUS_TEXT("file log test"));
/**//* step 4: Instantiate a logger object */
Logger _logger = Logger::getInstance(LOG4CPLUS_TEXT("test.subtestof_filelog"));
/**//* step 5: Attach the appender object to the logger */
_logger.addAppender(_append);
/**//* log activity */
int i;
for( i = 0; i < 5; ++i )
{
LOG4CPLUS_DEBUG(_logger, "Entering loop #" << i << "End line #");
}
_getch();
}