package com.chinacreator.process.util;

import java.util.HashMap;

import org.apache.log4j.Logger;

/**
 * 写日志工具类，每间隔多长时间写一次日志，以减少日志量
 * @author xu.zhou
 * @date 20211101
 */
public class WriteLogUtil {
	
	private static HashMap<String, Long> writeMap = new HashMap<String, Long>();
	
	private static long interval = 60L; //写日志问隔，默认60秒
	
	/**
	 * 指定间隔时间写日志
	 * @param logmsg 日志内容
	 * @param loghandle 日志handle
	 * @param logclass 要写日志的类
	 * @param loginterval 写日志问隔，单位秒
	 */
	public static void writeLong(String logmsg, Logger loghandle, String logclass, Long loginterval){
		try {
			long currtime = System.currentTimeMillis()/1000;
			if(writeMap.containsKey(logclass)){
				if(currtime - writeMap.get(logclass) > loginterval){
					writeMap.put(logclass, currtime);
					loghandle.info(logmsg);
				}
			}else{
				writeMap.put(logclass, currtime);
			}
		} catch (Exception e) {
			//e.printStackTrace();
			loghandle.error("【"+logclass+"】写日志出现异常，"+e.getMessage());
		}
	}
	
	/**
	 * 问隔60秒写一次日志
	 * @param logmsg 日志内容
	 * @param loghandle 日志handle
	 * @param logclass 要写日志的类
	 */
	public static void writeLong(String logmsg, Logger loghandle, String logclass){
		try {
			long currtime = System.currentTimeMillis()/1000;
			if(writeMap.containsKey(logclass)){
				if(currtime - writeMap.get(logclass) > interval){
					writeMap.put(logclass, currtime);
					loghandle.info(logmsg);
				}
			}else{
				writeMap.put(logclass, currtime);
			}
		} catch (Exception e) {
			//e.printStackTrace();
			loghandle.error("【"+logclass+"】写日志出现异常，"+e.getMessage());
		}
	}
}
