⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log4j.java

📁 自己用JAVA SE写的一个模仿web应用
💻 JAVA
字号:
package com.liaobin;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.Calendar;

/**
 * 自己写的日志类
 * 
 * @author liaobin
 * 
 */
public class Log4j {

	/**
	 * 一般信息的记录
	 * 
	 * @param s
	 */
	public static void info(String s) {
		s = "INFO"+getHHMMSS()+":" + s;
		wirte2File(s);
	}

	/**
	 * 记录错误信息
	 * 
	 * @param s
	 */
	public static void error(String s) {
		s = "ERROR:"+getHHMMSS()+":" + s;
		wirte2File(s);
	}

	private static void wirte2File(String msg) {

		RandomAccessFile raf = null;
		// 得到path
		String path = System.getProperties().getProperty("user.dir")
				+ java.io.File.separator + "log";
		// 创建log文件夹
		File file = new File(path);
		if (!file.exists()) {
			file.mkdir();
		}
		path += File.separator + "liaobin"+getYYMMDD() + ".log";
		// 创建Log文件
		File logfile = new File(path);
		// 如果创建的logfile是文件夹,则删除
		if (logfile.isDirectory()) {
			logfile.delete();
		}

		try {
			raf = new RandomAccessFile(logfile, "rwd");
			raf.seek(logfile.length());
			
			msg += "\r\n";
			raf.write(msg.getBytes());

			raf.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 得到系统当前的时间 年月日 信息
	 * @return
	 */
	public static String getYYMMDD() {
		Calendar now = Calendar.getInstance();
		String year = Integer.toString(now.get(Calendar.YEAR));
		String month = Integer.toString(now.get(Calendar.MONTH));
		String day = Integer.toString(now.get(Calendar.DAY_OF_MONTH));
		month = month.length() == 1 ? "0" + month : month;
		day = day.length() == 1 ? "0" + day : day;

		return year + month + day;

	}
	/**
	 * 得到系统当前的 时时分分秒秒信息
	 * @return
	 */
	public static String getHHMMSS() {
		Calendar now = Calendar.getInstance();
		String hour = Integer.toString(now.get(Calendar.HOUR_OF_DAY));
		String minutes = Integer.toString(now.get(Calendar.MINUTE));
		String second = Integer.toString(now.get(Calendar.SECOND));
		hour = hour.length() == 1 ? "0" + hour : hour;
		minutes = minutes.length() == 1 ? "0" + minutes : minutes;
		second=second.length()==1?"0"+second:second;

		return hour + minutes + second;

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -