Eclipse builds log4j test log

Source: Internet
Author: User
Tags log4j

Now write this tutorial for some newly-started friends, if the great God feels bad, please correct me.

First of all, we open the Eclipse software, a new Java project, my project name is Log4jdemo, click to complete;

Project name-right-Create a new folder, name Libs, store jar package here, etc.;


The Log4j-1.2.17.jar package is then imported into the Libs folder, and other versions of the LOG4J package are also available;

Log4j Related documents >> Links: https://pan.baidu.com/s/1ht5ej5q Password: 28te


Click Log4j-1.2.17.jar Package-Right-build path-add to build path;


Click the item right-build path-Accessories build path;


Click on the Library tab-add;


Select JUnit and click Next;


Click Finish;


See the library here are JUNIT4, you can click the application-apply and close;


Here you see JUnit 4 under the project;


Create a test package under SRC;


src right Key-new-file;


My name log4j.properties, click Finish;



We need to change its encoding format, file right key-attributes;


Click on the other-utf-8-application-apple and close;


The Log4j.properties code is as follows:

#这是一个属性配置文件, #号表示注释一行代码 #默认的log4j的配置文件需要放到src或classes目录下面, the filename is: log4j.properties #log4j一共有3个组件: Logger logger, appender-output layout-output layout/format #配置文件就是需要对3个组件进行配置 #配置一个根日志器 log4j.rootlogger =info,systemout,logfile #logDailyFile, 
Logrollingfile,logmail,logdb #调用哪个输出类进行输出 log4j.appender.systemOut = Org.apache.log4j.ConsoleAppender #配置输出格式 
Log4j.appender.systemOut.layout = Org.apache.log4j.PatternLayout
Log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%-22d{yyyy/mm/dd hh:mm:sss}][%l]%n%m%n #输出目的地 Log4j.appender.systemOut.Target = System.out #log4j. appender.systemOut.Threshold = DEBUG # Log4j.appender.systemOut.ImmediateFlush = TRUE #基本学习目的: 1. Master the installation steps of log4j. 2. Ability to modify log4j configuration file. 3. Use the appropriate log level to log when encoding.
4. View the log to run the program. #输出到文件-log4j.appender.logfile = Org.apache.log4j.FileAppender Log4j.appender.logFile.layout = Org.apache.log4j.PatternLayout Log4j.appender.logFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/mm/dd HH:mm:ssS }][%l]%n%m%n log4j.appender.logFile.Append = TRUE log4j.appender.logFiLe.
 
 File = Log.log #UTF-8 without spaces log4j.appender.logFile.Encoding =utf-8

Create a new test class Testdemo under test package;
The Testdemo.java code is as follows:

Package test;

Import Org.apache.log4j.Logger;
Import Org.junit.After;
Import Org.junit.Before;
Import Org.junit.Test;

public class Testdemo {

private static Logger Logger =logger.getrootlogger ();
	
	The Before test unit, typically used to initialize the test environment, performs a
	method stub that @Before public
	void Firsttest () {
		//TODO automatically generates
		before each @test. System.out.println ("I am the code for Priority Execution");
		Logger.info ("I am the code for Priority Execution");
	}
	
	After the test unit, which is typically used to test the environment, he will perform
	@After public
	void Lasttest () {
		//system.out.println ("I am the last code to run" after each @test) , after each test unit to execute Me ");
		Logger.info ("I am the last code to run, every test unit will execute me after");
	}
	
	@Test public
	void Test () {
		//debug Experience-encoding needs to output various levels of log information based on the severity of the error message
		//For the output program to produce a critical error message
		logger.fatal (" Test ");
		Used to output generic error message
		logger.error ("test");
		For output warning information-warn the above level is used to output program run exception information or error message
		logger.warn ("test");
		For output informational information-for primary operations or important operational records
		Logger.info ("test");
		For output debugging information, debug is typically used only for the debug phase using
		logger.debug ("test");
	}


Now do junit testing;



JUnit test log output results;


If the Green representative passes the test here.


Okay, here's the tutorial.






Related Article

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.