首页 > 代码库 > log4j用法

log4j用法

首先去apache官网下载http://logging.apache.org/log4j/1.2/download.html

新建一个项目,将log4j-1.2.X.jar加入classpath中。

package lee;import org.apache.log4j.Logger;public class testLog {	private static Logger logger = Logger.getLogger(testLog.class);	public static void main(String[] args){		logger.info("ok2");	}}

在src中新建一个log4j.properties

    log4j.rootLogger=INFO,toFile,console      ########   \u8F93\u51FA\u5230\u5230\u6587\u4EF6   ##########      log4j.appender.toFile=org.apache.log4j.FileAppender      log4j.appender.toFile.Threshold=INFO    log4j.appender.toFile.layout=org.apache.log4j.PatternLayout      log4j.appender.toFile.file=test.log      ########  \u8F93\u51FA\u5230\u5230\u63A7\u5236\u53F0  ##########      log4j.appender.console=org.apache.log4j.ConsoleAppender	log4j.appender.console.Threshold=INFO	log4j.appender.console.ImmediateFlush=true	log4j.appender.console.Target=System.err	log4j.appender.console.layout=org.apache.log4j.PatternLayout	log4j.appender.console.layout.ConversionPattern=[%d{yyyy-MM-dd HH:mm:ss.SSS}] %p [%t] %c{1}.%M(%L) | %m%n		log4j.appender.R=org.apache.log4j.RollingFileAppender  	log4j.appender.R.File=e:/logs/douban.log	log4j.appender.R.MaxBackupIndex=10  	log4j.appender.R.layout=org.apache.log4j.PatternLayout  	log4j.appender.R.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n

下面来具体分析这个配置文件

log4j用法