Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6539487
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: C/C++

2011-06-12 13:51:45

Log4cxx是开放源代码项目Apache Logging Service的子项目之一,用于为C++程序提供日志功能,以便开发者对目标程序进行调试和审计。
要使用log4cxx,首先要从官网下载,官网是,目前的最新版本是0.10.0,下载地址:。安装方式可以使用ant,也可以使用gcc编译安装,本文以gcc编译安装为主。安装log4cxx需要apr支持,apr官网地址是:,需要下载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的内容如下:
    log4j.rootLogger=debug, R
  1. log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
  1. # Pattern to output the caller's file name and line number.
  2. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
  3. log4j.appender.R=org.apache.log4j.RollingFileAppender
  4. log4j.appender.R.File=./hello.log
  5. log4j.appender.R.MaxFileSize=100KB
  6. # Keep one backup file
  7. log4j.appender.R.MaxBackupIndex=10
  8. log4j.appender.R.layout=org.apache.log4j.PatternLayout
  9. 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
阅读(6321) | 评论(0) | 转发(3) |
给主人留下些什么吧!~~