Linux下log4cxx的安装使用

简介: Log4cxx是开放源代码项目Apache Logging Service的子项目之一,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计。 要使用log4cxx,首先要从官网下载,官网是http://logging.apache.org/log4cxx/index.html,目前的最新版本是0.10.0,下载地址:http://www.apache.org/dyn/closer.cgi/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz。
Log4cxx是开放源代码项目 Apache Logging Service的子项目之一,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计。
要使用log4cxx,首先要从官网下载,官网是http://logging.apache.org/log4cxx/index.html,目前的最新版本是0.10.0,下载地址:http://www.apache.org/dyn/closer.cgi/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz。安装方式可以使用ant,也可以使用gcc编译安装,本文以gcc编译安装为主。安装log4cxx需要apr支持,apr官网地址是:http://apr.apache.org/,需要下载apr和apr_util两个组件。
安装步骤如下:
cd ~/libs
tar xjvf apr-1.4.4.tar.bz2
cd apr-1.4.4
./configure --prefix=${HOME}/libs && make && make install
cd ..
tar xjvf apr-util-1.3.11.tar.bz2
cd apr-util-1.3.11
./configure --prefix=${HOME}/libs --with-apr=${HOME}/libs && make && make install
cd ..
tar xzvf apache-log4cxx-0.10.0.tar.gz
cd apache-log4cxx-0.10.0
./configure --with-charset=utf-8 --with-apr=${HOME}/libs --with-apr-util=${HOME}/libs && make && make install
默认安装后会在/usr/local/include目录下包含log4cxx目录,该目录下就是log4cxx的相关头文件。

下面写个例子(采用基本配置方式,即不使用配置文件)

  1. #include log4cxx/logger.h>
  2. #include log4cxx/basicconfigurator.h>
  3. #include log4cxx/helpers/exception.h>

  4. using namespace log4cxx;
  5. using namespace log4cxx::helpers;

  6. LoggerPtr logger(Logger::getLogger("MyApp"));

  7. int main(int argc, char **argv)
  8. {
  9.     int result = EXIT_SUCCESS;
  10.     try
  11.     {
  12.         BasicConfigurator::configure();
  13.         LOG4CXX_INFO(logger,"Entering application");
  14.     }
  15.     catch(Exception&)
  16.     {
  17.         result =EXIT_FAILURE;
  18.     }
  19.     return result;
  20. }

在我们用g++命令编译的时候,可能会出现链接错误,从晚上找了半天,原来编译的时候要用-l参数指定log4cxx命名空间。如下:

  1. g++ -llog4cxx -o myApp myApp.cpp

这样就会产生myApp可执行文件了。

在执行的时候,你可能会得到下面的错误:
  1. ./a.out: error while loading shared libraries: liblog4cxx.so.10: cannot open shared object file: No such file or directory
查找半天发现在/usr/local/lib下有liblog4cxx.so.10文件,这个问题是因为你没有设置LD_LIBRARY_PATH环境变量造成的。执行
  1. export LD_LIBRARY_PATH=/usr/local/lib
再运行,看到树下的输出了吧:
  1. 0 [0xb7fb36c0] INFO MyApp null - Entering application
如果需要写配置文件,例子如下:
  1. #include "log4cxx/logger.h"
  2. #include "log4cxx/propertyconfigurator.h"

  3. static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("recommend_updater"));

  4. int main(int argc, char *argv[])
  5. {
  6.         log4cxx::PropertyConfigurator::configure("./log4cxx_hello.properties");
  7.         LOG4CXX_INFO(logger, "use properities file");
  8.         return 0;
  9. }
log4cxx_hello.properties的内容如下:
  1. log4j.rootLogger=debug, R
  2. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  1. log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  2. # Pattern to output the caller's file name and line number.
  3. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
  4. log4j.appender.R=org.apache.log4j.RollingFileAppender
  5. log4j.appender.R.File=./hello.log
  6. log4j.appender.R.MaxFileSize=100KB
  7. # Keep one backup file
  8. log4j.appender.R.MaxBackupIndex=10
  9. log4j.appender.R.layout=org.apache.log4j.PatternLayout
  10. log4j.appender.R.layout.ConversionPattern=%5p %c [%t] (%F:%L) - %m%n
上面的配置了大小为100K的滚动日志。
 
第二篇:Studio2008下编译使用log4cxx http://blog.chinaunix.net/space.php?uid=78707&do=blog&id=374503
 
下面是别人的在windows的环境下使用log4cxx的文章,http://blog.chinaunix.net/space.php?uid=78707&do=blog&id=368675
目录
相关文章
|
24天前
|
Linux Shell
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
Linux手动清理Linux脚本日志定时清理日志和log文件执行表达式
78 1
|
1月前
|
监控 Shell Linux
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
【Shell 命令集合 系统管理 】Linux 自动轮转(log rotation)日志文件 logrotate命令 使用指南
51 0
|
4月前
|
NoSQL 网络协议 Linux
Linux下安装使用Redis
Linux下安装使用Redis
|
6月前
|
Linux
百度搜索:蓝易云 ,Linux系统查看log日志命令详解!
通过使用以上命令,您可以方便地查看和搜索Linux系统中的日志文件内容。根据您的需求,选择适当的命令和选项来满足您的日志查看要求。
117 0
|
7月前
|
Linux Python
百度搜索:蓝易云【Linux系统下安装使用anaconda教程。】
以上是在Linux系统下安装和使用Anaconda的基本步骤。安装完成后,你可以根据自己的需要创建环境、安装软件包,并使用Python和数据科学工具进行开发和分析工作。
346 0
|
4月前
|
负载均衡 前端开发 应用服务中间件
【Linux】Nginx安装使用负载均衡及动静分离(前后端项目部署),前端项目打包
【Linux】Nginx安装使用负载均衡及动静分离(前后端项目部署),前端项目打包
379 0
|
6月前
|
Linux Python
百度搜索:蓝易云【如何在 Linux 环境下安装使用 pycharm?】
现在,您已成功在Linux环境下安装和使用PyCharm。您可以使用PyCharm进行Python开发和编程工作。
136 5
|
6月前
|
Linux 网络安全
【Linux】INFO: attempting to log in with the new key(s), to filter out any that are already...
【Linux】INFO: attempting to log in with the new key(s), to filter out any that are already...
318 0
|
7月前
|
运维 Linux 程序员
Linux查看log日志命令总结
Linux查看log日志命令总结
262 1
|
8月前
|
自然语言处理 Ubuntu Linux
Baumer工业相机堡盟相机在Linux系统下安装使用CameraExplorer软件和相机IP设置工具
Baumer工业相机堡盟相机在Linux系统下安装使用CameraExplorer软件和相机IP设置工具
104 0