A common encapsulation example of Log4cplus

Source: Internet
Author: User
, VLog.h #pragma once #ifndef vlog_h #define VLOG_H #include <log4cplus/logger.h> #include <log4cplus/layout.h&
Gt #include <log4cplus/loglevel.h> #include <log4cplus/fileappender.h> #include <log4cplus/
Consoleappender.h> #include <log4cplus/helpers/loglog.h> using namespace Log4cplus;

using namespace helpers; /** * Use Log4cplus * @param logfile record file path, such as "Main.log" * @param format prefix, default%d [%l]%-5p:%m%n * @param bebug whether to play  Print bebug information, default true * @param LV set Journal level * @return/void Initlog4cplus (const wchar_t* logfile, const BOOL Console = true, const bool Bebug = True, loglevel LV = debug_log_level, const wchar_t* format = L

"%d:%m%n"/*%d%-5p [%c <%l]:%m%n*/);

/** * Get root log * @return * * * * Logger getrootlogger (void);

/** * Get Sub log * @param child log name, e.g., SUB;SUB.SUB1 * @return * * * Logger Getsublogger (const wchar_t* sub);

/** * Shut down the log system/void Shutdownlogger (void); #endif/* Vlog_h*/ 


VLog.cpp

/************************************************************************//* Usage rules: */* Need log4cplus Dynamic/static link library and header file////////////* Log4cplus.lib + LOG4CPLUS.D ll is a dynamic link library. Log4cplusS.lib for static link library * */************************************************************************/#include " VLog.h "void Initlog4cplus" (const wchar_t* logfile, const BOOL console, const BOOL Bebug, loglevel LV, const WCHAR    _t* format)/* Not_set_log_level (-1): Accepts the default LogLevel, if a parent logger inherits its loglevelall_log_level ( 0): Open all LOG information output trace_log_level (0): Open TRACE information output (ie all_log_level) debug_log_level (100 00): Open Debug Information Output info_log_level (20000): Open info Information output warn_log_level (30000): Open warning information loss                   Out Error_log_level (40000): Open ERROR Information output fatal_log_level (50000): Open FATAL Information Output off_log_level (60000): Turn off all log information output/{//0. Log System configuration: Set display debug Information Loglog::getloglog ()->setinternaldebugging (bebug); Create screen output Appender, do not use Stderror stream, do not use immediate write mode sharedappenderptr pappender1 (new Rollingfileappender (logfile, 1024 * 1024 * 2
    , false)); Generated log file name, file maximum (min 1 M), expand 50 files, do not use immediately write mode/2. Instantiate a Layout object//2.1 Create layout layout format Std::auto_ptr<lay

    Out> playout1 (new Patternlayout (format));
    3. Bind the Layout object (attach) to the Appender object//Pappender.setlayout (std::auto_ptr<layout> Layout);

    Pappender1->setlayout (PLAYOUT1);
    4.Logger: Recorder, the entity that saves and tracks changes in object log information, and when you need to record an object, you need to generate a Logger.

    Logger Rootlogger = Logger::getroot ();

    5. Bind the Appender object (attach) to the Logger object, such as omitting this step, and the standard output (screen) Appender object is bound to logger rootlogger.addappender (pappender1);
        if (console) {//1.Appenders: A hook that works closely with the layout to output messages of a particular format to the attached device terminals (such as screens, files, and so on).
        Sharedappenderptr Pappender2 (New Consoleappender (False, false)); Std::auto_ptr<layout> playouT2 (new Patternlayout (format));
        Pappender2->setlayout (PLAYOUT2);
    Rootlogger.addappender (PAPPENDER2);
//6. Set the priority of the logger, such as omitting this step, a variety of finite-level messages will be recorded rootlogger.setloglevel (LV);

} Logger Getrootlogger (void) {return logger::getroot ();}

Logger Getsublogger (const wchar_t* sub) {return logger::getinstance (sub);} void Shutdownlogger (void) {Logger::getroot (). shutdown ();}

<p> #include "VLog.h" </p><p>int _tmain (int argc, _tchar* argv[])
{
&nbsp;//joins Log4cplus
&nbsp;//initialization Log4cplus
&nbsp;initlog4cplus (L "Main.log"); </p><p>&nbsp;//Log object: Test a
&nbsp;log4cplus::logger test1 = Getsublogger (L "test1");
&nbsp;//Log object: Test a first Test
&nbsp;log4cplus::logger test1_01 = Getsublogger (L "test1.01"); </p><p>&nbsp;//type logging
&nbsp; Log4cplus_debug (test1_01, "You can log as this:" << "Hello" << m << '! '); </p><p>&nbsp;//Log Off
&nbsp; Shutdownlogger ();
}
</p>


from:http://blog.csdn.net/eclipser1987/article/details/6904301

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.