Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
输出到控制台
#include <iostream>
#include 
<log4cplus/helpers/loglog.h>

using namespace std;
using namespace log4cplus::helpers;

void print_message() 
{
    cout 
<< "Entering print_message()" << 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"));
    cout 
<< "Exiting print_message()" << endl << endl;
}

int main() 
{
    print_message();

    cout 
<< "Turning on debug" << endl;
    LogLog::getLogLog()
->setInternalDebugging(true);
    print_message();

    cout 
<< "Turning on quiet mode" << endl;
    LogLog::getLogLog()
->setQuietMode(true);
    print_message();
    system(
"pause");
    
return 0;
}
另外一个例子:
#include "log4cplus/logger.h"
#include 
"log4cplus/consoleappender.h"
#include 
"log4cplus/loglevel.h"
#include 
<iomanip>

using namespace std;
using namespace log4cplus;

int main()
{
    SharedAppenderPtr append_1(
new ConsoleAppender());
    append_1
->setName(LOG4CPLUS_TEXT("First"));
    Logger::getRoot().addAppender(append_1);

    Logger root 
= Logger::getRoot();
    Logger test 
= Logger::getInstance(LOG4CPLUS_TEXT("test"));

    LOG4CPLUS_DEBUG(root,
                    
"This is"
                    
<< " a reall"
                    
<< "y long message." << endl
                    
<< "Just testing it out" << endl
                    
<< "What do you think?");
    test.setLogLevel(NOT_SET_LOG_LEVEL);
    LOG4CPLUS_DEBUG(test, 
"This is a bool: " << true);
    LOG4CPLUS_INFO(test, 
"This is a char: " << 'x');
    LOG4CPLUS_INFO(test, 
"This is a short: " << (short)-100);
    LOG4CPLUS_INFO(test, 
"This is a unsigned short: " << (unsigned short)100);
    LOG4CPLUS_INFO(test, 
"This is a int: " << (int)1000);
    LOG4CPLUS_INFO(test, 
"This is a unsigned int: " << (unsigned int)1000);
    LOG4CPLUS_INFO(test, 
"This is a long(hex): " << hex << (long)100000000);
    LOG4CPLUS_INFO(test, 
"This is a unsigned long: " << (unsigned long)100000000);
    LOG4CPLUS_WARN(test, 
"This is a float: " << (float)1.2345);
    LOG4CPLUS_ERROR(test, 
                    
"This is a double: " 
                    
<< setprecision(15
                    
<< (double)1.2345234234);
    LOG4CPLUS_FATAL(test, 
                    
"This is a long double: " 
                    
<< setprecision(15
                    
<< (long double)123452342342.342);
    system(
"pause");
    
return 0;
}
再来看几个输出到日志的小例子:
#include <log4cplus/logger.h>
#include 
<log4cplus/fileappender.h>
#include 
<log4cplus/layout.h>
#include 
<log4cplus/ndc.h>
#include 
<log4cplus/helpers/loglog.h>

using namespace log4cplus;

const int LOOP_COUNT = 20000;

int main()
{
    helpers::LogLog::getLogLog()
->setInternalDebugging(true);
    SharedAppenderPtr append_1(
new RollingFileAppender(LOG4CPLUS_TEXT("Test.log"),5*1024,5));
    append_1
->setName(LOG4CPLUS_TEXT("LOG4PLUS"));
    append_1
->setLayout(std::auto_ptr<Layout>(new TTCCLayout()));
    Logger::getRoot().addAppender(append_1);

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

    
for(int i=0; i<LOOP_COUNT; ++i) 
    {
        NDCContextCreator _context(LOG4CPLUS_TEXT(
"loop"));
        LOG4CPLUS_DEBUG(subTest, 
"Entering loop #" << i);
    }

    
return 0
}

几点说明:
1.setInternalDebuging 是启用内部调试日志,不过具体作用不是很明确
2.Appender是实现日志输出的策略描述
一般情况下,参考以上例子就足够了
posted on 2012-03-15 22:22 ccsdu2009 阅读(786) 评论(1)  编辑 收藏 引用 所属分类: 软件工程
Comments
  • # re: 使用log4plus
    cpper
    Posted @ 2012-03-17 13:55
    #include <log4cplus/logger.h>
    #include <log4cplus/fileappender.h>
    #include <log4cplus/layout.h>
    #include <log4cplus/ndc.h>
    #include <log4cplus/helpers/loglog.h>

    const int LOOP_COUNT = 20000;

    int main()
    {
    log4cplus::helpers::LogLog::getLogLog()->setInternalDebugging(false);
    log4cplus::SharedAppenderPtr append(new log4cplus::FileAppender(LOG4CPLUS_TEXT("emc.log")));
    append->setName(LOG4CPLUS_TEXT("LOG4PLUS"));
    append->setLayout(std::auto_ptr<log4cplus::Layout>(new log4cplus::TTCCLayout()));
    log4cplus::Logger::getRoot().addAppender(append);

    log4cplus::Logger root = log4cplus::Logger::getRoot();
    log4cplus::Logger test = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("emc"));

    for(int i=0; i<LOOP_COUNT; ++i)
    {
    log4cplus::NDCContextCreator logcontext(LOG4CPLUS_TEXT("loop"));
    LOG4CPLUS_DEBUG(test,"Entering loop #" << i);
    }

    return 0;
    }  回复  更多评论   

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理