linux下编译和安装log4cxx,ubuntu下log4cxx安装使用

本文详细介绍了在Ubuntu 14.04和RedHat上安装log4cxx的过程,包括apr、apr-util的安装以及解决编译时遇到的错误。在安装log4cxx时,需要先安装apr和apr-util,并在编译log4cxx时修改源文件以解决缺失头文件的问题。最后,通过示例代码展示了如何使用log4cxx进行日志记录,并提供了测试代码和配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要安装log4cxx,安装的过程中可是充满了坎坷。。。最大的问题是在make log4cxx时,总是报undefined XML什么什么的错误,查了一下也没解决了,然后把apr-utils删了重新装了一下就好了。。

log4cxx现在是apache的一个项目,用来记录日志。看名字就知道,是给c++使用的。

环境(在以下2个环境中进行验证测试):

gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4

gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-16)

log4cxx依赖于apache 的 apr 和 apr-util,所以安装顺序是: apr, apr-util, log4cxx。

1.软件包下载

apr: http://apr.apache.org/download.cgi

apr-util: http://apr.apache.org/download.cgi

log4cxx: http://logging.apache.org/log4cxx/download.html

2.安装apr

#tar xzvf apr-1.5.2.tar.bz2

#cd apr-1.5.2

#./configure --prefix=/usr/local/apr

#make

#make install

3.安装apr-util

#tar xzvf apr-util-1.5.4.tar.bz2

#cd apr-util-1.5.4

#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

#make

#make install

4.安装log4cxx

#tar xzf apache-log4cxx-0.10.0.tar.gz

#cd apache-log4cxx-0.10.0

log4cxx直接make会报类似error: ‘memmove’ was not declared in this scope的错误,参考前人的工作,修改以下源文件:

src/main/cpp/inputstreamreader.cpp 添加 #include

src/main/cpp/socketoutputstream.cpp 添加 #include

src/examples/cpp/console.cpp 添加 #include #include

#./configure --prefix=/usr/local/log4cxx --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util

#make

#make install

5.把编译后的库加载到环境变量中(实际使用和发布使用log4cx库需要其他更多的工作)

export LD_LIBRARY_PATH=/usr/local/apr/lib/:/usr/local/apr-util/lib/:/usr/local/log4cxx/lib/

6.测试代码

test.cpp

#include #include#include#include

using namespacestd;using namespacelog4cxx;using namespacelog4cxx::helpers;static const string CONF_LOG_FILE = "/home/fg/src/work/test/test.properties";

LoggerPtr logger(Logger::getRootLogger());int main(void)

{try{

PropertyConfigurator::configure(CONF_LOG_FILE);

LOG4CXX_INFO(logger,"Init() success.");

cout<< "success" <

}catch (Exception &) {

cout<< "log4cxx init error" <

}return 0;

}

test.cpp

test.properties文件的内容:

log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=log.dat

log4j.appender.file.MaxFileSize=20MB

log4j.appender.file.MaxBackupIndex=10

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

test.properties

114f38cbae84419335b09f994f97b989.gif

log4j.rootLogger=INFO, file

log4j.appender.file=org.apache.log4j.RollingFileAppender

log4j.appender.file.File=log.dat

log4j.appender.file.MaxFileSize=20MB

log4j.appender.file.MaxBackupIndex=10

log4j.appender.file.layout=org.apache.log4j.PatternLayout

log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

dc6dd8e8dfee9774a319e5adb69b6cd6.gif

7.编译:

g++ -o test0 test.cpp -L/usr/local/log4cxx/lib -llog4cxx -I/usr/local/log4cxx/include

运行:

./test

可以看到生成了log.dat文件,里面写入了LOG4CXX_INFO()输出的日志内容。

参考资料:

1.http://blog.chinaunix.net/uid-24512513-id-3195404.html

2.http://zipperary.com/2015/08/04/log4cxx/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值