log4cxx日志记录
来源:互联网 发布:自动扫矿软件 编辑:程序博客网 时间:2024/04/30 11:12
写了一个最简单的关于日志记录的程序,该示例在功能上创建了一个日志服务,该日志可通过配置文件进行必要控制,并可以同时向文件和控制台输出信息,但是还有一点小问题没解决。
#include <string>
#include <iostream>
#include <log4cxx/logger.h>
#include <log4cxx/basicconfigurator.h>
#include <log4cxx/helpers/exception.h>
#include <log4cxx/propertyconfigurator.h>
using namespace std;
using namespace log4cxx;
class CLogInFile
{public:
static LoggerPtr logger_Parse;
static LoggerPtr logger_Record;
};
LoggerPtr CLogInFile::logger_Parse=Logger::getLogger("myparse.record");
LoggerPtr CLogInFile::logger_Record=Logger::getLogger("mypcap.record");
int main(int argc,char* argv[])
{
string msg=_T("log_multi.properties");
PropertyConfigurator::configure(msg);
//日志记录级别在in log_multi.properties中已设定: logger_Parse -- WARN logger_Record –INFO
//只有日志请求的级别不小于已设定的级别时才会记录日志
//TRACE < DEBUG < INFO < WARN < ERROR < FATAL
//请求级别DEBUG <设定级别WARN下面第一个不会被记录
LOG4CXX_DEBUG(CLogInFile::logger_Parse,"Parselog: debug information");
LOG4CXX_ERROR(CLogInFile::logger_Parse,"Parselog: error information");//ERROE>WARN
LOG4CXX_INFO(CLogInFile::logger_Record,"Pcaplog: World");
LOG4CXX_WARN(CLogInFile::logger_Parse,"Parselog: warn information");
return 0;
}
log_multi.properties中内容如下:
log4j.rootLogger=TRACE,stdout,R
log4j.category.mypcap.record=INFO,stdout,R
log4j.category.myparse.record=WARN,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%-5p],[%F:%L]-%m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=out.log
log4j.appender.R.MaxFileSize=3000KB
log4j.appender.R.MaxBackupIndex=5
log4j.appender.R.layout=org.apache.log4j.PatternLayout
运行该程序如下:
[n@myname 1]$ g++ -c log_multi.cpp -o log_multi.o
[n@myname 1]$ g++ log_multi.o -o log_multi -llog4cxx
[n@myname 1]$ ./log_multi
2008-05-19 16:59:43,409 [ERROR],[log_multi.cpp:34]-Parselog: error information
2008-05-19 16:59:43,409 [ERROR],[log_multi.cpp:34]-Parselog: error information
2008-05-19 16:59:43,409 [INFO ],[log_multi.cpp:35]-Pcaplog: World
2008-05-19 16:59:43,409 [INFO ],[log_multi.cpp:35]-Pcaplog: World
2008-05-19 16:59:43,410 [WARN ],[log_multi.cpp:36]-Parselog: warn information
2008-05-19 16:59:43,410 [WARN ],[log_multi.cpp:36]-Parselog: warn information
[n@myname 1]$ vim out.log
可看到out.log中也有相同的日志记录,该文件是在程序执行的过程中被创建的。
日志被输出了两遍,不知道什么原因,希望哪位高手指点一下,不胜感激。
- log4cxx日志记录
- 在C++中使用Apache的Log4cxx记录日志
- 在C++中使用Apache的Log4cxx记录日志
- 在C++中使用Apache的Log4cxx记录日志
- 在C++中使用Apache的Log4cxx记录日志
- 在C++中使用Apache的Log4cxx记录日志(转帖)
- 日志操作利器log4cxx
- log4cxx日志工程引用
- 日志操作利器log4cxx 分享
- 日志操作利器log4cxx 分享
- 日志操作利器log4cxx 分享
- 多进程log4cxx区分日志
- 日志操作利器log4cxx 分享
- 日志操作利器log4cxx 分享
- log4cxx配置文件(log4cxx.properties文件)详解 --调整日志输出级别
- Log4cxx
- Log4cxx
- LOG4CXX
- Route66推荐速度设置
- 汶川8.0级强震 已造成全国40075人死亡
- 中日甲午战争期间的檄文
- MS SQL Server中的CONVERT日期格式化大全
- 安装新硬盘
- log4cxx日志记录
- 讨武瞾檄
- JS: window.open and window.showModelessDialog
- JS:常用正則表達式
- 讨曹操檄
- .NET 关于区别TimeOut后迁移到TimeOut画面与直接通过URL迁移到其他画面(没通过login画面)的解决方案
- ASP:Export to Excel (1)
- 女性一生应当拥有的20种品质
- 百度之星程序设计大赛试题