zoukankan      html  css  js  c++  java
  • Poco logger 日志使用小析

    Poco logger 日志使用小析

    日志

    在软件开发过程中,为了定位软件运行过程中可能出现的错误,一种常用的做法是在潜在的错误位置,设置防御代码,并且将错误代码执行后的错误信息记录下来,以供后续改进代码提供支持。

    在日志记录的内容,为了定位错误,最基本的信息要求一下几点:

    • 日期
    • 时间
    • 文件位置
    • 触发错误代码行号
    • 详细的错误信息

    日志记录上述信息之后,基本确定错误的地点,从而有的放矢的实现对代码的重构和优化

    logger 库选择

    基于C++ 的logger 库有很多, glog , log4cplus , log4cpp, Poco::logger , 这些库都是不错的,都有着不错的背景和强劲的技术开发团队。glog背后站着的谷歌这样的巨头。而log4cplus 的干爹则是Java库,其设计思路基本沿袭的java 一贯的OOP 风格 。 鉴于笔者长期使用Poco 作为C++ 的基础库作为STL库 的拓展库使用,为了保持代码的一致性风格,我优先选择了Poco::logger 作为开发库

    Poco::logger 架构简析

    这里笔者不想过多的介绍Poco::logger 设计思路,这方面的内容官方比我讲的要好的多。 理解logger 库的最核心一点就是看懂logger库的层次结构,这种设计方式很好的实现解耦,并且层次结构分明,非常值得学习

    步骤一 生成消息

    生成消息

    步骤二 写入logger

    写入logger

    步骤三 导入channel

    导入channel

    步骤四 写文件

     写文件

    使用

    如何使用poco::logger , 我想最好的方式就是贴代码了。 通过代码的方式一目了然的理解如何poco::logger 。 依葫芦画瓢是作为一名软件开发者最基本的素质

    .h file

    #pragma once
    #include "Poco/Logger.h"
    using  Poco::Logger;                    // for global decorator  
    
    namespace Logger
    {
    #define logger_handle   (Logger::get("logger"))
    
        void Logger_initiation();  // initiation
        void Setup_logger();       // init only once 
    
    }

    .cpp file

    #include "StdAfx.h"
    #include "dante_logger.h"
    #include "Poco/PatternFormatter.h"
    #include "Poco/FormattingChannel.h"
    #include "Poco/FileChannel.h"
    #include "Poco/AutoPtr.h"
    using namespace Poco;
    
    namespace Logger
    {   
    
        void Logger_initiation()
        {
            AutoPtr<FileChannel> file_channel(new FileChannel());
            file_channel->setProperty("rotation" ,"10M");
            file_channel->setProperty("archive" ,"timestamp");
            file_channel->setProperty("path" ,"dante_route_log.log");
            AutoPtr<PatternFormatter> pattern_formatter (new PatternFormatter("%L%H:%M:%S-code line :%u-%U : %t"));
            AutoPtr<FormattingChannel> formatter_channle(new FormattingChannel(pattern_formatter , file_channel));
            Logger::root().setChannel(formatter_channle);           
            ///- finish logger initiation
        }
    
        void Setup_logger()
        {
            static bool b_setup =false;     // only allow run once time 
            if (! b_setup)
            {
                b_setup =true;  
                Logger_initiation();
            }
        }
    }

    main 入口函数

    using Logger;
    void Test_logger()
    {
        poco_information(logger_handle , "hello world,this is a 苍原狮啸's blog");
    }
    
    int main()
    {
        Setup_logger();
        Test_logger();
    }

    备注

    经过笔者测试,发现Poco::logger 不支持在类中完成 Logger 的初始化过程, 一旦在类中初始化logger ,软件就会自动抛出异常。因为logger 作为一个在整个过程中都是视为全局变量。

    拓展

    作为开发者,个人觉得最好的方式,就是连初始化的过程都能直接自动完成,最好是在加载文件的时候就直接完成,而不是需要使用者使用调用一次Setup_logger() 来完成相关的操作。 有更好的解决方案的读者欢迎分享您的理解和方案

    总结

    通过对Poco::logger简单的封装之后,就可以做到一处初始化后,到处使用了. 
    在需要使用的地点,只需添加头文件using namespace. 调用过程则全程使用宏,将所需要记录的消息记录下来即可。是不是觉很方便呢?

    转载请注明本文链接 :

    http://blog.csdn.net/leos_blog/article/details/44699255

  • 相关阅读:
    pandas去重方法
    原生表单组件
    html表单
    html表格基本标签
    文档和网站架构
    文本格式
    【Leetcode链表】奇偶链表(328)
    【Leetcode链表】移除链表元素(203)
    【Leetcode链表】旋转链表(61)
    【Leetcode链表】反转链表 II(92)
  • 原文地址:https://www.cnblogs.com/findumars/p/7638798.html
Copyright © 2011-2022 走看看