log4cxx日志是:
log4cxx就是一个记录日志的C++库,它是从著名的Java日志库log4j移植而来的,并且它是Apache的一个项目,号称灵活好,速度快
log4cxx安装
1.下载安装
apr和apr-util下载地址为:http://apr.apache.org
log4cxx的下载地址为:http://logging.apache.org/log4cxx/download.html
我下载如图所示:
解压:
tar -xzvf apache-log4cxx-0.11.0.tar.gz apache-log4cxx-0.11.0
tar -xzvf apr-1.7.0.tar.gz apr-1.7.0
tar -xzvf apr-util-1.6.1.tar.gz apr-util-1.6.1
2.编译
编译apr,进入apr-1.7.0目录:
./configure --prefix=/home/lishuwei/ThirdpartyLib/log4cxx/apr
make
make install
编译apr-util,进入目录:
./configure --prefix=/home/lishuwei/ThirdpartyLib/log4cxx/apr-util --with-apr=/home/lishuwei/ThirdpartyLib/log4cxx/apr
make
make install
编译log4cxx进入目录:
./configure --prefix=/home/lishuwei/ThirdpartyLib/log4cxx/log4cxx --with-apr=/home/lishuwei/ThirdpartyLib/log4cxx/apr --with-apr-util=/home/lishuwei/ThirdpartyLib/log4cxx/apr-util
make
make install
编译错误:
pache-log4cxx-0.11.0/missing: line 81: aclocal-1.16: command
解决方式:ref:https://blog.csdn.net/jzp12/article/details/89185783
sudo autoreconf -ivf
log4cxx使用例子:
将log4cxx头文件和库文件复制到工程目录下,工程目录如下:
main.cpp:
#include<log4cxx/logger.h>
#include<log4cxx/propertyconfigurator.h>
using namespace log4cxx;
int main()
{
PropertyConfigurator::configure("../log4cxx.properties");
LoggerPtr rootLogger = Logger::getRootLogger();
LOG4CXX_INFO(rootLogger,"HHH");
return 0;
}
log4cxx.properties:
如何配置参考:https://blog.csdn.net/u012632043/article/details/79746573
#设置rootlogger为DEBUG级别,使用了ca和fa两个Appender
log4j.rootLogger=DEBUG,ca, fa
#对Appenderfa进行设置:
#这一个文件类型的Appender,
#其输出文件(File)为./output.log,
#输出方式(Append)为覆盖方式,
#输出格式(layout)为PatternLayout
log4j.appender.fa=org.apache.log4j.FileAppender
log4j.appender.fa.File=./output.log
log4j.appender.fa.Append=true
log4j.appender.fa.layout=org.apache.log4j.PatternLayout
log4j.appender.fa.layout.ConversionPattern=%d[%t] %-5p %.16c - %m%n
#对Appenderca进行设置:
#这是一个控制台类型的Appender
#输出格式(layout)为PatternLayout
log4j.appender.ca=org.apache.log4j.ConsoleAppender
log4j.appender.ca.layout=org.apache.log4j.PatternLayout
log4j.appender.ca.layout.ConversionPattern=%d[%t] %-5p %.16c - %m%n
CMakeLists.list:
cmake_minimum_required(VERSION 3.10)
project(log4cxx)
include_directories(./log4cxx-demo/include)
link_directories(./log4cxx-demo/lib)
add_executable(TestLog4cxx main.cpp)
target_link_libraries(TestLog4cxx log4cxx)
运行结果:
参考:
https://blog.51cto.com/yaocoder/980276
https://logging.apache.org/log4cxx/latest_stable/usage.html