An entry-level use case for the most simple log4j

Source: Internet
Author: User

Writing such a small example is to avoid a daunting log4j to the people.

Log4j This kind of thing, the individual thinks that the Java beginner does not need to understand too much too deeply, will use can. But in the process of use, the configuration will encounter a variety of problems. The following is an example of a HelloWorld that I have personally summed up in the process of using log4j. For everyone's reference.

The following points should be grasped when using log4j:

1, log level can be configured, through the configuration log level can control the output of the log, used in the most places is to control the debug information output;

2, using Logger.info and so on this way output log is better than using SYSTEM.OUT.PRINTLN () this way;

3, beginners do not use log4j 2 version, because 2 version than 1 version of the change is larger, to learn to increase the difficulty.

In this example, Log4j-1.2.17.zip is used.

1th Step:

Log4j-1.2.17.zip decompression, put log4j-1.2.17.jar this package into the project BuildPath (here I also want to not know what is a concept, let everyone laughed at. In short, let your project be able to refer to the class in the jar package of Log4j-1.2.17.jar);

2nd Step:

In this directory log4j-1.2.17.zip\apache-log4j-1.2.17\examples find the sort2.properties file, rename it to log4j.properties, copy it to the project's source code SRC followed by the path.

The reason why I use sort2.properties this profile is because I personally feel that the output information of this configuration file has been more detailed;

3rd Step:

Configure the log level on line 1th of the Log4j.properties file, there are 5 kinds of log level, you know, I will not wordy;

4th Step:

Declare a logger inside the test class:

/**      * Loggerfor this class*/    privatestaticfinal Logger Logger = Logger.getlogger (anjudianping.  Class);

You can then use it where you need to output the log:

Logger.debug ("The GetParameter () method of the request receives the parameter value:" + content);

The above is done to add log4j log output to your project. About the log4j of some detailed introduction, the Internet has a lot of information, here is not too much introduction. This article is just an example of getting started. You can change the configuration of the Log4j.properties property file according to the data you find on the network to match the log output requirements of your project.

An entry-level use case for the most simple log4j

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.