log4cplus 分级显示控制

      前不久已经整理过一篇关于log4cplus介绍和使用基础的文章。这次来看看如何更好的使用log4cplus这个

功能强大的日志系统吧。

 

###  层次结构  ###

 

在log4cplus中,所有logger都通过一个层次化的结构(其实内部是hash表)来组织的。

有一个Root级别的logger,可以通过以下方法获取:

Logger root = Logger::getRoot();
   

用户定义的logger都有一个名字与之对应,比如:

Logger test = Logger::getInstance("test");
   
可以定义该logger的子logger:

Logger subTest = Logger::getInstance("test.subtest");

 

注意:

Root级别的logger只有通过getRoot方法获取,Logger::getInstance("root")获得的是它的子对象而

已。有了这些具有父子关系的logger之后可分别设置其LogLevel,比如:

 

root.setLogLevel( ... );
Test.setLogLevel( ... );
subTest.setLogLevel( ... );

 

这种父子会在接下来的优先级控制中体现出来。子类在不设置任何优先级的情况下会自动继承父类的优先级。

 

 

### 优先级划分 ###

 

log4cplus将输出的log信息按照LogLevel(从低到高)分为:

NOT_SET_LOG_LEVEL   (   -1) :接受缺省的LogLevel,如果有父logger则继承它的LogLevel
ALL_LOG_LEVEL     (    0) :开放所有log信息输出
TRACE_LOG_LEVEL   (    0) :开放trace信息输出(即ALL_LOG_LEVEL)
DEBUG_LOG_LEVEL (10000) :开放debug信息输出
INFO_LOG_LEVEL    (20000) :开放info信息输出
WARN_LOG_LEVEL    (30000) :开放warning信息输出
ERROR_LOG_LEVEL   (40000) :开放error信息输出
FATAL_LOG_LEVEL   (50000) :开放fatal信息输出
OFF_LOG_LEVEL     (60000) :关闭所有log信息输出

 

 

### 优先级控制 ###

 

LogLevelManager负责设置logger的优先级,各个logger可以通过setLogLevel设置自己的优先级,

某个logger的LogLevel设置成NOT_SET_LOG_LEVEL时,该logger会继承父logger的优先级,另外,
如果定义了重名的多个logger, 对其中任何一个的修改都会同时改变其它logger。

下面这个例子将通过设置不同优先级别可以实现对不同级别输出的控制。

 

#include "log4cplus/logger.h"
#include "log4cplus/consoleappender.h"
#include "log4cplus/loglevel.h"
#include <iostream>

 

using namespace std;
using namespace log4cplus;

 

//显示所有日志级别的信息

void ShowMsg(void)
{
    LOG4CPLUS_TRACE(Logger::getRoot(),"info");
    LOG4CPLUS_DEBUG(Logger::getRoot(),"info");
    LOG4CPLUS_INFO(Logger::getRoot(),"info");
    LOG4CPLUS_WARN(Logger::getRoot(),"info");
    LOG4CPLUS_ERROR(Logger::getRoot(),"info");
    LOG4CPLUS_FATAL(Logger::getRoot(),"info");
}


int main()
{

    //设置日志输出格式和输出对象
    SharedAppenderPtr _append(new ConsoleAppender());
    _append->setName("test");
    _append->setLayout(std::auto_ptr<log4cplus::Layout> (new TTCCLayout()));
    Logger root = Logger::getRoot();
    root.addAppender(_append);

 

    //分优先级输出日志信息
    cout << endl << "all-log allowed" << endl;
    root.setLogLevel(ALL_LOG_LEVEL);
    ShowMsg();
    cout << endl << "trace-log and above allowed" << endl;
    root.setLogLevel(TRACE_LOG_LEVEL);
    ShowMsg();
    cout << endl << "debug-log and above allowed" << endl;
    root.setLogLevel(DEBUG_LOG_LEVEL);
    ShowMsg();
    cout << endl << "info-log and above allowed" << endl;
    root.setLogLevel(INFO_LOG_LEVEL);
    ShowMsg();
    cout << endl << "warn-log and above allowed" << endl;
    root.setLogLevel(WARN_LOG_LEVEL);
    ShowMsg();
    cout << endl << "error-log and above allowed" << endl;
    root.setLogLevel(ERROR_LOG_LEVEL);
    ShowMsg();
    cout << endl << "fatal-log and above allowed" << endl;
    root.setLogLevel(FATAL_LOG_LEVEL);
    ShowMsg();
    cout << endl << "log disabled" << endl;
    root.setLogLevel(OFF_LOG_LEVEL);
    ShowMsg();
    return 0;
}

 

运行结果:
all-log allowed                                                                    
08-11-09 22:42:27,601 [6136] TRACE root <> - info         
08-11-09 22:42:27,617 [6136] DEBUG root <> - info         
08-11-09 22:42:27,617 [6136] INFO root <> - info           
08-11-09 22:42:27,632 [6136] WARN root <> - info         
08-11-09 22:42:27,632 [6136] ERROR root <> - info         
08-11-09 22:42:27,632 [6136] FATAL root <> - info          

trace-log and above allowed                                              
08-11-09 22:42:27,632 [6136] TRACE root <> - info         
08-11-09 22:42:27,632 [6136] DEBUG root <> - info         
08-11-09 22:42:27,632 [6136] INFO root <> - info           
08-11-09 22:42:27,648 [6136] WARN root <> - info         
08-11-09 22:42:27,648 [6136] ERROR root <> - info         
08-11-09 22:42:27,648 [6136] FATAL root <> - info         

debug-log and above allowed                                            
08-11-09 22:42:27,648 [6136] DEBUG root <> - info         
08-11-09 22:42:27,663 [6136] INFO root <> - info           
08-11-09 22:42:27,663 [6136] WARN root <> - info         
08-11-09 22:42:27,663 [6136] ERROR root <> - info         
08-11-09 22:42:27,663 [6136] FATAL root <> - info         

info-log and above allowed                                               
08-11-09 22:42:27,679 [6136] INFO root <> - info           
08-11-09 22:42:27,679 [6136] WARN root <> - info         
08-11-09 22:42:27,679 [6136] ERROR root <> - info         
08-11-09 22:42:27,695 [6136] FATAL root <> - info         

warn-log and above allowed                                              
08-11-09 22:42:27,695 [6136] WARN root <> - info         
08-11-09 22:42:27,710 [6136] ERROR root <> - info         
08-11-09 22:42:27,710 [6136] FATAL root <> - info         

error-log and above allowed                                              
08-11-09 22:42:27,710 [6136] ERROR root <> - info         
08-11-09 22:42:27,726 [6136] FATAL root <> - info         

fatal-log and above allowed                                              
08-11-09 22:42:27,726 [6136] FATAL root <> - info         

log disabled                                                                       
请按任意键继续. . .                                                                 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(layout)