Implement Log4cplus write logs and split by size in single-process or single-process multithreading

Source: Internet
Author: User

Filter log information based on script configuration
In addition to implementing the configuration of the log environment through the program , Log4cplus implements script-based configuration 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 output, I will be at the end of the full text
Part of the article mentions how to use script configuration to implement performance testing in a multithreaded environment, this section will focus on footings This implementation of filtering log information functions.

Let's start with a brief introduction to the syntax rules of the script:
This includes the configuration syntax for Appender and the configuration syntax for logger, where:

Configuration syntax for 1.Appender:
1.1 Setting Name:
/* Set Method */log4cplus.appender.appendername=fully.qualified.name.of.appender.class
For example (list all possible appender, where 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:
Including selection filters and setting filters, selectable filters include: Loglevelmatchfilter, Loglevelrangefilter, and Stringmatchfilter:
For Loglevelmatchfilter, the filtering conditions include Logleveltomatch and Acceptonmatch (True|false), only if the loglevel value of the log information is the same as Logleveltomatch, And Acceptonmatch is true to match.
For Loglevelrangefilter, the filters include Loglevelmin, Loglevelmax, and Acceptonmatch, only if the loglevel of the log information is in Loglevelmin, Matches between Loglevelmax while Acceptonmatch is true.
For Stringmatchfilter, the filtering conditions include Stringtomatch and acceptonmatch, only if the loglevel value of the log information is the same as the Stringtomatch value corresponding to LogLevel, And Acceptonmatch is true when it matches.
The filter processing mechanism is similar to Iptable's responsibility chain (i.e., deny first, then allow) but the execution order is reversed, and the post-write condition is executed first, such as:
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 is executed first, all filters are turned off, and then the filters.1 is executed, matching only the trace information.

1.3. Set layout
To choose not to set, Ttcclayout, or patternlayout
to output log information in a simple format if not set. The
setting Ttcclayout is 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

2.logger configuration Syntax
The Appender under the same logger will output the contents to all files under the logger, and can be filtered by means of LogLevel.
The following demonstrates ways to isolate output content by creating different logger. The
includes Rootlogger and Non-root logger.
for Rootlogger: Log4cplus.rootlogger=[loglevel], Appendername, Appendername, ...
for Non-root logger: log4cplus.logger.logger_name=[loglevel| Inherited], Appendername, Appendername, ... The
     scripting approach is simple to use, as long as the configuration is loaded first (Urconfig.properties is a self-defined profile):
Propertyconfigurator:: Doconfigure ("urconfig.properties"); Let's take a look at Log4cplus's powerful script-based filtering of log information.

The following is a VS2012 WIN32 console project Log4cplus_test to demonstrate the log output,
The project needs to pay attention to two points:
1. Use the latest log4cplus-1.1.1 version, which is linked to the static library Log4cplusSD.lib
2. The project needs to set the charset to "use multibyte Character set", the setting method is the VS2012 menu:
Item->log4cplus_test Properties, configuration properties, character set

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

#全局默认根 logger, this ignores
#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 x Appender
Log4cplus.logger.APPfilelogger = trace,app,app_daily
Og4cplus.additivity.APPfilelogger = False

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

#独立的 logger configuration syntax
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 under Logger
Log4cplus.appender.sys=log4cplus::rollingfileappender
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

Implement Log4cplus write logs and split by size in single-process or single-process multithreading

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.