Linux下log4cxx的安装和配置

来源:互联网 发布:gulp.js下载 编辑:程序博客网 时间:2024/04/27 04:25

本节的主要内容是如何在Linux下进行Log4cxx的配置,基于的Linux版本是Ubuntu,不同应该同样适用于其他的Linux发型版本(推测,没有实际的测试)。下面详述安装与配置的相关过程(全部操作都是在文本模式下进行,建议是在root账户下操作)。

第一步:下载Log4cxx相关软件和文件包,分别是:apr-1.4.6.tar.gz, apr-util-1.4.1.tar.gz,apache-log4cxx-0.10.0.tar.gz三个文件,可以根据需要下载相应的版本,本节是基于上面的三个版本,在Linux下的下载方法(文本模式下):

注:假设当前目录为test,则全部下载完毕后该目录下会有apr-1.4.6.tar.gz、apr-util-1.4.1.tar.gz、apache-log4cxx-0.10.0.tar.gz三个文件

apr-1.4.6.tar.gz:wget http://apr.apache.org/apr-1.4.6.tar.gz

apr-util.1.4.1.tar.gz:wget http://mirror.bit.edu.cn/apache//apr/apr-util-1.4.1.tar.gz(apache//apr是两个斜杠间隔的)

apache-log4cxx-0.10.0.tar.gz:

wget http://apache.etoak.com/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz

第二步:解压下载的三个文件,在shell下敲入:

tar zxvfapr-1.4.6.tar.gz               # 生成apr-1.4.6文件夹

tar.zxvfapr-util-1.4.1.tar.gz   # 生成apr-util-1.4.1文件夹

tar zxvfapache-log4cxx-0.10.0.tar.gz   # 生成apache-log4cxx-0.10.0文件夹

第三步:编译Log4cxx相关程序

1. apr-1.4.6

       A. cd apr-1.4.6      # 进入apr-1.4.6目录

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

       C. make

       D. make install

2.apr-util-1.4.1.tar.gz

       A. cd ../apr-util-1.4.1

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

       C. make

       D.make install

3.apache-log4cxx-0.10.0

       A. cd ../apache-log4cxx-0.10.0

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

       C. make

D. make install

上述执行完毕后,可以看到在/usr/local目录下生成的三个文件:apr、apr-util和log4cxx。

第四步:修改Log4cxx程序

1.vimsrc/main/cpp/inputstreamreader.cpp

增加#include <string.h>;

#include<log4cxx/logstring.h>

#include<log4cxx/helpers/inputstreamreader.h>

#include<log4cxx/helpers/exception.h>

#include<log4cxx/helpers/pool.h>

#include<log4cxx/helpers/bytebuffer.h>

+

#include<string.h>

+

否则会出现inputstreamreader.cpp:66: error: 'memmove' was not declared in thisscope

make[3]: ***[inputstreamreader.lo] 错误 1

 

2.vimsrc/main/cpp/socketoutputstream.cpp

增加#include <string.h>;

#include<log4cxx/logstring.h>

#include<log4cxx/helpers/socketoutputstream.h>

#include<log4cxx/helpers/socket.h>

#include<log4cxx/helpers/bytebuffer.h>

+

#include<string.h>

+

否则会出现socketoutputstream.cpp:52: error: 'memcpy' was not declared in thisscope

3.vimsrc/examples/cpp/console.cpp

增加#include <string.h>,#include <stdio.h>;

+

#include<stdio.h>

+

#include<stdlib.h>

+

#include<string.h>

+

#include<log4cxx/logger.h>

#include<log4cxx/consoleappender.h>

#include<log4cxx/simplelayout.h>

#include<log4cxx/logmanager.h>

#include<iostream>

#include<locale.h>

否则会出现

console.cpp:In function ‘int main(int, char**)’:

console.cpp:58:错误:‘puts’在此作用域中尚未声明

第五步:配置Log4cxx环境

ExportLD_LIBRARY_PATH=/usr/local/apr/bin/:/usr/local/apr-util/bin:/usr/local/log4cxx/bin/:$LD_LIBRARY_PATH

经过上面的五步,Log4cxx在Linux下的环境配置已经完毕。

下面是测试实例:

第一步:编写Log4cxx配置文件:test.properties

# Root logger

Log4j.rootLogger=DEBUG,list

 

# Appender:list

log4j.appender.list=org.apache.log4j.FileAppender

log4j.appender.list.File=./list.log

log4j.appender.list.ImmediateFlush=true

log4j.appender.list.Append=true

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

log4j.appender.list.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:SS} %5p -%m %n

第二步:编写测试程序

#include<log4cxx/logger.h>

#include<log4cxx/propertyconfigurator.h>

 

usingnamespace log4cxx;

 

int main()

{

       // Read configure file

PropertyConfigurator::configure("./test.properties");

// Get root logger

LoggerPtr rootLogger = Logger:getRootLogger();

// Log information

LOG4CXX_TRACE(rootLogger, "TRACE");

LOG4CXX_DEBUG(rootLogger, "DEBUG");

LOG4CXX_WARN(rootLogger, "WARN");

LOG4CXX_INFO(rootLogger, "INFO");

LOG4CXX_ERROR(rootLogger, "ERROR");

      

       return 0;

}

第三步:编译

g++ -o testtest.cc –L/usr/local/log4cxx/bin –llog4cxx –I/usr/local/log4cxx/include

执行完后生成test可执行文件

第四步:运行

./test

以上是Log4cxx在Linux下的测试实例。

注:其中本节涉及到的Linux相关命令可以查询相关资料了解。

原创粉丝点击