需要安装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 <string.h>
src/main/cpp/socketoutputstream.cpp 添加 #include <string.h> 
src/examples/cpp/console.cpp 添加 #include <string.h> #include <stdio.h>
#./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 <iostream>
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/exception.h> using namespace std;
using namespace log4cxx;
using namespace log4cxx::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" << endl;
} catch (Exception &) {
cout << "log4cxx init error" << endl;
}
return ;
} 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

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

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/

ubuntu下log4cxx安装使用的更多相关文章

  1. Torch7在Ubuntu下的安装与配置

    Torch7的本系列教程的主要目的是介绍Torch的入门使用.今天首先分享一下Torch7的安装.(在Ubuntu14.04安装torch7) 为什么选择Torch Torch的目标是在建立科学算法的 ...

  2. ubuntu下git安装及使用

    ubuntu下git安装及使用   其实,好几个月前,就已经安装好了,可是一直搁置在那儿,所以密码等一些其它细节都忘的差不多了,所以今天就重新部署了一下,并开始积极使用......... 1,git ...

  3. premake Ubuntu下的安装

    premake是个跨平台的编译工具,先看看在Ubuntu下怎么安装. 首先下载,在/usr目录下: sudo wget -O premake-4.4-beta4-linux.tar.gz http:/ ...

  4. linux,windows,ubuntu下git安装与使用

    ubuntu下git安装与使用:首先应该检查本地是否已经安装了git ,如果没有安装的话,在命令模式下输入 sudo apt-get install git 进行安装 输入git命令查看安装状态及常用 ...

  5. ubuntu下如何安装codeblocks集成开发环境

    codeblocks是一个十分优秀的C/C++开发IDE,虽然后起之秀codelite目前来看大有超越之势哦. 不过在ubuntu下安装codeblocks却比较麻烦,不像其他linux发行版,比如s ...

  6. ubuntu下makeinfo安装,其实真正安装的是texinfo包

    操作系统环境:ubuntu 在终端中执行命令:sudo apt-get install texinfo   今天在打包的时候有个包需要 makeinfo,当时就各种搜结果就没有 makeinfo 这个 ...

  7. Ubuntu下软件安装方式、PATH配置、查找安装位置

    Ubuntu 18.04, 安装方式 目前孤知道的Ubuntu下安装软件方式有3种(命令): 1.make 2.apt/apt-get 3.dpkg 方式1基于软件源码安装,需要经历配置(可选).编译 ...

  8. ubuntu 下python安装及hello world

    //@desn:ubuntu 下python安装及hello world //@desn:码字不宜,转载请注明出处 //@author:张慧源  <turing_zhy@163.com> ...

  9. ubuntu下mysql安装(server、client、dev),开启、停止和重启,及常见错误

    转自:ubuntu下mysql安装(server.client.dev),开启.停止和重启,及常见错误 1. 在ubuntu下安装server和client很简单: (1)安装server apt-g ...

随机推荐

  1. 计算1-9总共九个数字可以满足abc+def=hij这样的式子

    计算1-9总共九个数字可以满足abc+def=hij这样的式子:其中abcdefghij九个数字各个都不相同,它们都属于1-9个数字中: 首先,第一种方法很简单很暴力,直接枚举,这样的话时间复杂度高: ...

  2. python知识储备目录

    1.with as 的实现原理 https://www.cnblogs.com/lice-blog/p/11581741.html 2.事务的四个隔离级别 3.rabitMQ 4.c3算法 5.wit ...

  3. 《Tsinghua oc mooc》第5~7讲 物理内存管理

    资源 OS2018Spring课程资料首页 uCore OS在线实验指导书 ucore实验基准源代码 MOOC OS习题集 OS课堂练习 Piazza问答平台 暂时无法注册 疑问 段式内存管理中,逻辑 ...

  4. PTA(Advanced Level)1037.Magic Coupon

    The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, m ...

  5. PTA(Basic Level)1020.月饼

    月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...

  6. Oracle存储过程——日常记录

    代码规范 Oracle存储过程,关键字大写,变量小写并以v_开头,规范代码,提高可读性 赋值与判断符号 Oracle存储过程,变量赋值使用 := 符号,条件判断直接用 = 符号. 变量声明需在 beg ...

  7. Python Excel文件的读写操作(xlwt xlrd xlsxwriter)

    转:https://www.cnblogs.com/ultimateWorld/p/8309197.html Python语法简洁清晰,作为工作中常用的开发语言还是很强大的(废话). python关于 ...

  8. fiddler笔记:快捷工具栏

    WinConfig:   Comment 为所有选中的Session添加Comment. Replay Replay+ctrl 重新发送请求,而不包括任何条件请求头. Replay+shift 指定每 ...

  9. Python_oneday

    基本程序设计 一切代码输入,请使用英文输入法   编写一个简单的程序 圆公式面积: area = radius * radius * 3.1415   在Python里面不需要定义数据的类型   控制 ...

  10. linux命令自动补全

    在linux中命令较长时,不易记忆,使用命令行自动补全,使用方便,配置方法记录如下 需要安装bash-completion 重启后生效 命令:yum install bash-completion,安 ...