log4j Terminator (i)--detailed description of the meaning of the code in the log4j configuration file in the form of an example

Source: Internet
Author: User

With log4j, you can easily process logs in your project-control the destination of the Log information output (console, file, GUI component, or NT Event recorder), control the output format of each log (htmllayout layout format, patternlayout layout format, Simplelayout layout format or ttcclayout), controls the level of output log information (debug level, info level, warn level, error level, or fatal level), and so on. Here's a simple example of how to use log4j in detail:

Engineering structures such as:


Code in the Testlog4j.java file:

Package Com.ghj.test;import Org.apache.log4j.logger;public class Testlog4j {private static final Logger Logger = logger.g Etlogger (testlog4j.class);p ublic static void Main (string[] args) {logger.debug ("This log  comes from the debug level!" ); Logger.info ("This log  comes from the info level!"); Logger.warn ("This log  comes from the Warn level!"); Logger.error ("This log  comes from the error level!");}    }

Code in the Log4j.properties file:

log4j.rootlogger=debug,console,dailyrollingfile# output log information to the console log4j.appender.console= org.apache.log4j.consoleappenderlog4j.appender.console.encoding=utf-8log4j.appender.console.layout= org.apache.log4j.simplelayout# output The log information to the index.html file in the operating system D packing directory log4j.appender.dailyrollingfile= org.apache.log4j.dailyrollingfileappenderlog4j.appender.dailyrollingfile.encoding= Utf-8log4j.appender.dailyrollingfile.layout=org.apache.log4j.htmllayoutlog4j.appender.dailyrollingfile.file=d\ : \\index.html

"0 points Download the above source project "

To Configure the log4j log output level :

The first line of code in the Log4j.properties file above equals to the right "debug" string indicates the log log output level, log4j log output level of a total of five levels, from small to large respectively is DEBUG, INFO, WARN, error and fatal.

Note:

1. Log information is only output to the specified log output destination when the log level is equal to or higher than the configured log level, for example, the log level for the configuration file configuration above is debug, Log information at the log level of debug or info or warn or error or fatal is then output to the specified log output destination, but if the log level in the profile is set to info, At this point the log level is info or warn or error or fatal log information to output to the specified log output destination, debug level log information will not be output to the destination of the log;

2. If the log information output level is not specified in the log profile, the system will output information at the log information level equal to or higher than the debug level to the specified log destination-in a word, the default log priority for LOG4J is the debug level.

log4j Log Output Destination :

Running the above program, we will find that the log information output to two places, respectively, is the MyEclipse console and





In summary ...

















log4j Terminator (i)--detailed description of the meaning of the code in the log4j configuration file in the form of an example

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.