下载安装包

http://logging.apache.org/log4cxx/index.html ,从这里下载,目前为0.10.0

下载apr, http://apr.apache.org/download.cgi ,从这里下载apr,apr-util,版本为apr-1.3.8.tar.gz 和apr-util-1.3.9.tar.gz

编译安装apr

第一步安装apr-1.3.8,顺序不能错,它必须首选安装

$tar zxvf apr-1.3.8.tar.gz

$cd apr-1.3.8 

$./configure --prefix=/usr/local

$make

$su root

$make install

然后安装apr-util-1.3.9

$tar zxvf apr-util-1.3.9.tar.gz

$cd apr-util-1.3.9

$./configure --prefix=/usr/local --with-apr=/usr/local/apr

$make

$su root

$make install

如果出现问题不能编译,则可采用yum安装,命令为:yum install apr-devel.x86_64 apr-util-devel.x86_64

 

编译安装log4cxx

$tar zxvf apache-log4cxx-0.10.0.tar.gz

$cd apache-log4cxx-0.10.0

$configure --prefix==/usr/local

$make

$su root

$make install

测试程序

// include log4cxx header files.
#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(Logger::getLogger("R"));
LoggerPtr logger_lib_a(Logger::getLogger("Lib_a"));
LoggerPtr logger_lib_b(Logger::getLogger("Lib_b"));

int main(int argc, char **argv)
{

    try
    {
        // Set up a simple configuration that logs on the console.
       PropertyConfigurator::configure("log4cxx.properties");

        //LOG4CXX_INFO(logger, "test R")
        LOG4CXX_DEBUG(logger_lib_a, "test R.a")
        LOG4CXX_DEBUG(logger_lib_b, "test R.b")
    }
    catch(...)
    {
    }

    return 0;
}

makefile文件
.PHONY:all
all: log

CC= g++

CFLAGS = -O0 -w -g -c -D_DEBUG -D_LINUX
CPLUSFLAGS = -O0 -c -g  -Wno-invalid-offsetof -D_DEBUG -D_LINUX
INCLUDES = -I./ -I/usr/local/include
LIBS= -L/usr/local/lib  -llog4cxx

libsrcc = $(wildcard ./*.c)
libsrccplus = $(wildcard  ./*.cpp)
libobjc=  $(libsrcc:.c=.o)
libobjcplus= $(libsrccplus:.cpp=.o)

%.o:%.cpp
	$(CC)	$(INCLUDES) $(CPLUSFLAGS) -o $@ $<

%.o:%.c
	gcc $(INCLUDES) $(CFLAGS) -o $@ $<

log: $(libobjc) $(libobjcplus)
	$(CC) -o $@ $^ $(LIBS)

.PHONY:clean
clean:
	rm  advertise_server  $(libobjc) $(libobjcplus) -f

配置文件:

log4j.additivity.gather = false  
log4j.rootLogger= debug, R, stdout


log4j.logger.Lib_a =debug, ap1, stdout
log4j.logger.Lib_b =debug, ap2, stdout


log4j.additivity.Lib_a=false  
log4j.additivity.Lib_b=false  

log4j.appender.logfile.encoding=UTF-8  
  

log4j.appender.stdout=org.apache.log4j.ConsoleAppender  
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout  
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n 

//Root
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=./hello.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern==%d [%c]-[%p] %m%n 

log4j.appender.ap1=org.apache.log4j.RollingFileAppender
log4j.appender.ap1.File=./hello_a.log
log4j.appender.ap1.MaxFileSize=100KB
log4j.appender.ap1.MaxBackupIndex=10
log4j.appender.ap1.layout=org.apache.log4j.PatternLayout
log4j.appender.ap1.layout.ConversionPattern==%d{yyyy-MM-dd} [%c]-[%p] %m%n

log4j.appender.ap2=org.apache.log4j.RollingFileAppender
log4j.appender.ap2.File=./hello_b.log
log4j.appender.ap2.MaxFileSize=100KB
log4j.appender.ap2.MaxBackupIndex=10
log4j.appender.ap2.layout=org.apache.log4j.PatternLayout
log4j.appender.ap2.layout.ConversionPattern==%d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n 



Logo

更多推荐