您的位置:首页 > 其它

windows客户端开发--为你的客户端增加日志系统(log4cplus)

2016-04-07 23:56 483 查看
客户端开发到一定阶段,不得不提上日程的就是日志系统了。

今天就先跟大家介绍一个简单的,用于C++的日志系统库:log4cplus

https://sourceforge.net/p/log4cplus/wiki/Home/

Summary

log4cplus is a simple to use C++ logging API providing thread-safe, flexible, and arbitrarily granular control over log management and configuration. It is modelled after the Java log4j API.

This project is now in “Production” status. See project status page.

Please feel free to contact me if you have any questions or if you would like to help the project: wilx@users.sourceforge.net.

If you want to get help with log4cplus use or compilation or if you have any other problem with it, please use the tracker or the log4cplus-devel mailing list. For bug reporting instructions see bug reporting instructions.

If you want to contribute changes to log4cplus, please see the Development wiki page for more information.

Log4cplus is the log system written in C++ source, formerly known as log4j system written in Java, the Apache Software License is the author of Tad E. Smith protection.

Log4cplus has a thread safe, flexible, and multi granularity control features, the log partition priority so that it can be for program debugging, testing, operation, and maintenance of the whole life cycle. You can select the log output to the screen, file, NT event log, or even a remote server by specifying a policy; on the log backup etc.

2 installation methods

使用步骤:

1 instantiates an appender object encapsulates the output medium,

2 instantiates a package of the output format of the layout object,

3 bind a layout object (attach) to the appender object; such as omit this step, simple layout for SimpleLayout (see Section 5.1) bound to the logger object.

4 instantiates a package of the log output logger object, and call its static function getInstance () for example, log4cplus::Logger::getInstance(“logger_name”),

5 bind a appender object (attach) to the logger object,

6 set the priority of logger, such as omit this step, all kinds of finite order log will be output.

例子:

/*
*Standards, strict implementation of 1-6.
 */
#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<Appender> _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> _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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: