Implement log4cplus to write logs in a single process, single thread, or single process, and separate logs by size

Source: Internet
Author: User

Implement log4cplus to write logs in a single process, single thread, or single process, and separate logs by size

Filter log information based on script configuration
In addition to program configuration of the log environment, log4cplus implements script-based configuration 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.
This section describes how to use script configuration to implement performance testing in a multi-threaded environment. This section describes how to use a base script to filter log information.

First, we will briefly introduce the script syntax rules:
Including the configuration Syntax of Appender and logger, where:

1. Appender configuration Syntax:
1.1 set the name:
/* Setting Method */log4cplus. appender. appenderName = fully. qualified. name. of. appender. class
For example (list all possible Appender, SocketAppender is not used here ):
Log4cplus. appender. append_1 = log4cplus: leleappender
Log4cplus. appender. append_2 = log4cplus: FileAppender
Log4cplus. appender. append_3 = log4cplus: RollingFileAppender
Log4cplus. appender. append_4 = log4cplus: DailyRollingFileAppender
Log4cplus. appender. append_4 = log4cplus: SocketAppender

1.2. Set Filter:
You can select the LogLevelMatchFilter, LogLevelRangeFilter, and StringMatchFilter:
For LogLevelMatchFilter, the filtering conditions include LogLevelToMatch and AcceptOnMatch (true | false). Only when the LogLevel value of log information is the same as that of LogLevelToMatch and the AcceptOnMatch value is true.
For LogLevelRangeFilter, the filtering conditions include LogLevelMin, LogLevelMax, and AcceptOnMatch. Only when the LogLevel of log information is set to true in both LogLevelMin and LogLevelMax.
For StringMatchFilter, the filter conditions include StringToMatch and AcceptOnMatch. Only when the LogLevel value of log information is the same as that of StringToMatch, And the AcceptOnMatch value is true.
The filter condition processing mechanism is similar to the Responsibility chain of IPTABLE (namely, deny first and then allow). However, the execution order is the opposite. Then, the written conditions are executed first, for example:
Log4cplus. appender. Handler = log4cplus: spi: container. appender. Handler = TRACElog4cplus. appender. Handler = true # log4cplus. appender. Handler = log4cplus: spi: DenyAllFilter
The system first executes the filtering conditions of filters.2, closes all filters, and then executes filters.1 to match only TRACE information.

1.3. Set Layout
You can choose not to set, TTCCLayout, or PatternLayout
If this parameter is not set, log information in simple format is output.
Set TTCCLayout as follows: log4cplus. appender. ALL_MSGS.layout = log4cplus: TTCCLayout
Set PatternLayout as follows: log4cplus. appender. append_1.layout = log4cplus: PatternLayoutlog4cplus. appender. append_1.layout.ConversionPattern = % d {% m/% d/% y % H: % M: % S, % Q} [% t] %-5 p-% m % n

2. logger configuration syntax
The Appender under the same logger will output content to all the files under the logger, which can be filtered by using measures such as LogLevel.
The following describes how to create different logger to isolate the output content.
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 ,...
The script method is very easy to use. You only need to load the configuration first (urconfig. properties is a custom configuration file ):
PropertyConfigurator: doConfigure ("urconfig. properties"); The following is an example of the powerful script-based log filtering function of log4cplus.

The following is the WIN32 console project log4cplus_test created in VS2012, which is used to demonstrate log output,
Note the following two points for the project:
1. Use the latest log4cplus-1.1.1 version, link to which is the static library log4cplusSD. lib
2. Set the character set to "use multi-Byte Character Set" in the project. The setting method is VS2012:
Project-> log4cplus_test properties-> Configuration properties-> Character Set

The following is the content of the configuration file urconfig. properties. Use the configuration to control the log output of log4cplus.

# The Global Default Root logger is ignored 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

# Independent logger configuration syntax, supporting two appender
Log4cplus. logger. APPfilelogger = TRACE, APP, APP_DAILY
Og4cplus. additi.pdf. APPfilelogger = false

# Independent logger configuration syntax. The same logger will be sent to all files,
# Whether to write data to all files, controlled by LogLevel
Log4cplus. logger. SYSfilelogger = TRACE, SYS
# Log4cplus. additiger. SYSfilelogger = TRUE

# Independent logger configuration syntax
Log4cplus. logger. ACCfilelogger = TRACE, ACC
# Log4cplus. additi.pdf. ACCfilelogger = TRUE

# Only the specified file under the same logger can be written.
Log4cplus. appender. APP = log4cplus: RollingFileAppender
Log4cplus. appender. APP. File =./logout/app_msgs.log
Log4cplus. appender. APP. ImmediateFlush = false
Log4cplus. appender. APP. MaxFileSize = 1 MB
# Log4cplus. appender. APP. MinFileSize = 1 M
Log4cplus. appender. APP. MaxBackupIndex = 3
Log4cplus. appender. APP. layout = log4cplus: PatternLayout
Log4cplus. appender. APP. layout. conversionPattern = % D {% Y-% m-% d % H: % M: % S. % Q} | %-5 p | % 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

# Only the specified file under the same logger can be written.
Log4cplus. appender. APP_DAILY = log4cplus: DailyRollingFileAppender
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: PatternLayout
Log4cplus. appender. APP_DAILY.layout.ConversionPattern = % D {% Y-% m-% d % H: % M: % S. % Q} | %-5 p | % 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

# Only the specified file under the same logger can be written.
Log4cplus. appender. SYS = log4cplus: RollingFileAppender
Log4cplus. appender. SYS. File =./logout/sys_msgs.log
Log4cplus. appender. SYS. MaxFileSize = 1 MB
Log4cplus. appender. SYS. MaxBackupIndex = 3
Log4cplus. appender. SYS. ImmediateFlush = false
Log4cplus. appender. SYS. layout = log4cplus: PatternLayout
Log4cplus. appender. SYS. layout. conversionPattern = % D {% Y-% m-% d % H: % M: % S. % Q} | %-5 p | % 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

# Only the specified file under the same logger can be written.
Log4cplus. appender. ACC = log4cplus: RollingFileAppender
Log4cplus. appender. ACC. File =./logout/acc_msgs.log
Log4cplus. appender. ACC. MaxFileSize = 1 MB
Log4cplus. appender. ACC. MaxBackupIndex = 3
Log4cplus. appender. ACC. ImmediateFlush = false
Log4cplus. appender. ACC. layout = log4cplus: PatternLayout
Log4cplus. appender. ACC. layout. conversionPattern = % D {% Y-% m-% d % H: % M: % S. % Q} | %-5 p | % 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

Related Article

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.