LogUtil.java 1.7 KB
package com.espeed.reading.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * 
 * @项目名称: 邮件跟踪系统
 * @版权所有: 深圳市科飞时速网络技术有限公司(0755-88843776)
 * @技术支持: info@21gmail.com
 * @单元名称: 日志工具类
 * @开始时间: 2018-5-23
 * @开发人员: 杨志钊
 */
public class LogUtil {

	private final static Logger logger = Logger.getLogger("syslog");
	private final static String savePath = ConfigPath.getLogSavePath();
	private final static SimpleDateFormat dateFormat = new SimpleDateFormat(
			"yyyy-MM-dd");
	private static String path = null;

	static {
		try {
			path = LogUtil.class.getClassLoader().getResource("").toURI()
					.getPath().replace("classes/", "")
					+ "log4j.properties";
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

	/** 保存日志 */
	public static void saveInfoLog(Object userId, Exception e) {

		System.setProperty("logPath", savePath + "/" + userId + "/"
				+ dateFormat.format(new Date()));

		// 读取log4j配置文件
		PropertyConfigurator.configure(path);

		StringWriter sw = new StringWriter();
		e.printStackTrace(new PrintWriter(sw, true));
		String info = sw.toString();

		logger.error(info);
	}

	/** 保存日志 */
	public static void saveInfoLog(Object userId, String info) {
		System.setProperty("logPath", savePath + "/" + userId + "/"
				+ dateFormat.format(new Date()));

		// 读取log4j配置文件
		PropertyConfigurator.configure(path);

		logger.error(info);
	}

}