参考
https://blog.csdn.net/weixin_38458994/article/details/89916687
https://blog.csdn.net/vtopqx/article/details/8432862
1.在pom.xml中添加依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
2.在同一个包下新建log4j.properties配置文件,配置内容:
log4j.rootLogger=INFO,console,file2
#log4j.rootLogger=INFO,console,file,file2
# ALL TRACE DEBUG INFO WARE ERROR FATAL OFF
#INFO DEBUG ERROR
#log4j.rootCategory=INFO, stdout, R
#log4j.appender.file2=org.apache.log4j.ConsoleAppender
log4j.appender.console=org.apache.log4j.ConsoleAppender
#log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#windows
#log4j.appender.file.File=E:/cdcMonitorWAS.log
#linux
#log4j.appender.file.File=/cdcMonitorWAS/logs/cdcMonitorWAS.log
#log4j.appender.file.File=/usr/local/hadoop/logsdata/accesslog/access_log
#log4j.appender.file.DatePattern=yyyy-MM-dd'.txt'
#file2----------
log4j.appender.file2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file2.Append=true
log4j.appender.file2.DatePattern='.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.file2.File=/usr/local/hadoop/logsdata/accesslog/access_log
log4j.appender.file2.Threshold=INFO
log4j.appender.file2.Encoding=UTF-8
log4j.appender.file2.layout=org.apache.log4j.PatternLayout
log4j.appender.file2.layout.ConversionPattern=%d{yyyy MM dd HH:mm:ss} %-5p %20c %3x - %m%n
#SqlMap logging configuration...
#log4j.logger.com.ibatis=DEBUG
#log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
#log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
#log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
#log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
#log4j.logger.java.sql.ResultSet=DEBUG
log4j.appender.console.layout=org.apache.log4j.SimpleLayout
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy MM dd HH:mm:ss} %-5p %20c %3x - %m%n
#log4j.appender.file.layout.ConversionPattern=%d{yyyy MM dd HH:mm:ss} %-10r %-5p [%t] %20c %3x - %m%n
log4j.logger.com.ibatis=debug
log4j.logger.com.opensymphony.xwork2=info
log4j.logger.java.sql=debug
log4j.logger.org.apache.struts=info
log4j.logger.org.springframework=info
log4j.logger.org.apache.cxf=info
3.定时生成log日志文件,在main函数中添加定时任务。
timer.schedule(new CreateLog(),0,60*1000L);
4.新建CreateLog类
package org.example;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.TimerTask;
import org.apache.log4j.Logger;
//import java.util.logging.Logger;
public class CreateLog extends TimerTask {
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
String day = sdf.format(new Date());
Logger logger = Logger.getLogger("file2");
logger.info("生成日志:"+day);
}
}