Encapsulation of C + + Log4cplus class libraries

Source: Internet
Author: User

Encapsulation of the Log4cplus library, modified from the network

LogUtils.h

/** LogUtils.h * * Created on:2018 August 9 * Author:oftenlin*/#ifndef Utils_logutils_h_#defineUtils_logutils_h_//Logutils.h:interface for the Logutils class.////////////////////////////////////////////////////////////////////////#include"log4cplus/loglevel.h"#include"log4cplus/ndc.h"#include"Log4cplus/logger.h"#include"Log4cplus/configurator.h"#include"Iomanip"#include"Log4cplus/fileappender.h"#include"log4cplus/layout.h"#include<log4cplus/loggingmacros.h>using namespaceLog4cplus;using namespacelog4cplus::helpers;#definePath_size 100//Log Encapsulation#defineTRACE (P) log4cplus_trace (Logutils::_logger, p)#defineDEBUG (P) log4cplus_debug (Logutils::_logger, p)#defineNOTICE (P) log4cplus_info (Logutils::_logger, p)#defineWARNING (P) log4cplus_warn (Logutils::_logger, p)#defineFATAL (P) log4cplus_error (Logutils::_logger, p)//log control class, global sharing a logclasslogutils{ Public:    //Open Log    BOOLOpen_log (); //get a log instance    Staticlogutils&instance (); StaticLogger _logger;Private: Logutils (); Virtual~logutils (); //log file path and name    Char_log_path[path_size]; Char_log_name[path_size];};#endif/* Utils_logutils_h_ */

LogUtils.cpp

/** LogUtils.cpp * * Created on:2018 August 9 * Author:oftenlin*///Log.cpp:implementation of the Log class.////////////////////////////////////////////////////////////////////////#include"LogUtils.h"#include<memory>////////////////////////////////////////////////////////////////////////construction/destruction//////////////////////////////////////////////////////////////////////Logger Logutils::_logger= Log4cplus::logger::getinstance ("Main_log"); Logutils::logutils () {snprintf (_log_path,sizeof(_log_path),"%s","./log"); snprintf (_log_name,sizeof(_log_name),"%s/%s.%s", _log_path,"app","Log");} Logutils::~logutils () {}logutils&logutils::instance () {Staticlogutils log; returnlog;}BOOLLogutils::open_log () {intLog_level =0; /*Step 1:instantiate an Appender object*/sharedappenderptr _append (NewFileappender (_log_name)); _append->setname ("File Log Test"); /*Step 2:instantiate A Layout object*/std::stringPattern ="[ %p] [%d{%m/%d/%y%h:%m:%s}] [%t]-%m%n"; Std::auto_ptr<Layout> _layout (Newpatternlayout (pattern));//std::auto_ptr<layout> pttclayout (New Ttcclayout ());    /*Step 3:attach The Layout object to the Appender*/_append-setlayout (_layout);//_append->setlayout (pttclayout);    /*Step 4:instantiate A Logger object*/    /*Step 5:attach The Appender object to the Logger*/Logutils::_logger.addappender (_append); /*Step 6:set A priority for the logger*/Logutils::_logger.setloglevel (Log_level); return true;}

Encapsulation of C + + Log4cplus class libraries

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.