一.簡介
    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();

}