关闭

log4cplus

标签: loggingnestedlayoutstring服务器存储
3023人阅读 评论(0) 收藏 举报
分类:

1,向文件中输出日志

#include <log4cplus/logger.h>

#include <log4cplus/fileappender.h>
#include <log4cplus/layout.h>
#include <log4cplus/ndc.h>
#include <log4cplus/helpers/loglog.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/configurator.h>


using namespace log4cplus;


const int LOOP_COUNT = 1000000;


int main()
{
    // RollingFileAppender and TTCCLayout
    SharedAppenderPtr append1(
        new RollingFileAppender(LOG4CPLUS_TEXT("test1.log"), 5*1024, 5)); // 系统要求的最低大小为200k,这里设置的是5k,系统会自动调整为200k
    append1->setName(LOG4CPLUS_TEXT("1"));
    append1->setLayout(std::auto_ptr<Layout>(new TTCCLayout()));


    // FileAppender and SimpleLayout
    SharedAppenderPtr append2(
        new FileAppender(LOG4CPLUS_TEXT("test2.log"))); // 文件大小无止境?
    append2->setName(LOG4CPLUS_TEXT("2"));
    append2->setLayout(std::auto_ptr<Layout>(new SimpleLayout()));


    /* DailyRollingFileAppender(不止是daily,也可以是
    MONTHLY(每月)、WEEKLY(每周)、DAILY(每日)、TWICE_DAILY(每两天)、
    HOURLY(每时)、MINUTELY(每分)) and PatternLayout。*/
    SharedAppenderPtr append3(
        new DailyRollingFileAppender(LOG4CPLUS_TEXT("test3.log"), DAILY, true, 10));
    append3->setName(LOG4CPLUS_TEXT("3"));


    std::string pattern = "[%D{%Y-%m-%d %H:%M:%S %q}] [%-5p] <%l %M> %m%n";
    append3->setLayout(std::auto_ptr<Layout>(new PatternLayout(pattern)));


    Logger root = Logger::getRoot();
    root.setLogLevel(ALL_LOG_LEVEL); // 输出所有级别的日志
    Logger logger1 = Logger::getInstance(LOG4CPLUS_TEXT("test1"));
    Logger logger2 = Logger::getInstance(LOG4CPLUS_TEXT("test2"));
    Logger logger3 = Logger::getInstance(LOG4CPLUS_TEXT("test3"));


    logger1.addAppender(append1);
    logger2.addAppender(append2);
    logger3.addAppender(append3);


    for (int i = 0; i < LOOP_COUNT; ++i) 
    {
        /*log4cplus中嵌入诊断上下文(Nested Diagnostic Context),即NDC。
        对log系统而言,
        当输入源可能不止一个,而只有一个输出时,往往需要分辩所有输出消息的来源,比如服务器处理来自不同
        客户端的消息时就需要作此判断,NDC可以为交错显示的信息打上一个标记(stamp), 使得辨认工作看起来
        容易些。这个标记是线程特有的,利用了线程局部存储机制,称为线程私有数据(Thread-specific
        Data,或TSD)。*/
        NDCContextCreator _context(LOG4CPLUS_TEXT("loop")); 
        
        LOG4CPLUS_DEBUG(logger1, "Entering loop #" << i);
        LOG4CPLUS_DEBUG(logger2, "Entering loop #" << i);
        LOG4CPLUS_DEBUG(logger3, "Entering loop #" << i);
    }


    // 从配置文件中获取logger、appender和layout
    PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("urconfig.properties"));
    Logger fileCat = Logger::getInstance(LOG4CPLUS_TEXT("filelogger"));


    for (int i = 0; i < LOOP_COUNT; ++i)
    {
        LOG4CPLUS_TRACE(fileCat, LOG4CPLUS_TEXT("This is a TRACE...") << i);
        LOG4CPLUS_DEBUG(fileCat, LOG4CPLUS_TEXT("This is a DEBUG...") << i);
        LOG4CPLUS_INFO(fileCat, LOG4CPLUS_TEXT("This is a INFO...") << i);
        LOG4CPLUS_WARN(fileCat, LOG4CPLUS_TEXT("This is a WARN...") << i);
        LOG4CPLUS_ERROR(fileCat, LOG4CPLUS_TEXT("This is a ERROR...") << i);
        LOG4CPLUS_FATAL(fileCat, LOG4CPLUS_TEXT("This is a FATAL...") << i);
    }


    /* 问题:DailyRollingFileAppender和RollingFileAppender不能同时使用,即一个appender不能既是
       DailyRollingFileAppender的又同时是RollingFileAppender的
    */


    return 0;
}


/*
g++ logdemo5.cpp -o demoxx -g -Wall -I /usr/local/include/ -L /usr/local/lib/ -llog4cplus
或者
g++ logdemo5.cpp -o demo1 -g -Wall -I /usr/local/include/ /usr/local/lib/liblog4cplus.a -lpthread

*/


// urconfig.properties文件

log4cplus.logger.filelogger=TRACE, append_1

log4cplus.appender.append_1=log4cplus::DailyRollingFileAppender
log4cplus.appender.append_1.Schedule=MINUTELY
log4cplus.appender.append_1.File=./log/test.log #目录要手动创建!
#log4cplus.appender.append_1.MaxFileSize=512000 #无效果
#log4cplus.appender.append_1.MaxBackupIndex=5   #貌似也无效果

log4cplus.appender.append_1.layout=log4cplus::PatternLayout
log4cplus.appender.append_1.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S %q}] [%-5p] <%l %M> %m%n


2,性能数据:



void performanceTest()
{
    #define LOOP_COUNT 100000
    PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT("log4cplus.properties"));
    Logger root = Logger::getRoot();
    Logger logger = Logger::getInstance(LOG4CPLUS_TEXT("testlogger"));
    
    LOG4CPLUS_WARN(Logger::getRoot (), "Starting test loop....");
    Time start = Time::gettimeofday();
    tstring msg(LOG4CPLUS_TEXT("This is a WARNING..."));
    
    int i = 0;
    for (i = 0; i < LOOP_COUNT; ++i)
    {
        LOG4CPLUS_WARN(logger, msg);
    }
    
    Time end = Time::gettimeofday();
    Time diff = end - start;
    
    LOG4CPLUS_WARN(LOG4CPLUS_TEXT("root"), "Logging " << LOOP_COUNT << " took: " << diff);
    LOG4CPLUS_WARN(root, "Logging average: " << (diff / LOOP_COUNT));
}


#log4cplus.properties配置文件
log4cplus.rootLogger=TRACE, STDOUT 
log4cplus.logger.testlogger=TRACE, TEST
log4cplus.additivity.testlogger=FALSE


log4cplus.appender.STDOUT=log4cplus::ConsoleAppender
log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout
log4cplus.appender.STDOUT.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} [%t] %-5p %c - %m %n


log4cplus.appender.TEST=log4cplus::FileAppender
log4cplus.appender.TEST.File=test_output.log
log4cplus.appender.TEST.layout=log4cplus::PatternLayout
log4cplus.appender.TEST.layout.ConversionPattern=%d{%y-%m-%d %H:%M:%S,%q} [%t] %-5p %c <%x> - %m%n


// 程序输出
03/28/12 01:58:35 [3078437120] WARN  root - Starting test loop.... 
03/28/12 01:58:38 [3078437120] WARN  root - Logging 100000 took: 2sec 459840usec
03/28/12 01:58:38 [3078437120] WARN  root - Logging average: 0sec 24usec -- 向文件中打印每条日志平均需要24微妙


test_output.log日志文件的内容:
12-03-28 01:57:29,677 [3078301952] WARN  testlogger <> - This is a WARNING...
...


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:391055次
    • 积分:4388
    • 等级:
    • 排名:第6821名
    • 原创:109篇
    • 转载:69篇
    • 译文:0篇
    • 评论:19条
    文章分类
    最新评论