1.下载jar:log4j-1.2.15.jar
2.配置文件:log4j.properties
# Priorities are: DEBUG, INFO, WARN, ERROR, or FATAL. log4j.rootCategory=INFO, CONSOLE, FILE logs.dir=${catalina.base}/logs/springdemo/ logs.filename=logs fileBufferSize=20480 #log to File log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender log4j.appender.FILE.File=D:/${number}/10.logs log4j.appender.FILE.encoding=UTF-8 log4j.appender.FILE.Append=true log4j.appender.FILE.LocationInfo=true log4j.appender.FILE.DatePattern=‘.‘yyyy-MM-dd‘.log‘ log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=[%d]: %m %x %n log4j.appender.FILE.bufferSize=${fileBufferSize} # CONSOLE is set to be a ConsoleAppender using a PatternLayout. log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.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"); } }
时间: 2024-11-05 16:38:47