首先下载log4plus的压缩包,http://sourceforge.net/projects/log4cplus/files/log4cplus-stable/1.0.4/log4cplus-1.0.4.tar.bz2/download
接着在ubuntu中解压,根据里面的安装提示
运行下面命令
./configure --prefix=/where/to/install
make
make install
默认的路径lib库路径是/usr/local/lib/,头文件的位置:/usr/local/include/log4cplus
接着在拷贝头文件到/usr/include,或者建立链接
sudo ln -s /usr/local/include/log4cplus /usr/include
接着在eclipse的项目属性中添加库以及库的查找路径
库里面添加:log4cplus,rt,pthread
库查找路径添加:/usr/local/lib
最后测试代码:
#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 2: Instantiate a logger object */
Logger _logger = Logger::getInstance("test.subtestof_filelog");
/* step 3: 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;
}
运行结果:
工程中增加了一个Test.log文件,内容如下
DEBUG - Entering loop #0End line #
DEBUG - Entering loop #1End line #
DEBUG - Entering loop #2End line #
DEBUG - Entering loop #3End line #
DEBUG - Entering loop #4End line #