参考例子:
下面给出详细的代码:
//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
阅读(3260) | 评论(0) | 转发(0) |