Background Service log output specification
Adding program logging in the background service tracks the code runtime as a basis for future audits, and acts as a debugger in the integrated development environment, printing code debug information to a file, and planning the output log file for easy communication with other relevant personnel for analysis. This provides that the C + + background service item must be used.
Log file
Log files are divided into Access logs and application logs and system logs according to the application requirements feature. The unified naming format for log files is as follows: Service name. Application type. Time. Index. The middle is separated by a dot symbol (.).
Service Name: The name of the process name or bin file.
Application type: currently divided into Access, abbreviated to Acc;application, abbreviated to the app, System log, abbreviated to SYS.
Time: The log is currently written by day, and the format is YYYYMMDD. If the file name cannot output a time format, you can omit it.
Index: The current day index starting from 0 or 1, preferably not more than 20. The index can also be an additional identity. The log must support rolling log output to prevent the log from being filled with disk.
1.1. Accessing log files
The access log file is primarily for the output server to receive requests, as well as to respond to requests, external requests, and external service responses to requests.
1.1.1. Access log File format
It is divided into two types: Accept log and answer log.
1.1.1.1. Network Receive log format
Charge time
Log level (Debug,info,warn,error,fatal)
Log Output flag
Thread identity
File name (debug log required, non-debug log can be empty)
File line number (debug log required, non-debug log can be empty)
Function name (debug log required, non-debug log can be empty)
Log description
Processing time (in milliseconds), the request is generally zero.
Log marks (including several large types of req,accept)
Opposite address
Opposite Port
Packet length
The contents of the packet (16 binary), if the content is too large, you can consider just print a portion, the excess is expressed in * * *. The recommended output length is not more than 512 bytes, which is larger than the data masking output of the bytes.
More fields need to be added only to the back, and the field contents are separated by |, if the output contains |, the | Escape to # after the output.
1.1.1.2. Network Answer log format
Response time
Log level (Debug,info,warn,error,fatal)
Log Output flag
Thread identity
File name (debug log required, non-debug log can be empty)
File line number (debug log required, non-debug log can be empty)
Function name (debug log required, non-debug log can be empty)
Log description
Processing time-out (in milliseconds)
Log marks (including several large types of resp,close,shundown)
Opposite address
Opposite Port
Packet length
The contents of the packet (16 binary), if the content is too large, you can consider just print a portion, the excess is expressed in * * *. The recommended output length is not more than 512 bytes, which is larger than the data masking output of the bytes.
More fields need to be added only to the back, and the field contents are separated by |, if the output contains |, the | Escape to # after the output.
1.1.1.3. Log Flags
RESP: Indicates receipt of response from the other party.
REQ: Represents the request for an external service.
ACCEPT: Indicates access to a new connection.
Connect: Indicates that a new connection is initiated to the external service;
Close: Indicates closing a connection;
Shundown: means to cut off a connection
1.1.1.4. Log marker Child Definitions
The RESP and req,connect inside the log mark (Resp,req,connect) can refine the definition and add subtypes. Because a network service may need to interact with different types of services such as A,B,C, the network service is likely to provide both external services and access to other network servers.
For example, access to a process A1 and A2 Server services, respectively, can be defined as:
REQ_A_A1,RESP_A_A1;
Req_a_a2,resp_a_a2;
For example, connecting a process:
Connect_a;
1.1.1.5. Log Output Example
2013-09-04 10:29:43.148.000| Debug| Accfilelogger.acc[2]|4324|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|34|printdebug| This is a ACC message|0| req|172.13.12.35|3456|845|0x 0d 0x
2013-09-04 10:29:43.148.000| Debug| Accfilelogger.acc[2]|4324|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|35|printdebug| This is a ACC message|150| resp|172.13.12.35|3456|345|0x 0d 0x 0A
2013-09-04 10:29:43.148.000| error| Accfilelogger.acc[2]|4324|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|36|printdebug| This is a ACC message|0| connect_a|172.13.12.99|8888| |
2013-09-04 10:29:43.148.000|info | accfilelogger.acc[2]|4324| | | | This is a ACC message|0| req_a_a1|172.13.12.99|8888|234|0x 0d 0x 0A
1.1.2. log file naming
The access log file is named
The service name. ACC. Time. Index.
For example, the process shutong several log file names are:
Shutong. acc.20130819.0
Shutong. acc.20130819.15
Shutong. acc.18
1.1.3. Log output points
there must be output for each request and response.
1.1.4. File storage
Storage directory: The maximum partition must be stored on the disk. such as/data/logs/service name/log file name, example:
/data/logs/shutong/shutong. acc.20130819.0
1.1.5. File size
A single log file is recommended between 50m~~100m .
1.1.6. Log parsing scripts
Need to provide log analysis script, can easily extract log content, location analysis, statistical analysis.
1.2. Application log file
Log mainly for business applications to provide business process support, the need to complete the output of a business of multiple key process node information, to ensure that the log can be consulted to understand the specific situation of each business interaction, to clarify the sub-process of business.
1.2.1. File naming
The access log file is named
The service name. APP. Time. Index.
For example, the process shutong several log file names are:
Shutong. app.20130819.0
Shutong. app.20130819.15
The 12 of the following file is an index, which indicates that the file is generated in 12:14 minutes.
Shutong. App.20130819.12-14
1.2.2. Application log Format
Time of Occurrence
Log level (Debug,info,warn,error,fatal)
Log Output flag
Thread identity
File name (debug log required, non-debug log can be empty)
File line number (debug log required, non-debug log can be empty)
Function name (debug log required, non-debug log can be empty)
Log description
order or message number
More fields need to be added only to the back, and the field contents are separated by |, if the output contains |, the | Escape to # after the output.
1.2.3. Log Output Example
2013-09-04 10:46:59.011.000|info | appfilelogger.app[2]|5592| | | | This is a APP message| orderid_2013090412121200001
2013-09-04 10:46:59.011.000| Debug| Appfilelogger.app[2]|5592|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|41|printdebug| This is a APP message| msgid_2013090412121200002
2013-09-04 10:46:59.011.000| error| Appfilelogger.app[2]|5592|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|42|printdebug| This is a APP message| msgid_2013090412121200003
2013-09-04 10:46:59.011.000| Debug| Appfilelogger.app_daily[2]|5592|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|43|printdebug| This is a APP message| msgid_2013090412121200006
1.2.4. File storage
Storage directory: The maximum partition must be stored on the disk. such as/data/logs/service name/log file name, example:
/data/logs/shutong/shutong. app.20130819.0
1.2.5. File size
A single log file is recommended between 50m~~100m .
1.2.6. Log parsing scripts
need to provide log analysis script, can easily extract log content, location analysis, statistical analysis.
1.3. System Log files
The system log is mainly to record the operation of the system, such as message queue current number of messages, and for debugging convenience, you can compare detailed output interaction of multiple steps, easy to locate the problem.
1.3.1. File naming
The access log file is named
The service name. SYS. Time. Index.
For example, the process shutong several log file names are:
Shutong. sys.20130819.0
Shutong. sys.20130819.15
1.3.2. Log format
Time of Occurrence
Log level (Debug,info,warn,error,fatal)
Log Output flag
Thread identity
File name (debug log required, non-debug log can be empty)
File line number (debug log required, non-debug log can be empty)
Function name (debug log required, non-debug log can be empty)
Log Description
More fields need to be added only to the back, and the field contents are separated by |, if the output contains |, the | Escape to # after the output.
1.3.3. Log Output Example
2013-09-04 10:49:20.296.000|info | sysfilelogger.sys[2]|5496| | | | This is a SYS message
2013-09-04 10:49:20.296.000| Debug| Sysfilelogger.sys[2]|5496|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|47|printdebug| This is a SYS message
2013-09-04 10:49:20.296.000| error| Sysfilelogger.sys[2]|5496|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|48|printdebug| This is a SYS message
2013-09-04 10:49:20.296.000| Debug| Sysfilelogger.sys[2]|5496|f:\work\mytest\log4cplus_test\log4cplus_test\log4cplus_test.cpp|49|printdebug| This is a SYS message
1.3.4. File storage
Storage directory: The maximum partition must be stored on the disk. such as/data/logs/service name/log file name, example:
/data/logs/shutong/shutong. sys.20130819.0
1.3.5. File size
A single log file is recommended between 50m~~100m.
1.4. Log backup mechanism
The day log must be compressed before the tape backup, on the production machine to retain the most recent 5~10 more than a day of log backups.
1.5. Log Content Specification
For "sensitive" unit information, such as name, balance and other information, prohibit the use of debug logging method above the level of logging, in the development process, should also reduce the logging of such information. Some sensitive information can be used to mosaic the output, such as the string part of the content for *. such as: Social Security Number 422927198806031234, you can consider this output: 42292719880*****34.
For particularly sensitive transaction information, you cannot export to a log file. All exceptions (Exception) must be written in the log, and when the system throws an exception, you must use Log.error () to record the custom information.
1.5. Definition of special sensitive information
Password: User login, transaction and other password information;
2. Log Information Level specification
Log information output priority from high to low should be divided into five files, respectively, fatal, ERROR, WARN, INFO, DEBUG. These levels are used to specify how important this log information is. In the test phase can open all levels of the log, the system is online, it is recommended to output only the level of info (including info). The functions of log information at all levels are as follows:
2.1. Fatal (Fatal)
Serious error, the system does not function properly, such as hard disk space is full. This level is rarely used, and often implies that the system or system components are nearing collapse.
2.2. Errors (Error)
The system can continue to run, but it is best to fix the errors as soon as possible. This level is used more often with Java exceptions, the error environment does not necessarily cause the system to crash, the system can continue to serve the next request.
2.3. Warning (Warn)
The system can run normally, but requires a warning message that is noticeable. This level indicates a smaller problem, caused by factors outside the system, such as a user entering a parameter that does not meet the criteria.
2.4. Information (Info)
The operation information of the main key point of the system operation, generally used to record the business log. But at the same time, there should be enough information to ensure that the path to reproduce the defect can be recorded. This level records meaningful events in the daily operation of the system.
2.5. Commissioning (Debug)
The debugging information in the system running is easy for the developers to make error analysis and correction, which is generally used in program log, concerned with program operation (fine granularity), and less concerned about business operation (coarse granularity). When there is a problem with the system, you must throw an exception, record the log when handling the exception, and the log level must be one of the top three levels (Fatal\error\warning).
Log4cplus Implementation method
3.1. Log4cplus Source
From open source project logging, the project address is located at:
Http://log4cplus.sourceforge.net
https://sourceforge.net/news/?group_id=40830
3.2. Version
Currently using a newer version of Log4cplus (1.1.X)
3.3. Configuration files
Use the configuration file urconfig.properties to control the output of log. The file is configured as follows.
#全局默认根 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
#独立的 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|%m%n
Log4cplus.appender.app.filters.1=log4cplus::spi::loglevelrangefilter
Log4cplus.appender.app.filters.1.loglevelmin=trace
Log4cplus.appender.app.filters.1.loglevelmax=fatal
#支持只写入同一个 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|%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|%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|%m%n
Log4cplus.appender.acc.filters.1=log4cplus::spi::loglevelrangefilter
Log4cplus.appender.acc.filters.1.loglevelmin=trace
Log4cplus.appender.acc.filters.1.loglevelmax=fatal
3.4. Code implementation
omitted here.