Log4j 4种日志文件生成方式

参考了某位兄弟的代码, 但是使用了之后,回过头来写博客忘记原地址,勿怪。

Log4j  4种日志文件生成方式

方式1 : 按天生成日志  org.apache.log4j.DailyRollingFileAppender (log4j.jar 自带)

方式2 : 按大小生成日志   org.apache.log4j.RollingFileAppender (log4j.jar  自带)

方式3 : 支持每日创建新的日志文件的同时,又限制日志文件的大小、个数 Log4JDateAndSizeSplit  (自定义,见下面的类代码)

方式4 :每日创建新的日志文件的同时,又限制日志文件的个数 Log4jDateSplitAndCountControl (自定义,见下面的类代码)

Log4JDateAndSizeSplit  代码如下

package com.sino.lgdd.utils;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.helpers.CountingQuietWriter;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.helpers.OptionConverter;
import org.apache.log4j.spi.LoggingEvent;

/**
 * 支持每日创建新的日志文件的同时,又限制日志文件的大小、个数
 * MaxBackupIndex=-1 的时候表示不限制个数
 */
public class Log4JDateAndSizeSplit extends FileAppender {
	static final int TOP_OF_TROUBLE = -1;
	static final int TOP_OF_MINUTE = 0;
	static final int TOP_OF_HOUR = 1;
	static final int HALF_DAY = 2;
	static final int TOP_OF_DAY = 3;
	static final int TOP_OF_WEEK = 4;
	static final int TOP_OF_MONTH = 5;

	/** 
	 * The default maximum file size is 10MB. 
	 */
	protected long maxFileSize = 10 * 1024 * 1024;

	/** 
	 * There is one backup file by default. 
	 */
	protected int maxBackupIndex = 1;

	/** 
	 * The date pattern. By default, the pattern is set to "'.'yyyy-MM-dd" 
	 * meaning daily rollover. 
	 */
	private String datePattern = "'.'yyyy-MM-dd";

	/** 
	 * The log file will be renamed to the value of the scheduledFilename 
	 * variable when the next interval is entered. For example, if the rollover 
	 * period is one hour, the log file will be renamed to the value of 
	 * "scheduledFilename" at the beginning of the next hour. 
	 *  
	 * The precise time when a rollover occurs depends on logging activity. 
	 */
	private String scheduledFilename;

	/** 
	 * The next time we estimate a rollover should occur. 
	 */
	private long nextCheck = System.currentTimeMillis() - 1;

	Date now = new Date();

	SimpleDateFormat sdf;

	RollingCalendar rc = new RollingCalendar();

	int checkPeriod = TOP_OF_TROUBLE;

	// The gmtTimeZone is used only in computeCheckPeriod() method.  
	static final TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");

	/** 
	 * The default constructor does nothing. 
	 */
	public Log4JDateAndSizeSplit() {
	}

	/** 
	 * Instantiate a <code>Log4JDateAndSizeSplit</code> and open the file 
	 * designated by <code>filename</code>. The opened filename will become the 
	 * ouput destination for this appender. 
	 */
	public Log4JDateAndSizeSplit(Layout layout, String filename, String datePattern) throws IOException {
		super(layout, filename, true);
		this.datePattern = datePattern;
		activateOptions();
	}

	/** 
	 * Get the maximum size that the output file is allowed to reach before 
	 * being rolled over to backup files. 
	 *  
	 * @since 1.1 
	 */
	public long getMaximumFileSize() {
		return maxFileSize;
	}

	/** 
	 * Set the maximum size that the output file is allowed to reach before 
	 * being rolled over to backup files. 
	 *  
	 * <p> 
	 * This method is equivalent to {@link #setMaxFileSize} except that it is 
	 * required for differentiating the setter taking a <code>long</code> 
	 * argument from the setter taking a <code>String</code> argument by the 
	 * JavaBeans {@link java.beans.Introspector Introspector}. 
	 *  
	 * @see #setMaxFileSize(String) 
	 */
	public void setMaximumFileSize(long maxFileSize) {
		this.maxFileSize = maxFileSize;
	}

	/** 
	 * Set the maximum size that the output file is allowed to reach before 
	 * being rolled over to backup files. 
	 *  
	 * <p> 
	 * In configuration files, the <b>MaxFileSize</b> option takes an long 
	 * integer in the range 0 - 2^63. You can specify the value with the 
	 * suffixes "KB", "MB" or "GB" so that the integer is interpreted being 
	 * expressed respectively in kilobytes, megabytes or gigabytes. For example, 
	 * the value "10KB" will be interpreted as 10240. 
	 */
	public void setMaxFileSize(String value) {
		maxFileSize = OptionConverter.toFileSize(value, maxFileSize + 1);
	}

	/** 
	 * Returns the value of the <b>MaxBackupIndex</b> option. 
	 */
	public int getMaxBackupIndex() {
		return maxBackupIndex;
	}

	/** 
	 * Set the maximum number of backup files to keep around. 
	 *  
	 * <p> 
	 * The <b>MaxBackupIndex</b> option determines how many backup files are 
	 * kept before the oldest is erased. This option takes a positive integer 
	 * value. If set to zero, then there will be no backup files and the log 
	 * file will be truncated when it reaches <code>MaxFileSize</code>. 
	 */
	public void setMaxBackupIndex(int maxBackups) {
		this.maxBackupIndex = maxBackups;
	}

	/** 
	 * The <b>DatePattern</b> takes a string in the same format as expected by 
	 * {@link SimpleDateFormat}. This options determines the rollover schedule. 
	 */
	public void setDatePattern(String pattern) {
		datePattern = pattern;
	}

	/** Returns the value of the <b>DatePattern</b> option. */
	public String getDatePattern() {
		return datePattern;
	}

	public void activateOptions() {
		super.activateOptions();
		if (datePattern != null && fileName != null)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明
要实现log4j每分钟生成一个日志文件,可以使用log4j的TimeBasedRollingPolicy。具体步骤如下: 1. 在log4j.properties文件中添加以下配置: ```properties log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=/path/to/log/file/logfile.log log4j.appender.file.RollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy log4j.appender.file.RollingPolicy.FileNamePattern=/path/to/log/file/logfile.%d{yyyyMMdd-HHmm}.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n ``` 2. 解释配置: - log4j.appender.file:指定日志输出到文件。 - log4j.appender.file.File:指定日志输出文件的路径和名称。 - log4j.appender.file.RollingPolicy:指定日志文件滚动策略,即按时间滚动。 - log4j.appender.file.RollingPolicy.FileNamePattern:指定日志文件名的格式,%d{yyyyMMdd-HHmm}表示每分钟生成一个新文件。 - log4j.appender.file.layout:指定日志输出格式。 - log4j.appender.file.layout.ConversionPattern:指定日志输出内容的格式。 3. 编写Java代码,使用log4j进行日志记录。 ```java import org.apache.log4j.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class); public static void main(String[] args) { logger.info("Hello, world!"); } } ``` 这样,每分钟都会生成一个新的日志文件日志内容按照指定格式输出到文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值