Java编写日志类-Web版与普通项目版-保存日志文件到指定路径

这是我在 之前的日志类进行优化添加功能后得到的,比较简单-功能就是输出日志 将日志保存,每天一个不同的日志文件

 此次发现web项目的路径与普通 项目的日志路径保存会不同,所以做了两个版本

简单的日志类https://blog.csdn.net/qq_41806966/article/details/102529664

WebLog

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * -Web项目日志类
 * -web项目 日志会保存在tomcat的WebContent/logs目录下
 * -普通项目 日志会保存在项目的/logs目录下
 * Made in Shendi QQ:1711680493
 * this is great class
 * @author Shendi
 */
public class WebLog {
	//是否开启日志 默认开启
	private static boolean isLog = true;
	//日志文件保存路径 默认为当前项目路径
	private static String savePath = WebLog.class.getResource("/").getPath()+"../../";
	//输出流
	private static FileWriter writer = null;
	
	static {
		timingUpdateWriter();
	}
	
	/**
	 * -输出日志
	 * @param obj
	 */
	public static void print(Object obj) {
		isDefaultWriter();
		String str = getString()+obj;
		//日志开启才输出
		if (isLog) {
			//获取堆栈跟踪信息
			System.out.println(str);
		}
		//将日志写在日志文件里
		try {
			writer.write("[Info] "+str+"\n");
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * -输出错误日志
	 * @param obj
	 */
	public static void printErr(Object obj) {
		isDefaultWriter();
		String str = getString()+obj;
		//日志开启才输出
		if (isLog) {
			System.err.println(str);
		}
		//将日志写在日志文件里
		try {
			writer.write("[Error] "+str+"\n");
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * -获取堆栈跟踪信息 获取调用者的调用者
	 * @return 信息
	 */
	private static StackTraceElement getStackTraceInfo() {
		StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
		StackTraceElement callInfo = stackTraceElements[4];
		return callInfo;
	}
	
	/**
	 * -获取调用者信息
	 * @return 信息
	 */
	private static String getString() {
		//获取堆栈跟踪信息
		StackTraceElement callInfo = getStackTraceInfo();
		return "["+ getCurrentTime() +"] "+callInfo.getClassName()+"."+callInfo.getMethodName()+"()."+callInfo.getLineNumber()+"\t\t>";
	}
	
	/**
	 * -如果writer等于null 则赋予其默认值
	 */
	private static void isDefaultWriter() {
		if (writer == null) {
			try {
				File file = new File(savePath+"/logs");
				if (!file.exists()) {
					file.mkdirs();
				}
				writer = new FileWriter(savePath+"/logs/" + getCurrentDate(),true);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * -设置日志是否在控制台可见 true可见 false不可见
	 * @param isLog
	 */
	public static void setIsLog(boolean isLog) {
		System.err.println(getString() + "设置了日志:"+isLog);
		WebLog.isLog = isLog;
	}
	
	/**
	 * -获取当前时间 年-月-日 时:分:秒
	 * @return
	 */
	private static String getCurrentTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date());
	}
	
	/**
	 * -获取当前日期 年-月-日
	 * @return
	 */
	private static String getCurrentDate() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
	
	/**
	 * -返回日志保存的路径
	 * @return
	 */
	public static String getSavePath() {
		return savePath;
	}
	
	/**
	 * -设置日志保存的路径
	 * @param savePath
	 */
	public static void setSavePath(String savePath) {
		WebLog.savePath = savePath;
		//关闭之前的流
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//进入垃圾回收
		writer = null;
	}
	
	/**
	 * -定时更换输出流 每天指向一个新的不同的文件
	 */
	private static void timingUpdateWriter() {
		Timer timer = new Timer();
		//计算到第二天的时间
		Date date = new Date();
		String sDate = new SimpleDateFormat("yyyyMMdd").format(date);
		int year = Integer.parseInt(sDate.substring(0,4));
		int month = Integer.parseInt(sDate.substring(4,6));
		int day = Integer.parseInt(sDate.substring(6,8));
		Date tomorrow = new Date(year-1900,month-1,day+1);
		long startTime = tomorrow.getTime() - date.getTime();
		timer.schedule(new TimerTask() {
			public void run() {
				writer = null;
			}
		},startTime,86400000);
	}

普通 项目Log类

Log类

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * -普通项目日志类
 * -web项目 日志会保存在tomcat的web-inf下的classes/logs目录下
 * -普通项目 日志会保存在项目的/logs目录下
 * Made in Shendi QQ:1711680493
 * this is great class
 * @author Shendi
 */
public class Log {
	//是否开启日志 默认开启
	private static boolean isLog = true;
	//日志文件保存路径 默认为当前项目路径
	private static String savePath = Log.class.getResource("/").getPath()+"../";
	//输出流
	private static FileWriter writer = null;
	
	static {
		timingUpdateWriter();
	}
	
	/**
	 * -输出日志
	 * @param obj
	 */
	public static void print(Object obj) {
		isDefaultWriter();
		String str = getString()+obj;
		//日志开启才输出
		if (isLog) {
			//获取堆栈跟踪信息
			System.out.println(str);
		}
		//将日志写在日志文件里
		try {
			writer.write("[Info] "+str+"\n");
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * -输出错误日志
	 * @param obj
	 */
	public static void printErr(Object obj) {
		isDefaultWriter();
		String str = getString()+obj;
		//日志开启才输出
		if (isLog) {
			System.err.println(str);
		}
		//将日志写在日志文件里
		try {
			writer.write("[Error] "+str+"\n");
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * -获取堆栈跟踪信息 获取调用者的调用者
	 * @return 信息
	 */
	private static StackTraceElement getStackTraceInfo() {
		StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
		StackTraceElement callInfo = stackTraceElements[4];
		return callInfo;
	}
	
	/**
	 * -获取调用者信息
	 * @return 信息
	 */
	private static String getString() {
		//获取堆栈跟踪信息
		StackTraceElement callInfo = getStackTraceInfo();
		return "["+ getCurrentTime() +"] "+callInfo.getClassName()+"."+callInfo.getMethodName()+"()."+callInfo.getLineNumber()+"\t\t>";
	}
	
	/**
	 * -如果writer等于null 则赋予其默认值
	 */
	private static void isDefaultWriter() {
		if (writer == null) {
			try {
				File file = new File(savePath+"/logs");
				if (!file.exists()) {
					file.mkdirs();
				}
				writer = new FileWriter(savePath+"/logs/" + getCurrentDate(),true);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * -设置日志是否在控制台可见 true可见 false不可见
	 * @param isLog
	 */
	public static void setIsLog(boolean isLog) {
		System.err.println(getString() + "设置了日志:"+isLog);
		Log.isLog = isLog;
	}
	
	/**
	 * -获取当前时间 年-月-日 时:分:秒
	 * @return
	 */
	private static String getCurrentTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:sss").format(new Date());
	}
	
	/**
	 * -获取当前日期 年-月-日
	 * @return
	 */
	private static String getCurrentDate() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
	
	/**
	 * -返回日志保存的路径
	 * @return
	 */
	public static String getSavePath() {
		return savePath;
	}
	
	/**
	 * -设置日志保存的路径
	 * @param savePath
	 */
	public static void setSavePath(String savePath) {
		Log.savePath = savePath;
		//关闭之前的流
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		//进入垃圾回收
		writer = null;
	}
	
	/**
	 * -定时更换输出流 每天指向一个新的不同的文件
	 */
	private static void timingUpdateWriter() {
		Timer timer = new Timer();
		//计算到第二天的时间
		Date date = new Date();
		String sDate = new SimpleDateFormat("yyyyMMdd").format(date);
		int year = Integer.parseInt(sDate.substring(0,4));
		int month = Integer.parseInt(sDate.substring(4,6));
		int day = Integer.parseInt(sDate.substring(6,8));
		Date tomorrow = new Date(year-1900,month-1,day+1);
		long startTime = tomorrow.getTime() - date.getTime();
		timer.schedule(new TimerTask() {
			public void run() {
				writer = null;
			}
		},startTime,86400000);
	}
}

 如果有什么缺陷或者更好的方法请在下方评论指出

发布了38 篇原创文章 · 获赞 23 · 访问量 9068

猜你喜欢

转载自blog.csdn.net/qq_41806966/article/details/102850559
今日推荐