概述
LOG4cxx安装指南
简介:Log4shell是开放源代码项目Apache Logging Service的子项目log4j在shell下的实现,用于为shell程序提供日志功能,以便开发者对目标程序进行调试和审计。虽然shell可以方便的操作文件,但通过成熟的日志框架更方便我们对日志的记录与处理。
(1)Reference-ubuntu
(2)Reference-其它
(3)Reference-log4shell(该参考文档无法实现仅供参考)
1 下载源码
安装:
aprapr-util、log4cxx
2 安装log4cxx步骤-ubuntu(已验证)
2.0 环境
- Linux version 4.15.0-70-generic (buildd@lgw01-amd64-057) (gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12))
2.1 安装apr
# ./configure --prefix=/usr/local
# make
# sudo make install
2.2 安装apr-util
# ./configure --prefix=/usr/local --with-apr=/usr/local
# make
# sudo make install
2.3 安装log4cxx
# ./configure --prefix=/usr/local/ --with-apr=/usr/local/ --with-apr-util=/usr/local/ --with-charset=utf-8 --with-logchar=utf-8
添加头文件
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>;
# make
# sudo make install
3 测试
3.1 测试主函数
main.cpp
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/propertyconfigurator.h>
using namespace log4cxx;
using namespace log4cxx::helpers;
LoggerPtr logger_file(Logger::getLogger("file"));//获取配置文件中file对应的句柄
LoggerPtr logger_file(Logger::getLogger("console"));//获取配置文件中console对应的句柄
int main(){
PropertyConfigurator::configure("./log4cxx.properties");//加载配置文件,下面会细说
LOG4CXX_INFO(logger_file,"This is a test");
return 0;
}
3.2 配置文件
log4cxx.properties
#设置日志level为INFO,fa为文件输出对象,ca为控制台输出对象
log4j.rootLogger=INFO,fa,ca
#设置日志对象
log4j.logger.console=INFO,ca
log4j.logger.file=INFO,fa
#不向默认对象(rootLogger)输出,因此信息只能输出到文件
log4j.additivity.file=false
#**************************
#控制台,同时也会输出到默认输出对象
#**************************
log4j.appender.ca=org.apache.log4j.ConsoleAppender
log4j.appender.ca.layout=org.apache.log4j.PatternLayout
log4j.appender.ca.layout.ConversionPattern=[%-5p][%d] : %m%n
#**************************
#设置日志文件信息,比如大小,时间格式
#**************************
log4j.appender.fa=org.apache.log4j.DailyRollingFileAppender
log4j.appender.fa.File=./log/today.log
log4j.appender.fa.Append=fasle
log4j.appender.fa.MaxFileSize=10MB
log4j.appender.fa.MaxBackupIndex=1000
log4j.appender.fa.DatePattern=yyyy-MM-dd
log4j.appender.fa.layout=org.apache.log4j.PatternLayout
log4j.appender.fa.layout.ConversionPattern=[%-5p][%d] : %m%n
3.2 配置环境变量(~/.bashrc的方法?)
export LD_LIBRARY_PATH=/usr/local/lib
或长期生效~/.bashrc
export LD_LIBRARY_PATH=/usr/local/lib
export PATH=$LD_LIBRARY_PATH:$PATH
# source ~/.bashrc
# echo $PATH # 查看结果
3.3 编译
# g++ main.cpp -o test -llog4cxx
3.4 执行
进入log文件夹打印’today.log’日志
[INFO ][2020-02-19 20:02:01,477] : This is a test
4 移植log4cxx步骤-集中器(交叉编译没有寻找到log4cxx库,待解决)
(1)Reference-交叉编译-报错与解决
4.0 环境
- Linux version 4.1.15-gee690fe-dirty (root@ubuntu) (gcc version 7.3.1 20180425 [linaro-7.3-2018.05 revision d29120a424ecfbc167ef90065c0eeb7f91977701] (Linaro GCC 7.3-2018.05) )
4.1 安装pcre
# ./configure --prefix=/home/pan/toolchain/log4shell/install/usr/local --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ LD=arm-linux-gnueabihf-ld
4.2 移植apr
①
# ./configure --host=arm-linux-gnueabihf --prefix=/home/pan/toolchain/log4shell/install/usr/local CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ LD=arm-linux-gnueabihf-ld ac_cv_file__dev_zero=yes ac_cv_func_setpgrp_void=yes apr_cv_tcp_nodelay_with_cork=yes ac_cv_sizeof_struct_iovec=8 --cache=arm-linux.cache
待测试
# export ARMROOTS=/home/pan/toolchain/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/libc
./configure --host=arm-linux-gnueabihf --prefix=$ARMROOTS/usr/apr CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ LD=arm-linux-gnueabihf-ld ac_cv_file__dev_zero=yes ac_cv_func_setpgrp_void=yes apr_cv_tcp_nodelay_with_cork=yes ac_cv_sizeof_struct_iovec=8 --cache=arm-linux.cache
②
./arm-linux.cache添加如下代码
apr_cv_process_shared_works=yes
apr_cv_mutex_robust_shared=yes
③
./configure添加如下代码
1>源码位置:
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking which format to use for apr_ssize_t" >&5
$as_echo_n "checking which format to use for apr_ssize_t... " >&6; }
...
$as_echo "%ld" >&6; }
上个代码后插入如下代码:(添加)
elif test "$ac_cv_sizeof_ssize_t" = "$ac_cv_sizeof_long_long";then
ssize_t_fmt="lld"
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: %lld" >&5
$as_echo "%lld" >&6; }
衔接源代码:
else
as_fn_error $? "could not determine the proper format for apr_ssize_t" "$LINENO" 5
fi
2>源码位置:
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking which format to use for apr_size_t" >&5
$as_echo_n "checking which format to use for apr_size_t... " >&6; }
...
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: %ld" >&5
$as_echo "%ld" >&6; }
上个代码后插入如下代码:(添加)
elif test "$ac_cv_sizeof_size_t" = "$ac_cv_sizeof_long_long"; then
size_t_fmt="lld"
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: %lld" >&5
$as_echo "%lld" >&6; }
衔接代码
else
as_fn_error $? "could not determine the proper format for apr_size_t" "$LINENO" 5
fi
# make
# sudo make install
4.3 移植apr-util
# ./configure --prefix=/home/pan/toolchain/log4shell/install/usr/local --with-apr=/home/pan/toolchain/log4shell/install/usr/local --host=arm-linux-gnueabihf CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ LD=arm-linux-gnueabihf-ld
# make
# sudo make install
4.3 安装log4cxx
# cd apache-log4cxx-0.10.0
# ./configure --host=arm-linux-gnueabihf --prefix=/home/pan/toolchain/log4shell/install/usr/log4cxx/ --with-apr=/home/pan/toolchain/log4shell/install/usr/local/ --with-apr-util=/home/pan/toolchain/log4shell/install/usr/local/ --with-charset=utf-8 --with-logchar=utf-8 CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++
添加头文件
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>;
# make
# sudo make install
5 测试移植
5.1 配置环境变量
# export ARM_LD_LIBRARY_PATH=/home/pan/toolchain/log4shell/log4cxx
log4cxx放到如下目录下
/home/pan/toolchain/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/include/c++/4.8.2/log4cxx
log4cxx/lib下的文件导入到arm-linux…目录下:
root@ubuntu:/home/pan/toolchain/log4shell/install/usr/log4cxx/lib# cp -rf * /home/pan/toolchain/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/lib
5.2 编译(编译不过)
# arm-linux-gnueabihf-g++ main.cpp -o test -I/home/pan/toolchain/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/arm-linux-gnueabihf/lib/arm-linux-gnueabi/log4cxx -llog4cxx
5.2.1 编译报错(未解决)
/home/pan/toolchain/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/ ../lib/gcc/arm-linux-gnueabihf/4.8.2/../../../../arm-linux-gnueabihf/bin/ld: warning: libaprutil-1.so.0, needed by /home/pan/toolchain/gcc-linaro-arm-linux-gnueabihf-4.8-2013.10_linux/bin/ ../lib/gcc/arm-linux-gnueabihf/4.8.2/../../../../arm-linux-gnueabihf/lib/liblog4cxx.so, not found (try using -rpath or -rpath-link)
最后
以上就是老迟到棒棒糖为你收集整理的LOG4cxx安装指南LOG4cxx安装指南的全部内容,希望文章能够帮你解决LOG4cxx安装指南LOG4cxx安装指南所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复