body, table{font-family: 微软雅黑; font-size: 10pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

首先回顾一下HelloWorld的日志格式,它使用了最简单的BasicLayout:

1248337987 ERROR  : Hello log4cpp in a Error Message!
1248337987 WARN : Hello log4cpp in a Warning Message!

上面的日志格式还可以,但显然不是许多程序员心中理想的格式,许多人理想的格式应该是这样的:

2009-07-24 15:59:55,703: INFO infoCategory : system is running
2009-07-24 15:59:55,703: WARN infoCategory : system has a warning
2009-07-24 15:59:55,703: ERROR infoCategory : system has a error, can't find a file
2009-07-24 15:59:55,718: FATAL infoCategory : system has a fatal error, must be shutdown
2009-07-24 15:59:55,718: INFO infoCategory : system shutdown, you can find some information in system log

要获得上面的格式,必须使用比BasicLayout复杂的PatternLayout,而且要花一个小时来熟悉一下PatternLayout的格式定义方式,如果你认为值得的话。

PatternLayout

  在介绍PatternLayout
以前,首先来看看log4cpp中所有的Layout子类(Layout本身是个虚类),一共三个:BasicLayout、PatternLayout
和SimpleLayout,其中SimapleLayout并不建议使用,而BaiscLayout过于简单,因此如果程序员不自己扩展Layout的话,就只能使用PatternLayout了,值得庆幸的是,PatternLayout还是比较好用的。

PatternLayout使用setConversionPattern函数来设置日志的输出格式。该函数的声明如下:

void log4cpp::PatternLayout::setConversionPattern  (  const std::string &  conversionPattern   )  throw (ConfigureFailure) [virtual]

其中参数类型为std::string,类似于C语言中的printf,使用格式化字符串来描述输出格式,其具体含义如下:

%c category;
%d 日期;日期可以进一步的设置格式,用花括号包围,例如%d{%H:%M:%S,%l} 或者 %d{%d %m %Y %H:%M:%S,%l}。如果不设置具体日期格式,则如下默认格式被使用“Wed Jan 02 02:03:55 1980”。日期的格式符号与ANSI C函数strftime中的一致。但增加了一个格式符号%l,表示毫秒,占三个十进制位。
%m 消息;
%n 换行符,会根据平台的不同而不同,但对于用户透明;
%p 优先级;
%r 自从layout被创建后的毫秒数;
%R 从1970年1月1日0时开始到目前为止的秒数;
%u 进程开始到目前为止的时钟周期数;
%x NDC。

因此,要得到上述的理想格式,可以将setConversionPattern的参数设置为“%d:
%p %c %x: %m%n”,其具体含义是“时间: 优先级 Category NDC: 消息 换行”。使用PatternLayout的例子程序如下,项目名称是LayoutExam:

#include <iostream>
#include <log4cpp/Category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Priority.hh>
using namespace std;
using namespace log4cpp;
int main(void)
{
        //指定日志输出目的地,只能对应一个Category
        OstreamAppender * pOsApender =
                new OstreamAppender("osApender", &cout);
        //格式化日志信息
        PatternLayout * ptnLyt = new PatternLayout();
        ptnLyt->setConversionPattern("%d: %p %c %x: %m%n");
        pOsApender->setLayout(ptnLyt);
        //负责输出日志,输出目的有Appender决定,可以指定多个Appender
        Category & root = Category::getRoot();
        Category & infoCat = root.getInstance("infoCat");
        infoCat.addAppender(pOsApender);
        infoCat.setPriority(Priority::INFO);
        //写日志
        infoCat.info("system is running!");
        infoCat.warn("system has a warning!");
        infoCat.error("system has an error, can't find file!");
        infoCat.fatal("system has an fatal error, must be shutdown!");
        infoCat.info("system shutdown,you can find information in system log");
        //关闭Category
        Category::shutdown();
        return 0;
}
#include<iostream>
#include<log4cpp/Category.hh>
#include<log4cpp/OstreamAppender.hh>
#include<log4cpp/Priority.hh>
#include<log4cpp/PatternLayout.hh>
using namespace std;
using namespace log4cpp;
int main(int argc,char* argv[])
{
        OstreamAppender* osAppender = new OstreamAppender("osAppender",&cout);
        PatternLayout* pLayout = new PatternLayout();
        pLayout->setConversionPattern("%d: %p %c %x: %m%n");  //%x 不写一样的输出结果
        //时间(默认格式) 优先级 Category NDC:消息 换行
        //NDC(嵌套的诊断上下文),%c 到时候就是输出下面的infoCategory
        osAppender->setLayout(pLayout);
        Category& root = Category::getRoot();
        //获取root下的一个实例,因为系统只有一个根,根下可以有多个Category,不能统一直接对root设置样式,除非所有日志记录都采用一样的样式
        Category& infoCategory = root.getInstance("infoCategory");
        infoCategory.addAppender(osAppender);
        infoCategory.setPriority(Priority::INFO);
        infoCategory.info("system is running");
        infoCategory.warn("system has a warning");
        infoCategory.error("system has a error,can't find a file");
        infoCategory.fatal("system has a fatal error,must be shutdown");
        infoCategory.info("system shutdown,you can find some information in system log");
        Category::shutdown();
        return 0;
}

其运行结果即如下所示:

2017-12-24 17:33:56,230: INFO infoCategory : system is running
2017-12-24 17:33:56,230: WARN infoCategory : system has a warning
2017-12-24 17:33:56,230: ERROR infoCategory : system has a error,can't find a file
2017-12-24 17:33:56,230: FATAL infoCategory : system has a fatal error,must be shutdown
2017-12-24 17:33:56,230: INFO infoCategory : system shutdown,you can find some information in system log

log4cpp之Layout布局的更多相关文章

  1. 新建android工程的时候eclipse没有生成MainActivity和layout布局

    一.新建android工程的时候eclipse没有生成MainActivity和layout布局 最近由于工作上的原因,开始学习Android开发,在入门的时候就遇到了不少的坑,遇到的第一个坑就是&q ...

  2. layout布局实例化

    实例化xml中的Layout布局在开发中经常会用到,有几种方法可以使用 1.在Activity中使用getLayoutInflater()方法 View layout = getLayoutInfla ...

  3. ASP.NET MVC3 系列教程 – 新的Layout布局系统

    原文地址:http://www.cnblogs.com/highend/archive/2011/04/18/asp_net_mvc3_layout.html I:回忆MVC2当中MasterPage ...

  4. Android 布局学习之——Layout(布局)具体解释二(常见布局和布局參数)

     [Android布局学习系列]   1.Android 布局学习之--Layout(布局)具体解释一   2.Android 布局学习之--Layout(布局)具体解释二(常见布局和布局參数)   ...

  5. 一天搞定CSS:支持IE的Layout布局--16

    1.BFC和Layout区别: BFC和Layout的作用是一样的,只是对浏览器的支持不同而已. BFC- -标准浏览器所具有的 Layout- -IE浏览器所具有的 BFC详解地址:http://b ...

  6. 解决thymeleaf layout布局不生效

    今天使用thymeleaf layout布局时总是不生效,特此把解决问题的步骤和几个关键点记录下来备忘. 一.检查依赖 1.thymeleaf必备maven依赖: <dependency> ...

  7. easyui中datagrid+layout布局

    1.掌握layout布局 首先,layout布局的具体使用可参考官网http://www.jeasyui.net/plugins/162.html layout布局分为东南西北中五个区域,如图我们将其 ...

  8. Springboot 使用thymeleaf模板layout布局

    使用layout布局前应该在pom文件中导入thymeleaf(dialect)依赖:如下 <properties> <project.build.sourceEncoding> ...

  9. easyui layout布局的属性说明

    layout布局的属性说明: 名称 类型 描述 默认值 fit boolean 当设置为 true 时,就设置布局(layout)的尺寸适应它的父容器.当在 'body' 标签上创建布局(layout ...

随机推荐

  1. 如何建立DB2分区数据库?(转)

    欢迎和大家交流技术相关问题:邮箱: jiangxinnju@163.com博客园地址: http://www.cnblogs.com/jiangxinnjuGitHub地址: https://gith ...

  2. iis日志时间与本地日期不一样

    iis日志里面的时间是 UTC时间,所以要自已加时区进行转换:即 utc时间+8小时:

  3. feather mac 问题小结

    feater 依赖php及jdk 1.自带的php没有cgi ,索性直接装个新的 修改环境变量,并使其生效,验证方式是 打印版本信息: php -v PHP 7.1.13 (cli) (built: ...

  4. 20145312 实验五 《Java网络编程》

    20145312 实验五<Java网络编程> 一. 实验内容及要求 实验内容: 运行下载的TCP代码,结对进行,一人服务器,一人客户端: 利用加解密代码包,编译运行代码,一人加密,一人解密 ...

  5. php实现dota天梯、wow竞技场、lol排位赛匹配加分算法ELO

    public function marchOpponents() { $rstep = Yii::$app->params['ratingStep'];//(随机范围) $rsN=100; $d ...

  6. [转载]Javassist 使用指南(三)

    ======================= 本文转载自简书,感谢原作者!. 原链接如下:https://www.jianshu.com/p/7803ffcc81c8 =============== ...

  7. bzoj1879: [Sdoi2009]Bill的挑战(codevs2308)(luoguP2167) 状压dp

    唔...懒兔子来写博客了... 点我看题 这题的话...我想了很久但是都不是可行解 刚开始想预处理任意两个串是否可以匹配然后在乱搞,后来发现完全不会写... 然后按照惯例,我会看题解认真的思考... ...

  8. 【bzoj2563】阿狸和桃子的游戏(贪心+构造)

    题目传送门:bzoj2563 先膜拜一波PoPoQQQ的题解:BZOJ 2563 阿狸和桃子的游戏 贪心 其实我们可以这样看:把一条边的权值均分到两个端点,那么取到两个端点就能得到这条边的边权,如果只 ...

  9. docker 修改 mysql 5.7 sql_mode

    docker exec -ti {容器ID} /bin/bash   进入容器 apt-get install vim 安装vim 找到 vim /etc/mysql/my.cnf 在 [mysqld ...

  10. 爬虫之urllib2库的基本使用

    urllib2库的基本使用 所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地. 在Python中有很多库可以用来抓取网页,我们先学习urllib2. urllib2 是 P ...