1、首先到Apache官网下载log4j.jar文件http://logging.apache.org/log4j/1.2/download.html 引用到eclipse项目里面
2、在src目录下建立:log4j.properties
log4j.rootCategory=debug, stdout,R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.appender.R.File=log/log.log # 如果为 (每天产生日志就不需要设置下面属性)DailyRollingFileAppender #log4j.appender.R.MaxFileSize=100KB #log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d{yyyy MMM dd HH:mm:ss} %-5p %c - %m%n
3、使用
public class Main { static Logger logger = Logger.getLogger(Main.class.getName()); public static void main(String[] args) throws IOException { // TODO Auto-generated method stub System.out.println("Hello World!"); PropertyConfigurator.configure ("src//log4j.properties"); logger.debug("Debug ..."); logger.info("Info ..."); logger.warn("Warn ..."); logger.error("Error ..."); } }
时间: 2024-11-08 07:26:55