log4j生成日志文件配置_定时每分钟生成新的log文件

参考

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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值