Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4208822
  • 博文数量: 176
  • 博客积分: 10059
  • 博客等级: 上将
  • 技术积分: 4681
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-24 12:27
文章分类

全部博文(176)

文章存档

2012年(1)

2011年(4)

2010年(14)

2009年(71)

2008年(103)

分类: C/C++

2009-06-11 16:26:17

参考例子:


下面给出详细的代码:

//bar.h
#include "log4cxx/logger.h"

namespace com {
   namespace foo {
      class Bar {
          static log4cxx::LoggerPtr logger;
          
      public:
          void doIt();
       };
    }
}

//bar.cpp
#include "bar.h"

using namespace com::foo;
using namespace log4cxx;

LoggerPtr Bar::logger(Logger::getLogger("bar"));

void Bar::doIt() {
   LOG4CXX_DEBUG(logger, "Did it again!");
}

//main.cpp
#include "bar.h"
using namespace com::foo;

// include log4cxx header files.
#include "log4cxx/logger.h"
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"

using namespace log4cxx;
using namespace log4cxx::helpers;

LoggerPtr logger(Logger::getLogger("MyApp"));

int main(int argc, char **argv)
{
        int result = EXIT_SUCCESS;
        try
        {
                // Set up a simple configuration that logs on the console.
                BasicConfigurator::configure();

                LOG4CXX_INFO(logger, "Entering application.");
                Bar bar;
                bar.doIt();
                LOG4CXX_INFO(logger, "Exiting application.");
        }
        catch(Exception&)
        {
                result = EXIT_FAILURE;
        }

        return result;
}

共有三个文件,bar.h bar.cpp main.cpp 。

编译:g++ main.cpp bar.cpp -llog4cxx -I /home/weizili/project/server/debug/include -L /home/weizili/project/server/debug/lib

 

阅读(3220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~