Log4cpp的主页为:http://sourceforge.net/projects/log4cpp/
下载log4cpp-1.1.1. tar.gz tar zxvf log4cpp-1.1.1 tar.gz -C /usr/local 解压到该目下
安装:打开终端,在/usr/local/log4cpp/
输入以下命令,
./configure
make
make check
make install
这里已经安装成功,默认的路径lib库路径是/usr/local/lib/,
头文件的位置:/usr/local/include/log4cpp
将/usr/local/lib下的和log4cpp相关的库都拷贝到/usr/lib,
强调:使用log4cpp时,要加上# g++ log4test.cpp -llog4cpp -lpthread,才能顺利的编译通过
Makefile中包含静态库或-llog4cpp动态库
以管理员身份登录,将自己的库所在路径/usr/local/lib添加
到/etc/ld.so.conf文件中。但一般这个文件中的内容都是一句“include /etc/ld.so.conf.d/*.conf”,因此最好在/etc/目录下新建一个文件 , 如“xxx.conf”,然后将自己的库所在路径添加
到这个conf文件中。
测试文档下:
修改完文件后,以root身份运行 /sbin/ldconfig -v 。这一步必不可少,别丢了。
测试文档下:
</pre><pre name="code" class="cpp">#include <stdio.h>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/SimpleLayout.hh>
#define LOGFILE "./test.log"
int main() {
/*Setting up Appender, layout and Category*/
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",LOGFILE);//第一个参数是appender的名字,第二个是log文件的名字
log4cpp::Layout *layout = new log4cpp::SimpleLayout();
//log4cpp::Layout *layout = new log4cpp::BasicLayout();
log4cpp::Category& category = log4cpp::Category::getInstance("abc");
appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp::Priority::INFO);
/*The actual logging*/
category.info("This is for tracing the flow");
category.notice("This is to notify certain events");
category.warn("This is to generate certain warnings");