Use the latest log4cplus (1.1.1) to output multiple custom log files, isolating different log file outputs

Source: Internet
Author: User

Part of the reference to the blog.

Http://www.cppblog.com/tx7do/articles/11719.html


Filter log information based on script configuration
In addition to the configuration of the log environment through the program, Log4cplus implements the scripting-based functionality through the Propertyconfigurator class. Pass
The script can complete the configuration of logger, appender and layout, so it can solve the problem of how to output and where to go, I will end up in the full text
In part, it mentions how to use script configuration to implement performance test in multithreaded environment, this section will focus on the function of footings to filter log information.

Let's start with a brief introduction to the syntax rules for scripting:
Includes Appender configuration syntax and logger configuration syntax, where:

Configuration syntax for 1.Appender:
1.1 Setting Name:
/* Set Method */log4cplus.appender.appendername=fully.qualified.name.of.appender.class
For example (enumerated all possible appender, of which socketappender is not used here):
Log4cplus.appender.append_1=log4cplus::consoleappender
Log4cplus.appender.append_2=log4cplus::fileappender
Log4cplus.appender.append_3=log4cplus::rollingfileappender
Log4cplus.appender.append_4=log4cplus::D Ailyrollingfileappender
Log4cplus.appender.append_4=log4cplus::socketappender

1.2. Set filter:
includes selecting filters and setting filter conditions, selectable filters include: Loglevelmatchfilter, Loglevelrangefilter, and Stringmatchfilter:
For loglevelmatchfilter, filtering conditions include Logleveltomatch and Acceptonmatch (True|false), and only if the LogLevel value of log information is the same as Logleveltomatch, And Acceptonmatch is true to match.
Loglevelrangefilter, filtering conditions include loglevelmin, Loglevelmax, and Acceptonmatch, only when log information is loglevel in Loglevelmin, Matching occurs when the Loglevelmax is Acceptonmatch to true.
for stringmatchfilter, filtering conditions include Stringtomatch and acceptonmatch, and only if the loglevel value of the log information corresponds to the Stringtomatch value of the same, and will match if Acceptonmatch is true. The
     filtering condition processing mechanism is similar to the responsibility chain of iptable, (that is, first deny, then allow) but the order of execution is just the opposite, and the post-write condition is executed first, for example:
Log4cplus.appender.append_1.filters.1=log4cplus::spi::loglevelmatchfilterlog4cplus.appender.append_1. filters.1.logleveltomatch=tracelog4cplus.appender.append_1.filters.1.acceptonmatch=true# LOG4CPLUS.APPENDER.APPEND_1.FILTERS.2=LOG4CPLUS::SPI::D enyallfilter
The filters.2 filter condition is executed first, all filters are turned off, and the filters.1 is executed to match only trace information.

1.3. Set layout
You can choose not to set, ttcclayout, or Patternlayout
If not set, the log information in a simple format is output.
The settings ttcclayout are as follows: Log4cplus.appender.all_msgs.layout=log4cplus::ttcclayout
The settings patternlayout are as follows: Log4cplus.appender.append_1.layout=log4cplus::P atternLayoutlog4cplus.appender.append_1. Layout. Conversionpattern=%d{%m/%d/%y%h:%m:%s,%q} [%t]%-5p-%m%n

Configuration syntax for 2.logger
The same logger under the Appender will output content to the logger under all the files, can be pulled through loglevel and other measures pull filtering.
The following shows a way to isolate the output from different logger.
including Rootlogger and Non-root logger.
For Rootlogger: Log4cplus.rootlogger=[loglevel], Appendername, Appendername, ...
For Non-root logger: log4cplus.logger.logger_name=[loglevel| Inherited], Appendername, Appendername, ...
Scripting is easy to use, as long as the configuration is loaded first (Urconfig.properties is a custom profile):
Propertyconfigurator::d oconfigure ("urconfig.properties"); Let's take a look at the log4cplus powerful script-based filtering log information.


The following is the VS2012 WIN32 console Project Log4cplus_test, which is used to demonstrate log output,
Project need to pay attention to two points:
1. Using the latest log4cplus-1.1.1 version, the link is the static library Log4cplusSD.lib
2. The project needs to set the character set to "use multibyte Character set", set by the VS2012 menu:
Project->log4cplus_test Properties-> Configuration Properties-> Character Set


The following is the contents of the configuration file urconfig.properties, using configuration to control the log output of Log4cplus.

#全局默认根 logger, ignoring here.
#log4cplus. Rootlogger=trace, All_msgs, Trace_msgs, Debug_info_msgs, fatal_msgs

#log4cplus. rootlogger=trace,all_msgs
#log4cplus. Appender.all_msgs=log4cplus::rollingfileappender
#log4cplus. Appender.all_msgs. File=./logout/all_msgs.log
#log4cplus. appender.all_msgs.layout=log4cplus::ttcclayout


#独立的 logger configuration syntax, support for two Appender
Log4cplus.logger.APPfilelogger = trace,app,app_daily
Og4cplus.additivity.APPfilelogger = False


#独立的 logger configuration syntax, the same logger is sent to all files,
#是否写入到所有文件, controlled by LogLevel.
Log4cplus.logger.SYSfilelogger = Trace,sys
#log4cplus. Additivity.sysfilelogger = TRUE

Configuration syntax for #独立的 logger
Log4cplus.logger.ACCfilelogger = TRACE,ACC
#log4cplus. Additivity.accfilelogger = TRUE


#支持只写入同一个 the specified file under Logger
Log4cplus.appender.app=log4cplus::rollingfileappender
Log4cplus.appender.app.file=./logout/app_msgs.log
Log4cplus.appender.app.immediateflush=false
Log4cplus.appender.app.maxfilesize=1mb
#log4cplus. appender.app.minfilesize=1m
Log4cplus.appender.app.maxbackupindex=3
Log4cplus.appender.app.layout=log4cplus::P atternlayout
log4cplus.appender.app.layout.conversionpattern=%d{%y-%m-%d%h:%m:%s.%q}|%-5p|%c[2]|%t|%f:%l|%m%n
Log4cplus.appender.app.filters.1=log4cplus::spi::loglevelrangefilter
Log4cplus.appender.app.filters.1.loglevelmin=trace
Log4cplus.appender.app.filters.1.loglevelmax=fatal


#支持只写入同一个 The specified file under Logger
Log4cplus.appender.app_daily=log4cplus::D ailyrollingfileappender
Log4cplus.appender.APP_DAILY. File=./logout/app_msgs_d.log
#MONTHLY, weekly,daily,twice_daily,hourly,minutely
Log4cplus.appender.APP_ Daily. Schedule=minutely
Log4cplus.appender.APP_DAILY. Datepattern= '. ' YYYY-MM-DD
Log4cplus.appender.APP_DAILY. Immediateflush=false
Log4cplus.appender.APP_DAILY. Maxbackupindex=3
Log4cplus.appender.app_daily.layout=log4cplus::P atternlayout
Log4cplus.appender.APP_ daily.layout.conversionpattern=%d{%y-%m-%d%h:%m:%s.%q}|%-5p|%c[2]|%t|%f:%l|%m%n
Log4cplus.appender.APP_ Daily.filters.1=log4cplus::spi::loglevelrangefilter
Log4cplus.appender.app_daily.filters.1.loglevelmin= Tarce
Log4cplus.appender.app_daily.filters.1.loglevelmax=fatal


#支持只写入同一个 The specified file
Log4cplus.appender.sys=log4cplus::rollingfileappender
under Logger Log4cplus.appender.sys.file=./logout/sys_msgs.log
LOG4CPLUS.APPENDER.SYS.MAXFILESIZE=1MB
Log4cplus.appender.sys.maxbackupindex=3
Log4cplus.appender.sys.immediateflush=false
Log4cplus.appender.sys.layout=log4cplus::P atternlayout
log4cplus.appender.sys.layout.conversionpattern=%d{% y-%m-%d%h:%m:%s.%q}|%-5p|%c[2]|%t|%f:%l|%m%n
Log4cplus.appender.sys.filters.1=log4cplus::spi:: Loglevelrangefilter
Log4cplus.appender.sys.filters.1.loglevelmin=trace
Log4cplus.appender.sys.filters.1.loglevelmax=fatal

#支持只写入同一个 the specified file under Logger
Log4cplus.appender.acc=log4cplus::rollingfileappender
Log4cplus.appender.acc.file=./logout/acc_msgs.log
Log4cplus.appender.acc.maxfilesize=1mb
Log4cplus.appender.acc.maxbackupindex=3
Log4cplus.appender.acc.immediateflush=false
Log4cplus.appender.acc.layout=log4cplus::P atternlayout
log4cplus.appender.acc.layout.conversionpattern=%d{%y-%m-%d%h:%m:%s.%q}|%-5p|%c[2]|%t|%f:%l|%m%n
Log4cplus.appender.acc.filters.1=log4cplus::spi::loglevelrangefilter
Log4cplus.appender.acc.filters.1.loglevelmin=trace
Log4cplus.appender.acc.filters.1.loglevelmax=fatal


The main code file is Log4cplus_test.cpp, and the code is as follows:

Log4cplus_test.cpp: Defines the entry point for a console application.
//

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <xmemory>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/appender.h>
#include <log4cplus/logger.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/configurator.h>
#include <log4cplus/helpers/property.h>

using namespace Std;
using namespace Log4cplus;

APP and app_daily have shared a logger configuration
static Logger APP = Logger::getinstance (Log4cplus_text ("Appfilelogger.app"));
static Logger app_daily = Logger::getinstance (Log4cplus_text ("appfilelogger.app_daily"));

The SYS,ACC is a separate logger configuration so that Sys,acc,app (app_daily) belong to different logger
They can output files independently, without interfering with each other.
static Logger SYS = Logger::getinstance (Log4cplus_text ("Sysfilelogger.sys"));
static Logger ACC = Logger::getinstance (Log4cplus_text ("ACCFILELOGGER.ACC"));

#define VERTICAL "|"
#define Filelocal __file__<<vertical<<__line__<<vertical<<__function__<<vertical
#define FILELOCALNULL vertical<<vertical<<vertical

void Printdebug ()
{   
 log4cplus_debug (ACC, Log4cplus_text (filelocal) << "this is a ACC message "<<" |0| req|172.13.12.35|3456|845|0x 0d 0x ");
 log4cplus_debug (ACC, Log4cplus_text (filelocal) << "This is an ACC message" << "|150| resp|172.13.12.35|3456|345|0x 0d 0x 0A ");
 log4cplus_error (ACC, Log4cplus_text (filelocal) << ("This is an ACC message") << "|0| connect_a|172.13.12.99|8888| | ");
 log4cplus_info (ACC, Log4cplus_text (filelocalnull) << ("This is an ACC message") << "|0| req_a_a1|172.13.12.99|8888|234|0x 0d 0x 0A ");


Log4cplus_info (App,log4cplus_text (filelocalnull) << "This is a APP message" << | Orderid_2013090412121200001 ");
Log4cplus_debug (App,log4cplus_text (filelocal) << "This is a APP message" << | Msgid_2013090412121200002 ");
Log4cplus_error (App,log4cplus_text (filelocal) << "This is a APP message" << | Msgid_2013090412121200003 ");
Log4cplus_debug (App_daily,log4cplus_text (filelocal) << "This is a APP message" << | Msgid_2013090412121200006 ");


Log4cplus_info (Sys,log4cplus_text (filelocalnull) << "This are a SYS message");
Log4cplus_debug (Sys,log4cplus_text (filelocal) << "This are a SYS message");
Log4cplus_error (Sys,log4cplus_text (filelocal) << "This are a SYS message");
Log4cplus_debug (Sys,log4cplus_text (filelocal) << "This are a SYS message");

}


void Ta ()
{
 log4cplus::D ailyrollingfileappender *_append;
 _append = new Dailyrollingfileappender ("./logout/dd.log", minutely,false,5);
 _append->setname ("File test");
 _append->setlayout (std::auto_ptr<ttcclayout> (New Ttcclayout ()));
 logger::getroot (). Addappender (Log4cplus::sharedappenderptr (_append));
 logger test = logger::getinstance ("test");
 for (int i = 0; i < 10000000 ++i)
 {
  log4cplus_trace (Test, "::p Rintdebug ()");
& Nbsp; log4cplus_fatal (Test, "This are a FATAL message");
  log4cplus_fatal (Test, "This are a FATAL message");
  log4cplus_fatal (Test, "This are a FATAL message");
  log4cplus_fatal (Test, "This are a FATAL message");
 }
}


int _tmain (int argc, _tchar* argv[])
{
Try
{
Propertyconfigurator Config (Log4cplus_text ("./urconfig.properties"));
Config.configure ();
}
catch (Std::exception &e)
{
Std::cout<<e.what () <<endl;
:: System ("pause");
return 0;
}

for (int i = 0; i< 100;++i)
{
Printdebug ();
}

:: System ("pause");

return 0;
}

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.