log4cxx 使用
时间:2010-07-23 来源:erlongabc
调用log4cxx步骤
1. 引入头文件和lib
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#pragma comment (lib, "log4cxx.lib") 或者 把log4cxx.lib 加到 project->settings->linker->object/library modules
2. 设置配置文件
log4cxx::PropertyConfigurator::configureAndWatch
3. 写log
log4cxx::Logger::getLogger(_pref)->info
log4cxx::Logger::getLogger(_pref)->debug
log4cxx::Logger::getLogger(_pref)->fatal
...
log4cxx支持对不同的prefix 使用不同的appender,比如像下面的.properties 文件
# DEBUG < INFO < WARN < ERROR < FATAL
log4j.rootLogger=info, R
log4j.logger.TNetSvr = info, TNS
log4j.logger.TNetCmd = info, TNC
log4j.logger.TMQSvr = info, TMQ
# local rolling file appender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=C:\\Program Files\\THC\\TRDS2\\thcclient.log
log4j.appender.R.MaxFileSize=1024KB
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern= %p %d{%Y-%m-%d:%H:%M:%S,%Q} %c - %m%n
# socketappender
# ensure the RemoteHost ip and port valid
log4j.appender.SOCKET=org.apache.log4j.SocketAppender
log4j.appender.SOCKET.RemoteHost=192.168.0.1
log4j.appender.SOCKET.Port=80
log4j.appender.SOCKET.LocationInfo=true
log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %d{%Y-%m-%d:%H:%M:%S,%Q} %c - %m%n
log4j.appender.TNS=org.apache.log4j.RollingFileAppender
log4j.appender.TNS.File=C:\\Program Files\\THC\\TRDS2\\TNetSvr.log
log4j.appender.TNS.MaxFileSize=1024KB
log4j.appender.TNS.MaxBackupIndex=10
log4j.appender.TNS.layout=org.apache.log4j.PatternLayout
log4j.appender.TNS.layout.ConversionPattern= %p %d{%Y-%m-%d %H:%M:%S} %c %u - %m%n
log4j.appender.TNC=org.apache.log4j.RollingFileAppender
log4j.appender.TNC.File=C:\\Program Files\\THC\\TRDS2\\TNetCmd.log
log4j.appender.TNC.MaxFileSize=1024KB
log4j.appender.TNC.MaxBackupIndex=10
log4j.appender.TNC.layout=org.apache.log4j.PatternLayout
log4j.appender.TNC.layout.ConversionPattern= %p %d{%Y-%m-%d %H:%M:%S} %c %u - %m%n
log4j.appender.TMQ=org.apache.log4j.RollingFileAppender
log4j.appender.TMQ.File=C:\\Program Files\\THC\\TRDS2\\TMQSvr.log
log4j.appender.TMQ.MaxFileSize=1024KB
log4j.appender.TMQ.MaxBackupIndex=10
log4j.appender.TMQ.layout=org.apache.log4j.PatternLayout
log4j.appender.TMQ.layout.ConversionPattern= %p %d{%Y-%m-%d %H:%M:%S} %c %u - %m%n