转自 http://www.cppblog.com/tx7do/articles/11716.html
本文介绍了使用log4cplus有六个步骤,并提供了一些例子引导你了解log4cplus的基本使用。
### 基本使用 ###
使用log4cplus有六个基本步骤:
1. 实例化一个appender对象
2. 实例化一个layout对象
3. 将layout对象绑定(attach)到appender对象
4. 实例化一个logger对象,调用静态函数:log4cplus::Logger::getInstance("logger_name")
5. 将appender对象绑定(attach)到logger对象,如省略此步骤,标准输出(屏幕)appender对象会绑定到logger
6. 设置logger的优先级,如省略此步骤,各种有限级的消息都将被记录
下面通过一些例子来了解log4cplus的基本使用。
〖例1〗
/* 严格实现步骤1-6,appender输出到屏幕, 其中的布局格式和LogLevel后面会详细解释。*/ #include <log4cplus/logger.h> #include <log4cplus/consoleappender.h> #include <log4cplus/layout.h> using namespace log4cplus; using namespace log4cplus::helpers; int main() { /* step 1: Instantiate an appender object */ SharedObjectPtr _append (new ConsoleAppender()); _append->setName("append for test"); /* step 2: Instantiate a layout object */ std::string pattern = "%d{%m/%d/%y %H:%M:%S} - %m [%l]%n"; std::auto_ptr _layout(new PatternLayout(pattern)); /* step 3: Attach the layout object to the appender */ _append->setLayout( _layout ); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* step 6: Set a priority for the logger */ _logger.setLogLevel(ALL_LOG_LEVEL); /* log activity */ LOG4CPLUS_DEBUG(_logger, "This is the FIRST log message") sleep(1); LOG4CPLUS_WARN(_logger, "This is the SECOND log message") return 0; }
/* 简洁使用模式,appender输出到屏幕。*/ #include <log4cplus/logger.h> #include <log4cplus/consoleappender.h> using namespace log4cplus; using namespace log4cplus::helpers; int main() { /* step 1: Instantiate an appender object */ SharedAppenderPtr _append(new ConsoleAppender()); _append->setName("append test"); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* log activity */ LOG4CPLUS_DEBUG(_logger, "This is the FIRST log message") sleep(1); LOG4CPLUS_WARN(_logger, "This is the SECOND log message") return 0; }
/* iostream模式,appender输出到屏幕。*/ #include <log4cplus/logger.h> #include <log4cplus/consoleappender.h> #include <iomanip> /* 其实这个东东还是放到log4cplus头文件中比较合适些,个人意见:) */ using namespace log4cplus; int main() { /* step 1: Instantiate an appender object */ SharedAppenderPtr _append(new ConsoleAppender()); _append->setName("append test"); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* log activity */ LOG4CPLUS_TRACE(_logger, "This is" << " just a t" << "est." << std::endl) LOG4CPLUS_DEBUG(_logger, "This is a bool: " << true) LOG4CPLUS_INFO(_logger, "This is a char: " << 'x') LOG4CPLUS_WARN(_logger, "This is a int: " << 1000) LOG4CPLUS_ERROR(_logger, "This is a long(hex): " << std::hex << 100000000) LOG4CPLUS_FATAL(_logger, "This is a double: " << std::setprecision(15) << 1.2345234234) return 0; }
/* 调试模式,通过loglog来控制输出调试、警告或错误信息,appender输出到屏幕。*/ #include <iostream> #include <log4cplus/helpers/loglog.h> using namespace log4cplus::helpers; void printMsgs(void) { std::cout << "Entering printMsgs()" << std::endl; LogLog::getLogLog()->debug("This is a Debug statement"); LogLog::getLogLog()->warn("This is a Warning"); LogLog::getLogLog()->error("This is a Error"); std::cout << "Exiting printMsgs()" << std::endl << std::endl; } int main() { /* LogLog类实现了debug, warn, error 函数用于输出调试、警告或错误信息, 同时提供了两个方法来进一步控制所输出的信息,其中: setInternalDebugging方法用来控制是否屏蔽输出信息中的调试信息,当输入参数为false则屏蔽,缺省设置为false。 setQuietMode方法用来控制是否屏蔽所有输出信息,当输入参数为true则屏蔽, 缺省设置为false。 LogLog::getLogLog()->setInternalDebugging(false); */ printMsgs(); std::cout << "Turning on debug" << std::endl; LogLog::getLogLog()->setInternalDebugging(true); printMsgs(); std::cout << "Turning on quiet mode" << std::endl; LogLog::getLogLog()->setQuietMode(true); printMsgs(); return 0; }
LogLog::LogLog() : mutex(LOG4CPLUS_MUTEX_CREATE), debugEnabled(false), quietMode(false), PREFIX( LOG4CPLUS_TEXT("log4cplus: ") ), WARN_PREFIX( LOG4CPLUS_TEXT"log4cplus:WARN ") ), ERR_PREFIX( LOG4CPLUS_TEXT"log4cplus:ERROR ") ){}
/* 文件模式,appender输出到文件。*/ #include <log4cplus/logger.h> #include <log4cplus/fileappender.h> using namespace log4cplus; int main() { /* step 1: Instantiate an appender object */ SharedAppenderPtr _append(new FileAppender("Test.log")); _append->setName("file log test"); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test.subtestof_filelog"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* log activity */ int i; for( i = 0; i < 5; ++i ) { LOG4CPLUS_DEBUG(_logger, "Entering loop #" << i << "End line #") } return 0; }