Log4j-level output

Source: Internet
Author: User
Log4j:
Log4j. Logger. XX cover ==> log4j. rootlogger

Log4j. appender. XX. threshold determines the minimum receiving level

That is to say, rootlogger first determines the output level of all logs, while log4j. Logger. XX modifies the output level of jar packages under the XX path, directly overwriting rootlogger

However, for different output ports, their receiving levels are different.

Example 1:
When the threshold of log4j. appender. XX. threshold is higher than that of log4j. Logger. XX,

Log4j.logger.com. test = debug

Log4j. appender. A. Threshold = info

Log4j. appender. B. Threshold = warn

Even if the output level of COM. Test is set to debug, A only receives logs of info level and above, and B only receives logs of warn level and above.

Example 2:
When the threshold of log4j. appender. XX. threshold is lower than that of log4j. Logger. XX,

Log4j.logger.com. test = info

Log4j. appender. A. Threshold = debug

Log4j. appender. B. Threshold = warn

So COM. test only outputs Info-level information, while debug is set under a, so COM. all the logs output by test can be displayed under a (of course, they are info or above, because COM. test only provides info and above ). In B, it is set to warn. Although com. Test outputs logs of info level and above to B, B only receives logs of warn and above.

With this configuration, you can filter logs of different levels at different output ports.

Application Example requirements:
We do not want to output some third-party jar logs, but we need to output logs of debug and above levels in our own package, but I want to see all the logs in the console, in the log file, I only want to write logs at or above the warn level. How can I configure this situation?

In this case, we will configure log4j based on the actual configuration.
First, set the background.
Our own jar packages start with COM. Test.
A stands for console output
B Indicates file output.
Of course, all these are just named

Okay. Let's get started. First, all third-party jars do not want to see the output log, but the error level or above needs
Log4j. rootlogger = error, a, B
This statement sets the level of all logs output to A and B to error.

Then, we want to output logs of debug and above levels in our own package.
Log4j.logger.com. test = debug
This statement sets the output level of all logs under the com. Test package to debug. The setting here directly overwrites the effect of rootlogger on the com. Test package.

Finally, I want to see all the logs in the console, but under the log file, I only want to write logs at or above the warn level.
Log4j. appender. A = org. Apache. log4j. leleappender
# Config a position output contain debug level and up
Log4j. appender. A. Threshold = debug
Log4j. appender. A. layout = org. Apache. log4j. patternlayout
Log4j. appender. A. layout. conversionpattern = % d [%-5 p] % L: % T-% m % N

# In the daily files
Log4j. appender. B = org. Apache. log4j. dailyrollingfileappender
Log4j. appender. B. Threshold = warn
Log4j. appender. B. File =$ {root. Path}/log/myproject. Log
Log4j. appender. B. datepattern = '. 'yyyy-mm-dd'. Log'
Log4j. appender. B. layout = org. Apache. log4j. patternlayout
Log4j. appender. B. layout. conversionpattern = % d [%-5 p] % L: % T-% m % N

The Code irrelevant to the log level is not explained. Direct focus,
Log4j. appender. A. Threshold = debug
Log4j. appender. B. Threshold = warn
Indicates that the receiving level of A is debug, and the receiving level of B is warn. In this way, a can see all the logs above debug, and only the logs above warn level are written to B.

Log4j-level output

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.