Fedora 6怎么使用log4cxx的版本为0.97
如何使用log4cxx的版本为0.97
log4cxx的版本为0.97是一个开源的C++日志记录库,它可以帮助我们轻松的记录日志信息。log4cxx使用了一个配置文件来读取日志记录的相关配置,这个配置文件一般是log4j.properties或者log4j.xml。
要想在Fedora 6中使用log4cxx的版本为0.97,我们需要做一些准备工作:
首先,确保你的系统中安装了gcc、g++、make等编译工具。如果没有安装,可以使用yum命令来安装:
sudo yum install gcc gcc-c++ make
然后,下载log4cxx的源码并解压缩:
wget http://www.apache.org/dist/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz
tar -xzvf apache-log4cxx-0.10.0.tar.gz
接下来,进入log4cxx的源码目录并执行configure脚本来生成Makefile文件:
cd apache-log4cxx-0.10.0
./configure
如果一切顺利,会看到如下输出:
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for string.h... (cached) yes
checking for memory.h... (cached) yes
checking sys/time.h usability... yes
checking sys/time.h presence... yes
checking for sys/time.h... yes
checking for unistd.h... (cached) yes
checking for an ANSI C-conforming const... yes
checking whether time.h and sys/time.h may both be included... yes
checking whether gcc needs -traditional... no
checking for g++... g++
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking how to run the C++ preprocessor... g++ -E
checking for objdir... .libs
checking if gcc supports -fno-rtti -fno-exceptions... no
checking for gcc option to produce PIC... -fPIC -DPIC
checking if gcc PIC flag -fPIC -DPIC works... yes
checking if gcc static flag -static works... yes
checking if gcc supports -c -o file.o... yes
checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no
configure: creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing default-1 commands
编译并安装log4cxx:
make
sudo make install
安装完成后,可以在/usr/local/lib目录下看到liblog4cxx.so文件。
最后,我们来测试一下log4cxx是否能够正常工作。首先,编写一个测试程序:
#include
#include
#include
#include
using namespace log4cxx;
int main()
{
// BasicConfigurator::configure();
PropertyConfigurator::configure("log4j.properties");
LoggerPtr logger(Logger::getLogger("test"));
LOG4CXX_INFO(logger, "This is a test!");
return 0;
}
然后,编写一个log4j.properties配置文件:
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss}] [%p] %m%n
最后,使用g++编译并运行测试程序:
g++ test.cpp -o test -llog4cxx
./test
如果一切正常的话,会在控制台输出如下信息:
[2018-01-01 12:00:00] [INFO] This is a test!
[2018-01-01 12:00:00] [INFO] This is a test!
说明log4cxx在Fedora 6中可以正常使用。
相关文章