博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
log4cxx 使用代码进行配置
阅读量:5344 次
发布时间:2019-06-15

本文共 4700 字,大约阅读时间需要 15 分钟。

(1)官网的一个例子

  • #include 
    #include
    #include
    #include
    #include
    int main() { log4cxx::FileAppender * fileAppender = newlog4cxx::FileAppender(log4cxx::LayoutPtr(new log4cxx::SimpleLayout()),"logfile", false); log4cxx::helpers::Pool p; fileAppender->activateOptions(p); log4cxx::BasicConfigurator::configure(log4cxx::AppenderPtr(fileAppender)); log4cxx::Logger::getRootLogger()->setLevel(log4cxx::Level::getDebug()); log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger("logger"); LOG4CXX_INFO(logger,"Created FileAppender appender"); return 0;}

(2)一个只使用代码的例子dailyrollingfileappender(经过验证)

 

ostringstream oss;  oss << getpid();  string pid= oss.str();  MDC::put("pid", oss.str()); //使得layout中可以携带进程号信息 PatternLayoutPtr layout = new PatternLayout();        string conversionPattern = "[%p] %d %c %M - %m%n";        layout->setConversionPattern(conversionPattern);        // creates daily rolling file appender        DailyRollingFileAppenderPtr rollingAppenderptr = new DailyRollingFileAppender();        rollingAppenderptr->setFile("/opt/bre/LrSortService/log/LrSortService.log."+pid);        rollingAppenderptr->setDatePattern("'.'yyyy-MM-dd");        rollingAppenderptr->setLayout(layout);        log4cxx::helpers::Pool p;        rollingAppenderptr->activateOptions(p);        // configures the root logger        log4cxx::LoggerPtr logger = log4cxx::Logger::getRootLogger();        logger->setLevel(log4cxx::Level::getDebug());        logger->addAppender(rollingAppenderptr);        LOG4CXX_INFO(logger,"Created FileAppender appender");
(3)一个例子。使用RollingFileAppender和
TimeBasedRollingPolicy(未经验证)

private static void log4jConfig(String actionName) {
org.apache.log4j.Logger rootLogger = LogManager.getRootLogger(); RollingFileAppender fileAppender = (RollingFileAppender)rootLogger.getAppender("fileAppender"); // String currentLogFile = fileAppender.getFile(); String newLogPattern = currentLogFile.replace(LOG4J_ROLLING_FILE_NAME_TOKEN, actionName); fileAppender.setFile(newLogPattern); TimeBasedRollingPolicy timeBasedRollingPolicy = (TimeBasedRollingPolicy) fileAppender.getRollingPolicy(); String fileNamePattern = timeBasedRollingPolicy.getFileNamePattern(); String newFileNamePattern = fileNamePattern.replace(LOG4J_ROLLING_FILE_NAME_TOKEN, actionName);; timeBasedRollingPolicy.setFileNamePattern(newFileNamePattern); timeBasedRollingPolicy.activateOptions(); fileAppender.activateOptions(); LOG.info(" Redirected launcher log output to log pattern: " + newFileNamePattern);}
(4)一个使用xml配置文件和代码协同工作的例子。(有问题)

参考:http://stackoverflow.com/questions/29769018/log4cxx-timebasedrollingpolicy-wont-keep-old-log-when-used-programtically

i'm trying use log4cxx combined with xml file and code in multiprocess, hope each process create a log and can rollback in TimeBasedRollingPolicy. here is my xml file:

then my code:

void LrSortService::InitLog(const std::string& logconf) {  ostringstream oss;  oss << getpid();  MDC::put("pid", oss.str());  DOMConfigurator::configure(logconf);  LoggerPtr logger(log4cxx::Logger::getRootLogger());  FileAppenderPtr oldappenderptr;  oldappenderptr = logger->getAppender("bfd_recommend_appender");    string oldfile = oldappenderptr->getFile();    string pid = oss.str();    string newfile1 = oldfile + ".";    string newfile  = newfile1 + pid;    oldappenderptr->setFile(newfile);    log4cxx::helpers::Pool p;    oldappenderptr->activateOptions(p);  cout<<"set log OK , log filename is: "<
<

what i got:

-rw-rw-r-- 1 bre bre  1809500 Apr 20 23:59 LrSortService.log.20150420-rw-rw-r-- 1 bre bre 26807477 Apr 21 17:37 LrSortService.log.20150420.2297-rw-rw-r-- 1 bre bre 23429289 Apr 21 17:37 LrSortService.log.20150420.2374-rw-rw-r-- 1 bre bre 23546280 Apr 21 17:37 LrSortService.log.20150420.2451-rw-rw-r-- 1 bre bre 22566813 Apr 21 17:37 LrSortService.log.20150420.2528

and yesterday i got:

LrSortService.log.20150420.2297    LrSortService.log.20150420.2374    LrSortService.log.20150420.2451    LrSortService.log.20150420.2528

what i meant to get is (today is 20150421):

LrSortService.log.20150420.2297    LrSortService.log.20150420.2374    LrSortService.log.20150420.2451    LrSortService.log.20150420.2528    LrSortService.log.20150421.2297    LrSortService.log.20150421.2374    LrSortService.log.20150421.2451    LrSortService.log.20150421.2528

any help will be appreciated ,thanks !

转载于:https://www.cnblogs.com/catkins/p/5270369.html

你可能感兴趣的文章
MaiN
查看>>
[Python学习] 简单网络爬虫抓取博客文章及思想介绍
查看>>
触发器课程SQL Server 知识梳理九 触发器的使用
查看>>
信息浏览器从Android的浏览器中传递cookie数据到App中信息浏览器
查看>>
客户端连接linux虚拟机集群报错
查看>>
linux下部署一个JavaEE项目的简单步骤
查看>>
hash储存机制
查看>>
[Android学习系列16]Android把php输出的json加载到listview
查看>>
20145205 《信息安全系统设计基础》第14周学习总结
查看>>
6)添加一个窗口的图标
查看>>
POJ - 1422 Air Raid 二分图最大匹配
查看>>
Road Map
查看>>
正则替换中的一个Bug
查看>>
HI3531uboot开机画面 分类: arm-linux-Ubunt...
查看>>
制作U盘启动CDLinux 分类: 生活百科 ...
查看>>
strcpy函数里的小九九
查看>>
搭建ssm过程中遇到的问题集
查看>>
OpenLayers绘制图形
查看>>
tp5集合h5 wap和公众号支付
查看>>
Flutter学习笔记(一)
查看>>