1.下载jar:log4j-1.2.15.jar
2.配置文件:log4j.properties
# Priorities are: DEBUG, INFO, WARN, ERROR, or FATAL.log4j.rootCategory=INFO, CONSOLE, FILElogs.dir=${catalina.base}/logs/springdemo/logs.filename=logsfileBufferSize=20480#log to Filelog4j.appender.FILE=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.FILE.File=D:/${number}/10.logslog4j.appender.FILE.encoding=UTF-8log4j.appender.FILE.Append=truelog4j.appender.FILE.LocationInfo=truelog4j.appender.FILE.DatePattern='.'yyyy-MM-dd'.log'log4j.appender.FILE.layout=org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.ConversionPattern=[%d]: %m %x %nlog4j.appender.FILE.bufferSize=${fileBufferSize}# CONSOLE is set to be a ConsoleAppender using a PatternLayout.log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.CONSOLE.Threshold=DEBUGlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.appender.CONSOLE.layout.ConversionPattern=- %m%n
3.java类:
package com.log.test;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.log4j.Logger;import org.apache.log4j.PropertyConfigurator;public class DemoTest { static Logger logger = Logger.getLogger(DemoTest.class.getName()); public static void main(String[] args) throws Exception { // 格式化时间 年月日 SimpleDateFormat data = new SimpleDateFormat("YYYYMMdd"); String format = data.format(new Date()); // 动态配置路径 System.setProperty("number", format);//以日期建文件夹 // 加载配置文件 PropertyConfigurator.configure("config/log4j.properties");//配置文件的路径 // 输出日志信息 logger.debug("HellosdsdWoasdassdasdasdfdsfrld"); }}