log4cxx是Java社区著名的log4j的c++移植版,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计,log4cxx是apache软件基金会的开源项目,基于APR实现跨平台支持。一个良好的日志系统不管是开发、调试和维护,对一个项目来说是多么的重要,类似的日志框架还有GLog、boost log。
依赖:apr、apr-util
apr、apr-util下载地址:
http://apr.apache.org/download.cgi
log4cxx下载地址:
http://logging.apache.org/log4cxx/download.html
二、下载与安装
环境:linux
依赖:apr、apr-util
apr、apr-util下载地址:
http://apr.apache.org/download.cgi
log4cxx下载地址:
http://logging.apache.org/log4cxx/download.html
安装步骤:
1.由于log4cxx是依赖于apr的,因此先安装apr和apr-util这两个库,我会将这两个库安装到同一个目录下;
下载完之后,当前目录有
apr-1.4.6.tar.gz和
apr-util-1.5.1.tar.gz两个文件。
- $>tar xvf apr-1.4.6.tar.gz
- $>cd apr-1.4.6
- $>./configuer --prefix=/usr/local/apr
- $>make
- $>make install
目前apr安装完成,会在/usr/local下产生apr目录
- $>tar xvf apr-util-1.5.1.tar.gz
- $>cd apr-util-1.5.1
- $>./configure --prefix=/usr/local/apr --with-apr=/usr/local/apr
- $>make
- $>make install
apr-util也会安装/usr/local/apr目录下
2.开始安装log4cxx,下载完之后,当前目录下有apache-log4cxx-0.10.0.tar.gz。
- $>tar xvf apache-log4cxx-0.10.0.tar.gz
- $>cd apache-log4cxx-0.10.0
- $>./configure --prefix=/usr/local/log4cxx --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr --with-charset=utf-8 --with-logchar=utf-8
- $>make
这时候会出现这样的错误
- inputstreamreader.cpp:66: error: 'memmove' was not declared in this scope
- make[3]: *** [inputstreamreader.lo] 错误 1
在网上找了相关资料,原来有几个.cpp文件缺少了一些引用头文件,添加上去即可。
- src/main/cpp/inputstreamreader.cpp添加#include <string.h>
- src/main/cpp/socketoutputstream.cpp添加#include <string.h>
- src/examples/cpp/console.cpp添加#include <string.h>;#include <stdio.h>;
这些文件修改完之后,执行make和make install就可安装成功。
三、测试
主函数代码:main.cpp
- #include <log4cxx/logger.h>
- #include <log4cxx/basicconfigurator.h>
- #include <log4cxx/propertyconfigurator.h>
- #include <log4cxx/helpers/exception.h>
- #include <iostream>
- int main()
- {
- log4cxx::PropertyConfigurator::configureAndWatch("log4cxx.properties");
- log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("lib"));
- LOG4_DEBUG(logger, "this is log4cxx test");
- return 0;
- }
配置文件:log4cxx.properties
- # 设置root logger为DEBUG级别,使用了ca和fa两个Appender
- log4j.rootLogger=DEBUG,lib
- #对Appender lib进行设置:
- # 这是一个文件类型的Appender,
- # 其输出文件(File)为./lib.log,
- # 输出方式(Append)为覆盖方式,
- # 输出格式(layout)为PatternLayout
- log4j.appender.lib=org.apache.log4j.ConsoleAppender
- log4j.appender.lib.Threshold=DEBUG
- #log4j.appender.lib.DatePattern='log/'yyyy-MM-dd'_SysService.log'
- log4j.appender.lib.File=./log/output.log
- log4j.appender.lib.Append=true
- log4j.appender.lib.layout=org.apache.log4j.PatternLayout
- log4j.appender.lib.layout.ConversionPattern=[%-5p] %d %l : %m%n
- $>g++ -o test -I/usr/local/apr/include -L/usr/local/apr/lib -lapr-1 -laprutil-1 main.cpp
- $./test
会在屏幕中打印出
[DEBUG] 2012年11月14日 15:16:17,890 main.cpp(10) : test
先写到这里,下篇将会介绍log4cxx的配置。
我使用报错,没找到文件:
defines.h:24:28: fatal error: log4cxx/logger.h: No such file or directory
#include <log4cxx/logger.h>
我是装在:/usr/local/log4cxx 目录
下面有2个目录,include和lib。
/usr/local/log4cxx/include/log4cxx 才是头文件。
我们在gcc加上-I/usr/local/log4cxx/include 即可。
加上lib目录:-L/usr/local/log4cxx/lib
使用时
//usr/local/apr/lib/libapr-1.so.0: undefined reference to `uuid_generate@UUID_1.0'
加上
参考:
http://blog.csdn.net/fhxpp_27/article/details/8280024
http://www.linuxidc.com/Linux/2012-07/66663.htm