Open-source log system-log4cplus (III)

Source: Internet
Author: User

How to control the Output Message format

As mentioned earlier, log4cplus uses layouts to control the output format. log4cplus provides three types of layouts,
They are simplelayout, patternlayout, and ttcclayout.
1. simplelayout
It is a simple format la er that adds a loglevel and a "-" before the original output information "-".
For example, the following code snippet:
    /* step 1: Instantiate an appender object */
// The appender name is the file name of the last generated log.
Export dobjectptr _ append (New consoleappender ());
_ Append-> setname ("APPEND for test ");
    /* step 2: Instantiate a layout object */
    std::auto_ptr< log4cplus::SimpleLayout> _layout(new log4cplus::SimpleLayout());
    /* step 3: Attach the layout object to the appender */
    _append->setLayout( _layout );
    /* step 4: Instantiate a logger object */
    Logger _logger = Logger::getInstance("test");
    /* step 5: Attach the appender object to the logger  */
    _logger.addAppender(_append);
/* Log Activity */
Log4cplus_debug (_ logger, "This is the simple formatted Log message ...")
......

The result will be printed:
Debug-this is the simple formatted Log message...
2. patternlayout
It is a pattern la er with lexical analysis function. When the mode is mentioned, it will remind you of the regular expression. The pattern here is similar to the regular expression,
Far simpler than the latter, it can parse predefined Identifiers (called conversion specifiers) and convert them to specific formats for output. Below
The code snippet demonstrates how to use patternlayout:
    ... ...
    /* step 1: Instantiate an appender object */
    SharedObjectPtr _append (new ConsoleAppender());
    _append->setName("append for test");
  
    /* step 2: Instantiate a layout object */
    std::string pattern = "%d{%m/%d/%y %H:%M:%S}  - %m [%l]%n";
    std::auto_ptr<PatternLayout> _layout(new PatternLayout(pattern));
    /* step 3: Attach the layout object to the appender */
    _append->setLayout( _layout );
    /* step 4: Instantiate a logger object */
    Logger _logger = Logger::getInstance("test_logger.subtest");
    /* step 5: Attach the appender object to the logger  */
    _logger.addAppender(_append);
/* Log Activity */
Log4cplus_debug (_ logger, "teststr ")

......

Output result:
10/16/04 18:51:25-teststr [main. cpp: 51]
We can see that by entering the pattern string "pattern" in a specific format, the original information is contained in a pile of formatted Information, which makes
You can customize the display content as needed. "Pattern" can contain common strings and predefined identifiers, where:
(1) information that can be directly displayed as a normal string.
(2) A pre-defined identifier. "%" and one or more characters form a pre-defined identifier and produce specific format information.
For pre-defined identifiers, the log4cplus documentation provides detailed format descriptions. I tried each of them. The above code is used as an example.
The usage of various message formats is as follows:
(1) "%", escape as %, that is, output when STD: String Pattern = "%": "%"
(2) "% C": output the logger name, for example, STD: String Pattern = "% C": "test_logger.subtest ",
You can also control the display level of the logger name. For example, "test_logger" is output when "% c {1}", where numbers represent layers.
(3) "% d": displays the local time. When STD: String Pattern = "% d" is output: "18:55:45", % d indicates the standard time,
Therefore, when STD: String Pattern = "% d" is output "10:55:45" (because we are in the east 8 zone, 8 hours apart ).
You can use % d {...} to define a more detailed display format. For example, % d {% H: % m: % s} indicates that the display duration is hour: minute: second. The
The predefined identifier is as follows:

% A -- indicates the day of a week, abbreviated as "fri"
% A -- indicates the day of the week, such as "Friday"
% B -- represents the number of months, abbreviated form, such as "Oct"
% B -- number of months, "October"
% C -- standard date + time format, such as "sat Oct 16 18:56:19 2004"
% D -- indicates the number of the current month (1-31) "16"
% H -- indicates the current time (0-23), for example, "18"
% I -- indicates the current time (1-12), such as "6"
% J -- indicates the Day (1-366), for example, "290"
% M -- indicates the month January (1-12), for example, "10"
% M -- indicates the minute of the current time (0-59), for example, "59"
% P -- indicates whether it is morning or afternoon, am or PM
% Q -- Millisecond (0-999) in the current time, such as "237"
% Q -- the millisecond (0-999.999) with decimal digits in the current time, for example, "430.732"
% S -- the number of seconds (0-59) of the current time, such as "32"
% U -- indicates the week of the year. It is calculated from Sunday (0-53), for example, "41"
% W -- indicates the number of weeks (0-6, 0 on Sunday), such as "6"
% W -- indicates that this week is the week of this year. It is calculated from Monday as the first day (0-53), for example, "41"
% X -- standard date format, such as "10/16/04"
% X -- standard time format, such as "19:02:34"
% Y -- two-digit year (0-99), for example, "04"
% Y -- four-digit year, for example, "2004"
% Z -- Time Zone name, such as "GMT"
(4) "% F", output the name of the file where the current recorder is located, for example, STD: String Pattern = "% F", output: "Main. cpp"
(5) "% L", output the row number of the file where the current recorder is located, for example, STD: String Pattern = "% L", output: "51"
(6) "% L": output the name and row number of the file where the current recorder is located, for example, STD: String Pattern = "% L:
"Main. cpp: 51"
(7) "% m", output original information, for example, STD: String Pattern = "% m", output: "teststr", that is, in the above Code
The second parameter of log4cplus_debug. This implementation mechanism ensures that the original information is embedded in the information with the format.
(8) "% N", line break, nothing to explain
(9) "% P", output loglevel, for example, STD: String Pattern = "% P": "debug"
(10) "% t", output the ID of the thread where the recorder is located, for example, STD: String Pattern = "% t", output: "1075298944"
(11) "% x", nested diagnostic context NDC (nested diagnostic context) output, pop up context information from the stack, NDC can use
Different sources of log information (at the same time) Cross-output are differentiated. A detailed introduction to NDC is described below.
(12) format alignment. For example, STD: String Pattern = "%-10 m" indicates the left alignment, and the width is 10. "teststr" is output when
However, other control characters can also be used in the same way, such as "%-12D", "%-5 p", etc.
"%-5 p" indicates the loglevel ).

3. ttcclayout
It is a default layout with format output developed on the basis of patternlayout. Its format consists of time, thread ID, logger, and NDC group.
Consists of time, thread, logger and nested diagnostic context information, hence the name ),
So it is named (how is it named? Where is the abbreviation of "C" in logger! The name is really bad enough ). Provided to those who want to display
The typical information (usually enough) is too lazy to configure pattern.
Ttcclayout has the opportunity to display the local time or GMT time during construction. By default, it is displayed according to the local time:
Ttcclayout: ttcclayout (bool use_gmtime = false)
The following code snippet demonstrates how to use ttcclayout:
    /* step 1: Instantiate an appender object */
    SharedObjectPtr _append (new ConsoleAppender());
    _append->setName("append for test");
    /* step 2: Instantiate a layout object */
    std::auto_ptr _layout(new TTCCLayout());
    /* step 3: Attach the layout object to the appender */
    _append->setLayout( _layout );
    /* step 4: Instantiate a logger object */
    Logger _logger = Logger::getInstance("test_logger");
    /* step 5: Attach the appender object to the logger  */
    _logger.addAppender(_append);
/* Log Activity */
Log4cplus_debug (_ logger, "teststr ")

......

Output result:
10-16-04 19:08:27, 501 [1075298944] Debug test_logger <>-teststr

When the ttcclayout object is constructed in GMT format:
STD: auto_ptr _ layout (New ttcclayout (true ));

......

Output result:
10-16-04 11:12:47, 678 [1075298944] Debug test_logger <>-testst
This section describes how to control log information formats. The next section describes several file operations for log information.


 

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.