log4cplus是C++编写的开源的日志系统,功能非常全面,用到自己开发的工程中会比较专业的,:),本文介绍了log4cplus基本概念,以及如何安装,配置。
### 简介 ###
log4cplus是C++编写的开源的日志系统,前身是Java编写的log4j系统.受Apache Software License保护。作者是Tad E. Smith。log4cplus具有线程安全、灵活、以及多粒度控制的特点,通过将信息划分优先级使其可以面向程序调试、运行、测试、和维护等全生命周期; 你可以选择将信息输出到屏幕、文件、NT event log、甚至是远程服务器;通过指定策略对日志进行定期备份等等。
### 下载 ###
最新的log4cplus可以从以下网址下载 http://sourceforge.NET/projects/log4cplus/
选择生成 生成解决方案
2. windows下的配置
打开MSVC10文件夹
vs2010打开 log4cplus.sln
编译选项 选择 Debug win32_unicode
文件夹log4cplus-1.2.0-rc3\msvc10\Win32下
就会多出bin.Debug_Unicode
将编译选项Debug 改成Release就生成release版本
在这个文件夹下
有log4cplusSUD.lib
log4cplusUD.dll
log4cplusUD.lib
将其复制到log4文件夹根目录下新建文件夹lib
4。在环境配置完成后,我们可以对具体的项目环境进行配置。
log4cplus
包含目录 D:\CPP\log4cplus-1.2.0-rc3\include
库目录D:\CPP\log4cplus-1.2.0-rc3\lib
连接器
log4cplusSUD.lib
log4cplusUD.lib
我们创建一个控制台项目,并进行如下配置:
1。采用静态链接库:
右键项目->属性->
加入log4cplusSD.lib
注意:才用静态链接库,可以删除bin目录下的log4cplusD.dll 以及lib目录下的log4cplusD.lib 2个文件。
2。采用动态链接库:
右键项目->属性->
注意:才用动态链接库,需要bin目录下的log4cplusD.dll 以及lib目录下的log4cplusD.lib 2个文件。可以删除lib目录下的log4cplusSD.lib 文件
5.编写简单程序并测试
- #include "stdafx.h"
-
- #include <log4cplus/loggingmacros.h>
- #include <log4cplus/configurator.h>
- #include <iomanip>
-
- using namespace log4cplus;
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- BasicConfigurator config;
- config.configure();
-
- Logger logger = Logger::getInstance("main");
- LOG4CPLUS_WARN(logger, "Hello, World!");
- getchar();
- return 0;
- }
此处
- #include <log4cplus/log.h>头文件有误
注意:
log4cplus官方源码采用的“多字节字符集”,如果你项目采用“使用 Unicode 字符集”则可以将log4cplus字符集修改后重新生成对应的库文件!
以下为常用的封装好的测试实例:
VLog.h
- #pragma once
- #ifndef VLOG_H
- #define VLOG_H
-
- #include <log4cplus/logger.h>
- #include <log4cplus/layout.h>
- #include <log4cplus/loglevel.h>
- #include <log4cplus/fileappender.h>
- #include <log4cplus/consoleappender.h>
-
- #include <log4cplus/helpers/loglog.h>
-
- using namespace log4cplus;
- using namespace helpers;
-
-
-
-
-
-
-
-
-
- void InitLog4cplus(
- const wchar_t* logfile,
- const bool console = true,
- const bool bebug = true,
- LogLevel lv = DEBUG_LOG_LEVEL,
- const wchar_t* format = L"%d : %m%n"
- );
-
-
-
-
-
- Logger GetRootLogger(void);
-
-
-
-
-
-
- Logger GetSubLogger(const wchar_t* sub);
-
-
-
-
- void ShutdownLogger(void);
-
- #endif /* VLOG_H */
VLog.cpp
-
-
-
-
-
- #include "VLog.h"
-
- void InitLog4cplus(const wchar_t* logfile, const bool console, const bool bebug,
- LogLevel lv, const wchar_t* format)
-
-
-
-
-
-
-
-
-
-
- {
-
- LogLog::getLogLog()->setInternalDebugging(bebug);
-
-
-
- SharedAppenderPtr pappender1(new RollingFileAppender(logfile, 1024 * 1024 * 200, 50, false));
-
-
-
-
- std::auto_ptr<Layout> playout1(new PatternLayout(format));
-
-
-
- pappender1->setLayout(playout1);
-
-
- Logger rootLogger = Logger::getRoot();
-
-
- rootLogger.addAppender(pappender1);
-
- if (console) {
-
- SharedAppenderPtr pappender2(new ConsoleAppender(false, false));
- std::auto_ptr<Layout> playout2(new PatternLayout(format));
- pappender2->setLayout(playout2);
- rootLogger.addAppender(pappender2);
- }
-
-
- rootLogger.setLogLevel(lv);
- }
-
- Logger GetRootLogger(void) {
- return Logger::getRoot();
- }
-
- Logger GetSubLogger(const wchar_t* sub) {
- return Logger::getInstance(sub);
- }
-
- void ShutdownLogger(void) {
- Logger::getRoot().shutdown();
- }
Main.cpp
- <p>#include "VLog.h"</p><p>int _tmain(int argc, _TCHAR* argv[])
- {
-
-
- InitLog4cplus(L"main.log");</p><p>
- log4cplus::Logger test1 = GetSubLogger(L"test1");
-
- log4cplus::Logger test1_01 = GetSubLogger(L"test1.01");</p><p>
- LOG4CPLUS_DEBUG(test1_01, "you can log like this :" << "hello " << 100 << '!');</p><p>
- ShutdownLogger();
- }
- </p>