linux下编译和安装log4cxx,ubuntu下log4cxx安装使用「建议收藏」

linux下编译和安装log4cxx,ubuntu下log4cxx安装使用「建议收藏」需要安装log4cxx,安装的过程中可是充满了坎坷。。。最大的问题是在makelog4cxx时,总是报undefinedXML什么什么的错误,查了一下也没解决了,然后把apr-utils删了重新装了一下就好了。。log4cxx现在是apache的一个项目,用来记录日志。看名字就知道,是给c++使用的。环境(在以下2个环境中进行验证测试):gcc(Ubuntu4.8.4-2ubuntu1~1…

大家好,又见面了,我是你们的朋友全栈君。

需要安装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/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/151217.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月20日 下午9:46
下一篇 2022年6月20日 下午10:00


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号