inotgo.com

Step 1 : System.out.println   
Step 2 : Import jar Bag   
Step 3 : use Log4j   
Step 4 : Code explanation   

usually , When we write code , It is inevitable to output all kinds of debugging information . Before using any logging tools , Will use System.out.println To do . This is intuitive and effective , But there are a series of shortcomings :
1. I don't know which class this sentence is in , Which thread came out
2. I don't know when and how long the interval between the output of the first and second sentences
3. Unable to close debug information , Once System.out.println After more , Output is everywhere , Increase the difficulty of locating the information you need
wait
As shown in the figure , The bare appearance of such output information
System.out.println
package log4j; public class TestLog4j { public static void main(String[] args) throws InterruptedException { System.out.println(" Tracking information "); System.out.println(" Debugging information "); System.out.println(" Output information "); System.out.println(" Warning message "); System.out.println(" Error message "); System.out.println(" Fatal information "); } }
package log4j;

public class TestLog4j {
	
	public static void main(String[] args) throws InterruptedException {
		
		System.out.println(" Tracking information ");
		System.out.println(" Debugging information ");
		System.out.println(" Output information ");
		System.out.println(" Warning message ");
		System.out.println(" Error message ");
		System.out.println(" Fatal information ");

	}
}
Next, show me how to use log4j Log output , You need to import... Before proceeding jar Bag , Download... In the upper right corner
To deal with this situation , We use Log4j For log output . Use the following code , Execute the same output . You can see several improvements in the output :
1. I know it's log4j.TestLog4j Logs in this class
2. It's in [main] Log in thread
3. The log level is observable , There are 6 Two levels TRACE DEBUG INFO WARN ERROR FATAL
4. The range of log output levels can be controlled , As shown in the code , Only output higher than DEBUG Level , So TRACE Level logs are not automatically output
5. The number of milliseconds consumed per log sentence ( The first number ), Observable , In this way, performance calculation can be carried out
 use Log4j
package log4j; import org.apache.log4j.BasicConfigurator; import org.apache.log4j.Level; import org.apache.log4j.Logger; public class TestLog4j { static Logger logger = Logger.getLogger(TestLog4j.class); public static void main(String[] args) throws InterruptedException { BasicConfigurator.configure(); logger.setLevel(Level.DEBUG); logger.trace(" Tracking information "); logger.debug(" Debugging information "); logger.info(" Output information "); Thread.sleep(1000); logger.warn(" Warning message "); logger.error(" Error message "); logger.fatal(" Fatal information "); } }
1. Get the log object based on the name of the class

static Logger logger = Logger.getLogger(TestLog4j.class);


2. Make the default configuration

BasicConfigurator.configure();


3. Set the log output level

logger.setLevel(Level.DEBUG);


3. Perform different levels of log output

logger.trace(" Tracking information ");
logger.debug(" Debugging information ");
logger.info(" Output information ");
Thread.sleep(1000);
logger.warn(" Warning message ");
logger.error(" Error message ");
logger.fatal(" Fatal information ");

Thread.sleep(1000); This is to facilitate the observation of the time difference between the log output before and after


The official account of programming , Follow and get the latest tutorials and promotions in real time , thank you .


Q & A area    
2020-07-24 maven The dependent coordinates are as follows
jimisun

<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>







The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2020-01-08 log4j Too old , Changed log4j2, The following is a tutorial
When drunk

https://blog.csdn.net/weixin_44174530/article/details/103889260




2 One answer

newbeeee
Answer time :2020-09-03
Why only output logger.fatal Information about ....

Xiaobai, Baibai
Answer time :2020-06-21
Thanks



The answer has been submitted successfully , Auditing . Please My answer Check the answer record at , thank you
answer Or code please Fill in at least one , If you have a problem , Please ask again , Otherwise, the webmaster may not see




2019-08-09 lib Next jar What's the use ;
2019-07-23 The import org.apache cannot be resolved
2019-03-01 What if you download it


Too many questions , Page rendering is too slow , To speed up rendering , Only a few questions are displayed on this page at most . also 7 Previous questions , please Click to view

Please... Before asking questions land
The question has been submitted successfully , Auditing . Please My question Check the question record at , thank you
about JAVA application -Log4j- introduction Your questions

Try to provide Screenshot code and Abnormal information , Help to analyze and solve problems . You can also enter this station QQ Group communication : 496725845
Ask questions and try to provide complete code , Environment description , The more conducive to the recurrence of the problem , The faster your question can be answered .
Have questions about the code in the tutorial , Please provide which step , Which line is in doubt , This makes it easy to quickly locate the problem , Improve the speed at which questions are answered
In the thousands of questions that already exist , A considerable proportion , Because of the use of and webmaster Different versions of the development environment Resulting in , For example jdk, eclpise, idea, mysql,tomcat Wait, the version of the software is inconsistent .
Please use the same version as the webmaster , You can save yourself a lot of learning time . The webmaster sorted out the software versions used in teaching , It's all here , Convenient for everyone to download : /k/helloworld/helloworld-version/1718.html

Upload screenshot