- 注意:等号线以下为网络原文:在make log4cxx的时候会报编译错误,在所
- 报错误的原文件里添加相应的标准c头文件即可。
- 主要有string.h/stdio.h
- ===================================================
- 简介
http://logging.apache.org/log4cxx/index.html
- 下载、编译、安装
cd ~/libs
wget http://mirror.bjtu.edu.cn/apache//apr/apr-1.4.4.tar.bz2
tar xjvf apr-1.4.4.tar.bz2
cd apr-1.4.4
./configure --prefix=${HOME}/libs && make && make install
cd ..
wget http://mirror.bjtu.edu.cn/apache//apr/apr-util-1.3.11.tar.bz2
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 ..
wget http://apache.etoak.com//logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz
tar xzvf apache-log4cxx-0.10.0.tar.gz
cd apache-log4cxx-0.10.0
./configure --with-charset=utf-8 --prefix=${HOME}/libs --with-apr=${HOME}/libs --with-apr-util=${HOME}/libs && make && make install
- 使用例子
#include
"
log4cxx/logger.h
"
#include " log4cxx/propertyconfigurator.h "
static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger( " hello " ));
int main( int argc, char * argv[])
{
log4cxx::PropertyConfigurator::configure( " ./log4cxx_hello.properties " );
LOG4CXX_INFO(logger, " 你好,log4cxx! " );
return 0 ;
}
#include " log4cxx/propertyconfigurator.h "
static log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger( " hello " ));
int main( int argc, char * argv[])
{
log4cxx::PropertyConfigurator::configure( " ./log4cxx_hello.properties " );
LOG4CXX_INFO(logger, " 你好,log4cxx! " );
return 0 ;
}
log4cxx_hello.properties:
log4j.rootLogger
=
debug
,
R
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern = %5p [ %t ] (%F:%L) - %m%n
log4j.appender.R = org.apache.log4j.RollingFileAppender
log4j.appender.R.File = ./hello.log
log4j.appender.R.MaxFileSize = 100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex = 10
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %5p %c [ %t ] (%F:%L) - %m%n
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern = %5p [ %t ] (%F:%L) - %m%n
log4j.appender.R = org.apache.log4j.RollingFileAppender
log4j.appender.R.File = ./hello.log
log4j.appender.R.MaxFileSize = 100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex = 10
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %5p %c [ %t ] (%F:%L) - %m%n
g++ -o hello hello.cpp -I${HOME}/libs/include ${HOME}/libs/lib/liblog4cxx.a ${HOME}/libs/lib/libaprutil-1.a ${HOME}/libs/lib/libapr-1.a -lexpat -lpthread
- 注意事项
这个限制避免性能问题,可以通过设置大一点的MaxFileSize来保存更多日志,否则就要在编译时改大一点了。
参考:http://objectmix.com/apache/684503-urgent-log4cxx-large-window-sizes-not-allowed.html
- 使用技巧
xml虽较property格式繁锁,支持的配置面更全,而property格式的配置文件使用更简单,容易在网上找到现成的配置文件。
2,logger命名。
logger名称反映了软件模块,如果有代表软件模块的类,则在类中包含以该类类名命名的logger对象,该模块功能相关代码通过该logger进行日志记录。
另外可将logger对象作为全局变量,方便使用,特别是当软件模块较松散,并无对应的封装类时。
3,在代码中适当地放置日志代码。引用适当的日志对象,对日志进行适当分级。
4,余下的工作就是修改配置文件,对日志进行控制了。
使用配置文件的好处就是可以方便地配置日志而不需要修改源代码,可以在配置文件中方便配置日志过滤、格式、日志目的地。
- 体验