Log4j-simple example of using configuration for log output tools

Source: Internet
Author: User
Tags log4j

The performance of log4j is much better than that of system.out.println, especially when a large number of output statements are observed.

--------------------------------------------------------------------------------------------------

Download Address:

Current version, 2.0 beta3

Jar:http://www.apache.org/dyn/closer.cgi/logging/log4j/2.0-beta3/apache-log4j-2.0-beta3-bin.zip

Src:http://mirror.bit.edu.cn/apache/logging/log4j/2.0-beta3/apache-log4j-2.0-beta3-src.zip

Instance version:

Jar:http://archive.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.jar

Src:http://archive.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17-sources.jar

--------------------------------------------------------------------------------------------------

Instance Start (Sample version: 1.2.17):

I. Configuration file log4j.properties

#log4j. Rootlogger, the first parameter indicates the priority of the log record, and only logs that are equal to or greater than this level are output.
#log4j的日志的级别共有以下几种: Off, FATAL, ERROR, WARN, INFO, DEBUG, all,
#但是我们一般只是用ERROR, WARN, INFO, Debug these four kinds (here according to from high to low).
log4j.rootlogger=debug,console 

#将日志输出到控制台 
log4j.appender.console=org.apache.log4j.consoleappender
#日志输出的信息布局
log4j.appender.console.layout=org.apache.log4j.patternlayout
Log4j.appender.console.target=system.out 
#日志输出格式:%d{time format},%c classpath,%n newline,%m print information #中文需 to
Unicode 
# LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=%N[\U65F6\U95F4]%d{yyyy-mm-dd HH\:mm\:ss}  [\u8DEF\u5F84]%c  %n[\u4fe1\u606f]%m%n
LOG4J.APPENDER.CONSOLE.LAYOUT.CONVERSIONPATTERN=%N[\U65F6\U95F4]%d{yyyy-MM-dd HH\:MM\:SS}   

Second, the use of Web engineering deployment

Main Mode 1, log4j.properties placed under SRC

			String Class_path =  getclass (). GetResource ("/"). GetPath ();
			Propertyconfigurator.configure (class_path+ "log4j.properties")//Get log4j profile
			Logger Logger = Logger.getlogger ( Phrase_wordsservlet.class); Gets the instance of log4j, Phrase_wordsservlet is the name of the current Java file
			

Main Way 2, Log4j.properties put in Lib inside (with Log4j.jar package sibling)

		String rootdir=system.getproperty ("User.dir")//Get Engineering Root path
		propertyconfigurator.configure (rootdir+ "\\WebRoot\\ Web-inf\\lib\\log4j.properties ");//Get log4j configuration file
		//propertyconfigurator.configure (" C:\MyEclipse9\Workspaces\ Finance\webroot\web-inf\lib\log4j.properties "); 
		Logger Logger = Logger.getlogger (Logger_test.class); Gets the instance logger.debug of the log4j
	        ("Console debug Information--debug");//7


--------------------------------------------------------------------------------------------------

Accessories: Unicode and Chinese log4j.properties ("\u65f6\u95f4" means "time" in the configuration file)


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.