一个项目的服务器端在Linux平台下,用到了开源日志库log4cxx,这个库是apache项目的一个子库。功能很不错。下面记录下它的编译和安装过程。
log4cxx的官方下载地址是http://logging.apache.org/log4cxx/index.html,我用的是0.10.0版本。
为了编译它,还需要两个辅助库,apr,和apr-util,可以在这里下载http://apr.apache.org/download.cgi,我用的版本分别是apr-1.3.8.tar.gz和apr-util-1.3.9.tar.gz。
第一步安装apr-1.3.8,顺序不能错,它必须首选安装
$tar zxvf apr-1.3.8.tar.gz
$cd apr-1.3.8
$./configure --prefix=/usr/local
$make
$su root
$make install
然后安装apr-util-1.3.9
$tar zxvf apr-util-1.3.9.tar.gz
$cd apr-util-1.3.9
$./configure --prefix=/usr/local --with-apr=/usr/local/apr
$make
$su root
$make install
configure选项 --with-apr=/usr/local/apr指定apr库的位置
最后就可以安装log4cxx了
$tar zxvf apache-log4cxx-0.10.0.tar.gz
$cd apache-log4cxx-0.10.0
$configure --prefix==/usr/local
(
或者 ./configure --prefix=/usr/local/log4cxx --with-apr=/usr/local/apr –-with-apr-util=/usr/local/apr-util)
$make
$su root
$make install
修改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相关命令可以查询相关资料了解。
因为编译成的是共享库,最后还要设置下搜索目录,编辑~/.bashrc,添加下面两行
LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH
OK,至此log4cxx就安装完毕了。
最后写个程序,测试下。
#include <log4cxx/logger.h>
#include <log4cxx/logstring.h>
#include <log4cxx/propertyconfigurator.h>
int main(int argc, char* argv[])
{
using namespace log4cxx;
// 读取配置文件
PropertyConfigurator::configure("log4cxx.cfg");
// 建立两个logger
LoggerPtr logger1 = Logger::getLogger("TraceYourMama");
LoggerPtr logger2 = Logger::getLogger("Patch");
LOG4CXX_TRACE(logger1, "跟踪");
LOG4CXX_WARN(logger1, "警告");
LOG4CXX_DEBUG(logger1, "调试");
LOG4CXX_ASSERT(logger1, false, "断言");
LOG4CXX_FATAL(logger1, "致命");
LOG4CXX_TRACE(logger2, "跟踪");
LOG4CXX_ERROR(logger2, "错误");
return 0;
}
编译链接
$g++ -o main main.cpp -llog4cxx
OK,打完手工。
linux 安装 log4cxx
猜你喜欢
转载自yand789.iteye.com/blog/2080556
今日推荐
周排行