Log4j 简单概念及示例.

Loggers, Appenders and Layouts
Log4j has three main components: loggers, appenders and layouts. These three 

types of components work together to enable developers to log messages according 

to message type and level, and to control at runtime how these messages are 

formatted and where they are reported.

This rule is at the heart of log4j. It assumes that levels are ordered. For the 

standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.

Appender:
 an output destination is called an appender.
For example, if a console appender is added to the root logger, then all enabled 

logging requests will at least print on the console.



Here is a sample configuration file that results in identical output as the previous BasicConfigurator based example.

------------------------------------------------
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
 


Suppose we are no longer interested in seeing the output of any component belonging to the com.foo package. The following configuration file shows one possible way of achieving this.


log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

# Print the date in ISO 8601 format
log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN 

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

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
 


Calling the enhanced MyApp with the this configuration file will output the following on the console.

 INFO [main] (MyApp2.java:12) - Entering application.
DEBUG [main] (Bar.java:8) - Doing it again!
 INFO [main] (MyApp2.java:15) - Exiting application.

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

#ERROR为等级,FILE为Appender
log4j.rootLogger=ERROR,CONSOLE,FILE
#定义一个新的logger,名称为admin,可通过Logger.getLogger("admin")获得,继承了rootLogger的配置
log4j.logger.admin=WARN

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%p-[%d] -%M (%F\:%L) %m%n%n

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=c\:/risen_app.log
log4j.appender.FILE.MaxFileSize=2000KB
log4j.appender.FILE.MaxBackupIndex=1
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c - %m%n



//MyApp.java

package com.test.log4j;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class MyApp {
	// Define a static logger variable so that it references the
	// Logger instance named "MyApp".
	static Logger log=Logger.getLogger(MyApp.class);
	//按名称取logger
	//static Logger log=Logger.getLogger("admin");
	public static void main(String[] args) {
		 // Set up a simple configuration that logs on the console.
	     BasicConfigurator.configure();
		log.debug("MyApp 启动");
		Foo f=new Foo();
		f.doIt();
		log.fatal("MyApp 中止");
		
	}
}

//Foo.java

package com.test.log4j;
import org.apache.log4j.Logger;
public class Foo {
	static Logger log=Logger.getLogger(Foo.class);
	public void doIt(){
		
		log.info("Foo DoIt.");
	}
}
log4j.properties
log4j.rootLogger=INFO,FILE
#定义一个新的logger,名称为admin,可通过Logger.getLogger("admin")获得,继承了rootLogger的配置
log4j.logger.admin=DEBUG

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.conversionPattern=%p-[%d] -%M (%F\:%L) %m%n%n

log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=c\:/wcg.log
log4j.appender.FILE.MaxFileSize=2000KB
log4j.appender.FILE.MaxBackupIndex=1
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c - %m%n
posted @ 2011-04-21 19:13  庚武  Views(429)  Comments(0Edit  收藏  举报