Poco logger logs use a small analysis

Source: Internet
Author: User

Poco logger logs use a small analysis

    • Poco logger logs use a small analysis
    • Log
    • Logger Library Selection
    • A brief analysis of Pocologger architecture
      • Step life into News
      • Step Two Write Logger
      • Step three import Channel
      • Step four Write file
    • Use
      • H file
      • CPP file
      • Main entry function
    • Note
    • Expand
    • Summarize
        • Reprint please indicate this article link

Log

In the process of software development, in order to locate errors that may occur during the running of the software, a common practice is to set up the defensive code in the potential error location, and to record the error message after the error code execution to provide support for subsequent improvement code.

In the logging content, in order to locate the error, the most basic information requires a few points:

  • Date
  • Time
  • File location
  • Trigger error code line number
  • Detailed error message

After logging the above information, the basic determination of the wrong location, so that the targeted implementation of code refactoring and optimization

Logger Library Selection

C + + based logger library There are many, Glog, Log4cplus, Log4cpp, Poco::logger , these libraries are good, have a good background and strong technical development team. Glog is standing behind the giant like Google. and Log4cplus 's father-in-the-Java library, the design of the basic idea of the Java consistent OOP style. In view of the long-term use of Poco as a C + + base library as an extension of the STL Library to use, in order to maintain the consistency of code style, I preferred Poco::logger as a development library

A brief analysis of Poco::logger architecture

Here I do not want to introduce too much poco::logger design ideas, this aspect of the content of the official than I speak much better. Understanding the core of the logger library is to understand the hierarchical structure of the logger library, this design method is very good decoupling, and hierarchical structure, it is worth learning

Step life into News

Step Two Write Logger

Step three import Channel

Step four Write file

Use

How to use Poco::logger, I think the best way is to paste the code. The way the code is understood at a glance is how poco::logger. The gourd is the most basic quality as a software developer.

. 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{voidLogger_initiation () {autoptr<FileChannel>File_channel (NewFileChannel ()); File_channel -SetProperty ("Rotation","10M"); File_channel -SetProperty ("Archive","Timestamp"); File_channel -SetProperty ("Path","Dante_route_log.log"); Autoptr<Patternformatter>Pattern_formatter (NewPatternformatter ("%l%h:%m:%s-code line:%u-%u:%t")); Autoptr<Formattingchannel>Formatter_channle (NewFormattingchannel (Pattern_formatter, File_channel)); Logger:: Root().Setchannel (Formatter_channle);///-Finish Logger Initiation}voidSetup_logger () {static bool B_setup=false;//Only allow run once time        if(!B_setup) {B_setup=true;        Logger_initiation (); }    }}
Main entry function
Test_logger(){    poco_information(logger_handle , "hello world,this is a 苍原狮啸‘s blog")main(){    Setup_logger();    Test_logger();}
Note

After the author tests, found that Poco::logger does not support the completion of the logger initialization process in the class, once the logger is initialized in the class, the software will automatically throw an exception. Because logger as a whole is considered a global variable throughout the process.

Expand

As a developer, the best way for the individual to feel is that even the initialization process can be done directly, preferably when the file is loaded, rather than requiring the user to use the call once Setup_logger () to complete the relevant operation. Readers with better solutions welcome to share your understanding and solutions

Summarize

After a simple encapsulation of the Poco::logger , it is possible to initialize it and use it everywhere .
In the location you need to use, simply add the header file and the using namespace. The calling procedure uses macros throughout the entire process, recording the messages needed for logging. is it convenient to sleep?

Reprint please indicate this article link:

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

Poco logger logs use a small analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.