Log4j Log Component parsing

Source: Internet
Author: User
Tags log log log4j
1. What is log4j

Log4j is an open source project for Apache and is a package for manipulating logs. By using log4j, you can specify destinations for log information output, such as consoles, files, Cui components, NT event loggers, and control each log output format. and are all in the form of a configuration file. What is the composition of 2.log4j?

<1>. Logger: Responsible for generating logs, and can classify the log information to filter, popularly speaking is to decide what log information should output, what log information should be ignored.

<2>. Appender: Define the destination of log information output, specify where the log information should be exported, such as console, file, network equipment, etc.

<3>. Layout: Specifies the output format of the log information.

With the collaboration of three components, the developer can record information according to the log information category, and can control the output format and log storage location of the log while the program is running. A logger can have multiple appender, which means that log information can be exported to multiple devices at the same time, and each appender corresponds to a layout,layout format that determines the output log information. 3. Usage

To introduce a jar package:



############# log output to the console #############
#通过根元素指定日志输出的级别, Destination
#  Log Output priority:  Debug < Info < warn < Error 
log4j.appender.console= Org.apache.log4j.ConsoleAppender
#指定日志输出的格式: Flexible format
log4j.appender.console.layout= Org.apache.log4j.PatternLayout
log4j.appender.console.layout.conversionpattern=%d%p%c.%m ()-% m%n

############# Log output to file #############
# file parameters, specifying the path of the log file (here is the logs folder to be exported to Tomcat)
log4j.appender.file.file= ... /logs/mylog.log
# file parameters, specifying maximum log file size
# file parameters, specifying the maximum number
of log files generated log4j.appender.file.maxbackupindex=100
log4j.appender.file.layout.conversionpattern=%d%c.%m ()-%m%n

Output to the console implementation. The configuration for the output to the file is not needed, otherwise the file error cannot be reported :

public class App_log4j {

	log log = Logfactory.getlog (App_log4j.class);
	@Test public
	Void Save () {
		try {
			Log.info (Save: Start to Save method);

			int i = 1/0;
			Log.info ("Save: Execute save End, success");
		} An exception occurred in catch (Exception e) {
			log.error ("Execute App Class Save () method." ");  Exception
			E.printstacktrace ();
	@Test public
	void testlog4j () throws exception{
		//output different levels of hint
		log.debug ("Debug Information");
		Log.info ("information hint");
		Log.warn ("warning");
		Log.error ("exception");


--------------------------------------------------------------originating from Document:----------------------------------

The meanings represented by several symbols in the log Information format:
-X: Left-aligned when x information is output;
%p: Output log information priority, i.e. Debug,info,warn,error,fatal,
%d: the date or time of the output log point-in-time, the default format is ISO8601, or the format can be specified thereafter, for example:%d{yyy MMM dd hh:mm:ss,sss}, Output is similar: October 18, 2002 22:10:28,921
%r: The number of milliseconds to output from the application boot to the output of this log information
%c: The class that the output log information belongs to, usually the full name of the class in which it is located
%t: Output The name of the thread that generated the log event
%l: The location of the output log event, equivalent to the combination of%c.%m (%f:%l), including the class name, the thread that occurred, and the number of lines in the code. Example: Testlog4.main (testlog4.java:10)
%x: The NDC (nested diagnostics environment) associated with the current line threads relative, especially in multiple client multi-threaded applications such as Java Servlets.
Percent%: output a "%" character
%F: The name of the file where the output log message was generated
%l: line number in output code
%M: The message specified in the output code, the resulting log specific information
%n: Output a carriage return line break, Windows platform for "\ r \ n", Unix platform for "\ n" Output log message Wrapping

%m: Represents the name of the method

You can control the minimum width, the maximum width, and the alignment of text by adding modifiers between% and pattern characters. Such as:
1)%20c: Specifies the name of the output category, the minimum width is 20, if the category name is less than 20.
2)%-20c: Specifies the name of the output category, the minimum width is 20, if the category name is less than 20, the "-" number specifies left-aligned.
3)%.30c: Specifies the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more characters truncated, but less than 30 words will not have spaces.
4)%20.30c: If the name of the category is less than 20 to fill the space, and the right alignment, if its name is longer than 30 characters, from the left side of the exported characters are cut off.

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.