log4cpp之Layout布局

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

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 spa

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

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

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
相关文章
相关标签/搜索