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

Source: Internet
Author: User

Filter log information based on script configuration
In addition to 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
Part of this section mentions how scripting configurations can be used to implement performance testing in a multithreaded environment. This section will focus on the footings implementation of the filter log information function.

First introduce the syntax rules for the script: The
contains appender configuration syntax and logger configuration syntax. Middle:

1.appender configuration Syntax:
1.1 Setting Name:
/* Set method */log4cplus.appender.appendername= Fully.qualified.name.of.appender.class
For example (listing all possible appender, Socketappender 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:
Contains selection filters and set filter criteria. The selectable filters include: Loglevelmatchfilter, Loglevelrangefilter, and Stringmatchfilter:
For Loglevelmatchfilter, the filter includes Logleveltomatch and Acceptonmatch (True|false), only when the loglevel value of the log information is the same as Logleveltomatch, And Acceptonmatch is true to match.


Loglevelrangefilter, the filter includes Loglevelmin, Loglevelmax, and Acceptonmatch, only when the log information loglevel in Loglevelmin, Loglevelmax matches when Acceptonmatch is true at the same time.


for Stringmatchfilter, the filter condition contains Stringtomatch and Acceptonmatch. Only the loglevel value of the log information is the same as the corresponding loglevel value of the Stringtomatch. And Acceptonmatch is true when it matches. The
     filter processing mechanism is similar to the responsibility chain of iptable (i.e., deny first, then allow) just the opposite of the run order. The post-write condition will be run 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 is run first. Close all filters. Then run filters.1, matching only trace information.

1.3. Setting layout
To choose not to set, Ttcclayout, or Patternlayout
assumes that it is not set. Will output log information in a simple format.
Set Ttcclayout as seen in the following: Log4cplus.appender.all_msgs.layout=log4cplus::ttcclayout
Set Patternlayout as seen below: 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 establishing different logger. The
contains 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, just to load the configuration first (Urconfig.properties is a self-defined configuration file):
Propertyconfigurator ::d oconfigure ("urconfig.properties"); The following is a sample of log4cplus powerful script-based filtering of log information.

The following is a VS2012 WIN32 console project Log4cplus_test to demonstrate the log output,
Project needs to be aware of two points:
1. Use the latest log4cplus-1.1.1 version number, which is linked to the static library Log4cplusSD.lib
2. Project needs to set the character set 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 cut 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.